|
@ -5,6 +5,7 @@ import de.tims.tictactoe.GameLogic; |
|
|
public class AIHard implements TicTacToeAI { |
|
|
public class AIHard implements TicTacToeAI { |
|
|
private static final char AI_CHAR = 'o'; |
|
|
private static final char AI_CHAR = 'o'; |
|
|
private static final char EMPTY_CHAR = '-'; |
|
|
private static final char EMPTY_CHAR = '-'; |
|
|
|
|
|
private static final char PLAYER_CHAR = 'x'; |
|
|
private static final int BOARD_SIZE = 3; |
|
|
private static final int BOARD_SIZE = 3; |
|
|
|
|
|
|
|
|
private GameLogic gl; |
|
|
private GameLogic gl; |
|
@ -18,16 +19,22 @@ public class AIHard implements TicTacToeAI { |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void calculateNextMove() { |
|
|
public void calculateNextMove() { |
|
|
boolean emptyBoard = true; |
|
|
|
|
|
|
|
|
int charsOfOpponent = 0; |
|
|
char[][] board = gl.getBoard(); |
|
|
char[][] board = gl.getBoard(); |
|
|
|
|
|
|
|
|
for (char[] row : board) { |
|
|
|
|
|
for (char field : row) { |
|
|
|
|
|
emptyBoard &= field == EMPTY_CHAR; |
|
|
|
|
|
|
|
|
for (int i = 0; i < BOARD_SIZE; i++) { |
|
|
|
|
|
charsOfOpponent = countCharsInRow(i, PLAYER_CHAR); |
|
|
|
|
|
if (charsOfOpponent == 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 (emptyBoard || board[1][1] == EMPTY_CHAR) { |
|
|
|
|
|
|
|
|
if (board[1][1] == EMPTY_CHAR) { |
|
|
gl.setField(1, 1, AI_CHAR); |
|
|
gl.setField(1, 1, AI_CHAR); |
|
|
} else { |
|
|
} else { |
|
|
boolean emptyEdgeFound = false; |
|
|
boolean emptyEdgeFound = false; |
|
|