Browse Source

tictactoe: hard AI chooses edge field if field in the middle was already set

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

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

@ -17,6 +17,17 @@ public class AIHard implements TicTacToeAI {
@Override
public void calculateNextMove() {
gl.setField(1, 1, AI_CHAR);
boolean emptyBoard = true;
char[][] board = gl.getBoard();
for (char[] row : board) {
for (char field : row) {
emptyBoard &= field == '-';
}
}
if (emptyBoard) {
gl.setField(1, 1, AI_CHAR);
} else {
gl.setField(0, 0, AI_CHAR);
}
}
}

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

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