-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMediumAIPlayer.java
More file actions
58 lines (51 loc) · 2.24 KB
/
MediumAIPlayer.java
File metadata and controls
58 lines (51 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package tictactoe;
public class MediumAIPlayer extends AIPlayer {
private final char enemy;
public MediumAIPlayer(char identifier) {
super(identifier, "medium");
enemy = identifier == 'X' ? 'O' : 'X';
}
@Override
public int getTurn(char[][] gameboard) {
int move = -1;
int len = gameboard.length;
for (int i = 0; i < len; i++) {
int prevRow = i - 1 < 0 ? len - 1 : i - 1;
int nextRow = i + 1 >= len ? 0 : i + 1;
for (int j = 0; j < len; j++) {
char current = gameboard[i][j];
int prevCol = j - 1 < 0 ? len - 1 : j - 1;
int nextCol = j + 1 >= len ? 0 : j + 1;
if (current == ' ' || current == '_') {
if (gameboard[i][nextCol] == gameboard[i][prevCol]) {
if (gameboard[i][nextCol] == identifier || (gameboard[i][nextCol] == enemy && move == -1)) {
move = translateToPos(len, i, j);
}
}
if (gameboard[nextRow][j] == gameboard[prevRow][j]) {
if (gameboard[nextRow][j] == identifier || (gameboard[nextRow][j] == enemy && move == -1)) {
move = translateToPos(len, i, j);
}
}
if (j - i == 0 && gameboard[nextRow][nextCol] == gameboard[prevRow][prevCol]) {
if (gameboard[nextRow][nextCol] == identifier ||
(gameboard[nextRow][nextCol] == enemy && move == -1)) {
move = translateToPos(len, i, j);
}
}
if (i + j + 1 == len && gameboard[prevRow][nextCol] == gameboard[nextRow][prevCol]) {
if (gameboard[prevRow][nextCol] == identifier ||
(gameboard[prevRow][nextCol] == enemy && move == -1)) {
move = translateToPos(len, i, j);
}
}
}
}
}
if (move == -1) {
return getRandomMove(gameboard);
} else {
return move;
}
}
}