|
@ -19,15 +19,17 @@ public class AIHard implements TicTacToeAI { |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void calculateNextMove() { |
|
|
public void calculateNextMove() { |
|
|
int charsOfOpponent = 0; |
|
|
|
|
|
|
|
|
int charsOfOpponentInRow = 0; |
|
|
|
|
|
int charsOfOpponentInCol = 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++) { |
|
|
charsOfOpponent = countCharsInRow(i, PLAYER_CHAR); |
|
|
|
|
|
if (charsOfOpponent == BOARD_SIZE - 1) { |
|
|
|
|
|
|
|
|
charsOfOpponentInRow = countCharsInRow(i, PLAYER_CHAR); |
|
|
|
|
|
charsOfOpponentInCol = countCharsInCol(i, PLAYER_CHAR); |
|
|
|
|
|
if (charsOfOpponentInRow == BOARD_SIZE - 1 || charsOfOpponentInCol == BOARD_SIZE - 1) { |
|
|
for (int j = 0; j < BOARD_SIZE; j++) { |
|
|
for (int j = 0; j < BOARD_SIZE; j++) { |
|
|
if (board[i][j] == EMPTY_CHAR) { |
|
|
|
|
|
gl.setField(i, j, AI_CHAR); |
|
|
|
|
|
|
|
|
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); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|