-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOpponent.java
More file actions
111 lines (105 loc) · 3.09 KB
/
Opponent.java
File metadata and controls
111 lines (105 loc) · 3.09 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
public class Opponent {
// Will return the index of the best move to make
public int getMove(Board board) {
Board boardCopy = board.copy();
// Take middle if it's not taken
if (boardCopy.spotAvailable(4)) {
return 4;
// Return index of corner if middle is taken by player
} else if (boardCopy.getBoard()[4] == board.getPlayer() &&
boardCopy.spotAvailable(2)) {
return 2;
}
// Return index of first winning positon for computer
int computerIndex = findWinPosition(boardCopy, board.getComputer());
if (computerIndex != -1) {
return computerIndex;
}
// Return index of first winning position for player
int playerIndex = findWinPosition(boardCopy, board.getPlayer());
if (playerIndex != -1) {
return playerIndex;
}
// Return index of fork for computer
computerIndex = findFork(boardCopy, board.getComputer(), 2);
if (computerIndex != -1) {
return computerIndex;
}
// Return index of best move against player's fork
playerIndex = findFork(boardCopy, board.getPlayer(), 2);
if (playerIndex != -1) {
// Play two in a row to counter fork
if (boardCopy.getBoard()[4] == boardCopy.getPlayer()) {
return findFork(boardCopy, board.getComputer(), 1);
// Play into fork position of player
} else {
return playerIndex;
}
}
// Return index of opposite corners to player
for (int i=0; i<boardCopy.getLength(); i+= 2) {
if (boardCopy.getBoard()[i] == board.getPlayer() &&
i != 4 && boardCopy.spotAvailable(8 - i)) {
return 8 - i;
}
}
// Return index of first available corner
for (int i=0; i<boardCopy.getLength(); i+= 2) {
if (i != 4 && boardCopy.spotAvailable(i)) {
return i;
}
}
// Return index of first open spot
for (int i=0; i<boardCopy.getLength(); i++) {
if (boardCopy.spotAvailable(i)) {
return i;
}
}
return -1;
}
// Returns index of first position that wins the game for piece
public int findWinPosition(Board board, char piece) {
Board boardCopy = board.copy();
Board newBoardCopy;
// Return index of first winning spot for piece
for (int i=0; i<boardCopy.getLength(); i++) {
newBoardCopy = boardCopy.copy();
if (newBoardCopy.spotAvailable(i)) {
newBoardCopy.newPiece(piece, i);
if (newBoardCopy.isWinner(piece) == true) {
return i;
}
}
}
// No win position found
return -1;
}
// Return index of position that leads to a particular number of possible
// wins on the next turn (Only works with 1 or 2 wins)
public int findFork(Board board, char piece, int wins) {
Board boardCopy = board.copy();
Board newBoardCopy;
Board newBoardCopy2;
int totalWins;
for (int i=0; i<boardCopy.getLength(); i++) {
totalWins = 0;
newBoardCopy = boardCopy.copy();
if (newBoardCopy.spotAvailable(i)) {
newBoardCopy.newPiece(piece, i);
}
for (int j=0; j<boardCopy.getLength(); j++) {
newBoardCopy2 = newBoardCopy.copy();
if (newBoardCopy2.spotAvailable(j)) {
newBoardCopy2.newPiece(piece, j);
if (newBoardCopy2.isWinner(piece)) {
totalWins++;
}
if (totalWins >= wins) {
return i;
}
}
}
}
return -1;
}
}