|
|
@ -21,20 +21,37 @@ public class AIHard implements TicTacToeAI { |
|
|
|
public void calculateNextMove() { |
|
|
|
int charsOfOpponentInRow = 0; |
|
|
|
int charsOfOpponentInCol = 0; |
|
|
|
int charsOfOpponentInDiag = 0; |
|
|
|
char[][] board = gl.getBoard(); |
|
|
|
|
|
|
|
for (int i = 0; i < BOARD_SIZE; i++) { |
|
|
|
charsOfOpponentInRow = countCharsInRow(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++) { |
|
|
|
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; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (board[1][1] == EMPTY_CHAR) { |
|
|
|
gl.setField(1, 1, AI_CHAR); |
|
|
|