From 85f79d14bc2c5c486b80e698c4182e580852c012 Mon Sep 17 00:00:00 2001 From: Malte Schellhardt Date: Thu, 10 Feb 2022 23:26:10 +0100 Subject: [PATCH] tictactoe: added test case to check diagonal left win for player 1 --- src/main/java/de/tims/tictactoe/GameLogic.java | 7 +++++++ src/test/java/de/tims/tictactoe/GameLogicTest.java | 10 +++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/tims/tictactoe/GameLogic.java b/src/main/java/de/tims/tictactoe/GameLogic.java index 6fed5b6..ffaa984 100644 --- a/src/main/java/de/tims/tictactoe/GameLogic.java +++ b/src/main/java/de/tims/tictactoe/GameLogic.java @@ -64,6 +64,13 @@ public class GameLogic { countFields = 0; } + // check diagonal left + for (int i = this.board.length - 1, j = this.board.length - 1; i >= 0 ; i--, j--) { + if (board[i][j] == player) countFields++; + } + if(countFields == this.board.length) return finished = true; + + return finished; } diff --git a/src/test/java/de/tims/tictactoe/GameLogicTest.java b/src/test/java/de/tims/tictactoe/GameLogicTest.java index 30ab928..216e02c 100644 --- a/src/test/java/de/tims/tictactoe/GameLogicTest.java +++ b/src/test/java/de/tims/tictactoe/GameLogicTest.java @@ -196,13 +196,17 @@ class GameLogicTest { {'-', '-', '-'}}), Arguments.of("check a draw for player 2", 'o', false, new char[][] {{'o', 'o', 'x'}, - {'o', 'o', 'x'}, + {'o', 'x', 'x'}, {'x', 'x', 'o'}}), Arguments.of("check a draw for player 1", 'x', false, new char[][] {{'o', 'o', 'x'}, {'o', 'o', 'x'}, - {'x', 'x', 'o'}}) - ); + {'x', 'x', 'o'}}), + Arguments.of("check diagonal left win for player 1", 'x', true, new char[][] + {{'x', 'o', 'x'}, + {'x', 'x', 'o'}, + {'o', 'o', 'x'}}) + ); } }