Browse Source

tictactoe: added checkForWin for special player and checking all columns

tictactoe
Malte Schellhardt 2 years ago
committed by Lorenz Hohmann
parent
commit
26768bf8f3
  1. 15
      src/main/java/de/tims/tictactoe/GameLogic.java
  2. 26
      src/test/java/de/tims/tictactoe/GameLogicTest.java

15
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;
}
}

26
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<Arguments> 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', '-', '-'},
{'-', '-', '-'}})
);
}

Loading…
Cancel
Save