Browse Source

tictactoe: hard AI sets middle field if it hasnt been set yet

tictactoe
Tobias Krause 3 years ago
committed by Lorenz Hohmann
parent
commit
7c7e134ad9
  1. 4
      src/main/java/de/tims/tictactoe/ai/AIHard.java
  2. 11
      src/test/java/de/tims/tictactoe/ai/AIHardTest.java

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

@ -21,10 +21,10 @@ public class AIHard implements TicTacToeAI {
char[][] board = gl.getBoard(); char[][] board = gl.getBoard();
for (char[] row : board) { for (char[] row : board) {
for (char field : row) { for (char field : row) {
emptyBoard &= field == '-';
emptyBoard &= field == EMPTY_CHAR;
} }
} }
if (emptyBoard) {
if (emptyBoard || board[1][1] == EMPTY_CHAR) {
gl.setField(1, 1, AI_CHAR); gl.setField(1, 1, AI_CHAR);
} else { } else {
gl.setField(0, 0, AI_CHAR); gl.setField(0, 0, AI_CHAR);

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

@ -38,4 +38,15 @@ class AIHardTest {
verify(gl, times(1)).setField(0, 0, realChar); verify(gl, times(1)).setField(0, 0, realChar);
} }
@Test
void opponentDidntChooseMiddleFieldSoAIDoes() {
char realChar = 'o';
doReturn(new char[][] { {'-', '-', 'x'}, {'-', '-', '-'}, {'-', '-', '-'} }).when(gl).getBoard();
TicTacToeAI ai = new AIHard(gl);
ai.calculateNextMove();
verify(gl, times(1)).setField(1, 1, realChar);
}
} }
Loading…
Cancel
Save