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