Browse Source

tictactoe: countCharsInDiag counts number of given char in specific diagonal

tictactoe
Tobias Krause 3 years ago
committed by Lorenz Hohmann
parent
commit
cce28aacd0
  1. 9
      src/main/java/de/tims/tictactoe/ai/AIHard.java
  2. 5
      src/test/java/de/tims/tictactoe/ai/AIHardTest.java

9
src/main/java/de/tims/tictactoe/ai/AIHard.java

@ -79,6 +79,13 @@ public class AIHard implements TicTacToeAI {
}
public int countCharsInDiag(int index, char charToCount) {
return 0;
int count = 0;
char[][] board = gl.getBoard();
for (int i = 0; i < BOARD_SIZE; i++) {
count += (board[i][(index == 0) ? i : BOARD_SIZE - 1 - i] == charToCount) ? 1 : 0;
}
return count;
}
}

5
src/test/java/de/tims/tictactoe/ai/AIHardTest.java

@ -120,7 +120,10 @@ class AIHardTest {
private static Stream<Arguments> testCasesForCountCharsInDiag() {
return Stream.of(Arguments.of("EmptyFieldReturns0",
new char[][] { {'-', '-', '-'}, {'-', '-', '-'}, {'-', '-', '-'} },
0, 'o', 0));
0, 'o', 0),
Arguments.of("TwoCharsInRowReturnsTwo",
new char[][] { {'-', '-', 'o'}, {'o', 'o', '-'}, {'-', '-', '-'} },
1, 'o', 2));
}
}
Loading…
Cancel
Save