Browse Source

tictactoe: hard AI prevents opponents win in diagonal

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

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

@ -21,20 +21,37 @@ public class AIHard implements TicTacToeAI {
public void calculateNextMove() { public void calculateNextMove() {
int charsOfOpponentInRow = 0; int charsOfOpponentInRow = 0;
int charsOfOpponentInCol = 0; int charsOfOpponentInCol = 0;
int charsOfOpponentInDiag = 0;
char[][] board = gl.getBoard(); char[][] board = gl.getBoard();
for (int i = 0; i < BOARD_SIZE; i++) { for (int i = 0; i < BOARD_SIZE; i++) {
charsOfOpponentInRow = countCharsInRow(i, PLAYER_CHAR); charsOfOpponentInRow = countCharsInRow(i, PLAYER_CHAR);
charsOfOpponentInCol = countCharsInCol(i, PLAYER_CHAR); charsOfOpponentInCol = countCharsInCol(i, PLAYER_CHAR);
if (charsOfOpponentInRow == BOARD_SIZE - 1 || charsOfOpponentInCol == BOARD_SIZE - 1) {
if (i < 2) {
charsOfOpponentInDiag = countCharsInDiag(i, PLAYER_CHAR);
}
if (charsOfOpponentInRow == BOARD_SIZE - 1 || charsOfOpponentInCol == BOARD_SIZE - 1 || charsOfOpponentInDiag == BOARD_SIZE - 1) {
for (int j = 0; j < BOARD_SIZE; j++) { for (int j = 0; j < BOARD_SIZE; j++) {
if (board[(charsOfOpponentInRow == BOARD_SIZE - 1) ? i : j][(charsOfOpponentInRow == BOARD_SIZE - 1) ? j : i] == EMPTY_CHAR) {
gl.setField((charsOfOpponentInRow == BOARD_SIZE - 1) ? i : j, (charsOfOpponentInRow == BOARD_SIZE - 1) ? j : i, AI_CHAR);
if (charsOfOpponentInRow == BOARD_SIZE - 1) {
if (board[i][j] == EMPTY_CHAR) {
gl.setField(i, j, AI_CHAR);
return;
}
} else if (charsOfOpponentInCol == BOARD_SIZE - 1) {
if (board[j][i] == EMPTY_CHAR) {
gl.setField(j, i, AI_CHAR);
return;
}
} else if (charsOfOpponentInDiag == BOARD_SIZE - 1) {
if (board[j][(i == 0) ? j : BOARD_SIZE - 1 - j] == EMPTY_CHAR) {
gl.setField(j, (i == 0) ? j : BOARD_SIZE - 1 - j, AI_CHAR);
return; return;
} }
} }
} }
} }
}
if (board[1][1] == EMPTY_CHAR) { if (board[1][1] == EMPTY_CHAR) {
gl.setField(1, 1, AI_CHAR); gl.setField(1, 1, AI_CHAR);

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

@ -88,6 +88,17 @@ class AIHardTest {
verify(gl, times(1)).setField(2, 1, realChar); verify(gl, times(1)).setField(2, 1, realChar);
} }
@Test
void preventOpponentsWinInDiag() {
char realChar = 'o';
doReturn(new char[][] { {'x', '-', 'o'}, {'-', 'x', '-'}, {'-', '-', '-'} }).when(gl).getBoard();
TicTacToeAI ai = new AIHard(gl);
ai.calculateNextMove();
verify(gl, times(1)).setField(2, 2, realChar);
}
@ParameterizedTest @ParameterizedTest
@MethodSource("testCasesForCountCharsInRow") @MethodSource("testCasesForCountCharsInRow")
void countCharsInRowTest(String testName, char[][] board, int rowNum, char charToCount, int expectedResult) { void countCharsInRowTest(String testName, char[][] board, int rowNum, char charToCount, int expectedResult) {

Loading…
Cancel
Save