From 26768bf8f38b10093262ed59b41de110e63d5ba0 Mon Sep 17 00:00:00 2001 From: Malte Schellhardt Date: Wed, 9 Feb 2022 17:15:16 +0100 Subject: [PATCH] tictactoe: added checkForWin for special player and checking all columns --- .../java/de/tims/tictactoe/GameLogic.java | 15 +++++++++-- .../java/de/tims/tictactoe/GameLogicTest.java | 26 +++++++++++-------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/main/java/de/tims/tictactoe/GameLogic.java b/src/main/java/de/tims/tictactoe/GameLogic.java index 463bbc7..49582bc 100644 --- a/src/main/java/de/tims/tictactoe/GameLogic.java +++ b/src/main/java/de/tims/tictactoe/GameLogic.java @@ -41,8 +41,19 @@ public class GameLogic { return true; } - public boolean checkForWin() { - return true; + public boolean checkForWin(char player) { + boolean finished = false; + int countFields = 0; + + // check columns + for (int i = 0; i < this.board.length; i++) { + for (int j = 0; j < this.board[0].length; j++) { + if(board[i][j] == player) countFields++; + } + } + if(countFields == this.board.length) 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 11bffca..718caaf 100644 --- a/src/test/java/de/tims/tictactoe/GameLogicTest.java +++ b/src/test/java/de/tims/tictactoe/GameLogicTest.java @@ -80,10 +80,10 @@ class GameLogicTest { assertEquals(expectedResult, realResult); } - @ParameterizedTest(name = "[{index}] {0}") + @ParameterizedTest(name = "[{index}] {0}: should be {2}") @MethodSource("testCasesForCheckForWin") - void checkForWinTest(String testName, boolean expectedResult, char[][] boardToCheck) { - boolean realResult = new GameLogic(boardToCheck).checkForWin(); + void checkForWinTest(String testName, char player, boolean expectedResult, char[][] boardToCheck) { + boolean realResult = new GameLogic(boardToCheck).checkForWin(player); assertEquals(expectedResult, realResult); } @@ -134,14 +134,18 @@ class GameLogicTest { private static Stream testCasesForCheckForWin() { return Stream.of( - Arguments.of("check win for player 1", true, new char[][] - {{'x', '-', '-'}, - {'x', '-', '-'}, - {'x', '-', '-'}}), - Arguments.of("check win for player 2", true, new char[][] - {{'o', '-', '-'}, - {'o', '-', '-'}, - {'o', '-', '-'}}) + Arguments.of("check win for player 1", 'x', true, new char[][] + {{'x', '-', '-'}, + {'x', '-', '-'}, + {'x', '-', '-'}}), + Arguments.of("check win for player 2", 'o', true, new char[][] + {{'o', '-', '-'}, + {'o', '-', '-'}, + {'o', '-', '-'}}), + Arguments.of("check win for player 2", 'o', false, new char[][] + {{'o', '-', '-'}, + {'o', '-', '-'}, + {'-', '-', '-'}}) ); }