Browse Source

tictactoe: added test case to check if player 1 has won in column 0 with full board

tictactoe
Malte Schellhardt 2 years ago
committed by Lorenz Hohmann
parent
commit
8e9f622172
  1. 9
      src/main/java/de/tims/tictactoe/GameLogic.java
  2. 6
      src/test/java/de/tims/tictactoe/GameLogicTest.java

9
src/main/java/de/tims/tictactoe/GameLogic.java

@ -47,16 +47,19 @@ public class GameLogic {
// check columns
for (int i = 0; i < this.board.length; i++) {
if(countFields == this.board.length) return finished = true;
countFields = 0;
for (int j = 0; j < this.board[0].length; j++) {
if(board[i][j] == player) countFields++;
if(board[j][i] == player) countFields++;
}
}
if(countFields == this.board.length) finished = true;
countFields = 0;
// check rows
for (int i = 0; i < this.board[0].length; i++) {
for (int j = 0; j < this.board.length; j++) {
if(board[j][i] == player) countFields++;
if(board[i][j] == player) countFields++;
}
}
if(countFields == this.board.length) finished = true;

6
src/test/java/de/tims/tictactoe/GameLogicTest.java

@ -169,7 +169,11 @@ class GameLogicTest {
Arguments.of("check win in row 2 for player 2", 'o', true, new char[][]
{{'-', '-', '-'},
{'-', '-', '-'},
{'o', 'o', 'o'}})
{'o', 'o', 'o'}}),
Arguments.of("check win in column 0 for player 1 with full board", 'x', true, new char[][]
{{'x', 'o', 'o'},
{'x', 'o', 'x'},
{'x', 'x', 'o'}})
);
}

Loading…
Cancel
Save