-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.java
306 lines (281 loc) · 7.87 KB
/
Player.java
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import java.util.Random;
public abstract class Player implements Comparable<Player> {
private int playerNum;
private int[][] state = new int[3][3];
private int moveNumber;
protected Random prng = new Random();
private int wins, losses, ties;
public abstract Move getMove(boolean debug);
public abstract String getBotName();
public int compareTo(Player that) {
if (this.getWinPercent() > that.getWinPercent())
return -1;
if (this.getWinPercent() < that.getWinPercent())
return 1;
else
return 0;
}
/**
* Determine is a square is available based on the bot's view of the board
*
* @param move the square
* @return true if the square has not been marked, as far as the bot knows
*/
public boolean isAvailable(Move move) {
// Check if available
return state[move.getRow()][move.getColumn()] == 0;
}
public boolean canWinWithMove(Move move) {
// Check if available
if (!isAvailable(move))
return false;
// Check row
boolean canWin = true;
for (int i = 1; i < 3 && canWin; i++) {
int row = (move.getRow() + i) % 3;
if (state[row][move.getColumn()] != getPlayerNum()) {
canWin = false;
}
}
if (canWin)
return true;
// Check column
canWin = true;
for (int i = 1; i < 3 && canWin; i++) {
int col = (move.getColumn() + i) % 3;
if (state[move.getRow()][col] != getPlayerNum()) {
canWin = false;
}
}
if (canWin)
return true;
// Check diagonals
canWin = false;
if (move.getRow() == 0 && move.getColumn() == 0) {
canWin |= state[1][1] == state[2][2] && state[1][1] == getPlayerNum();
}
if (move.getRow() == 2 && move.getColumn() == 2) {
canWin |= state[0][0] == state[1][1] && state[1][1] == getPlayerNum();
}
if (move.getRow() == 0 && move.getColumn() == 2) {
canWin |= state[2][0] == state[1][1] && state[1][1] == getPlayerNum();
}
if (move.getRow() == 2 && move.getColumn() == 0) {
canWin |= state[0][2] == state[1][1] && state[1][1] == getPlayerNum();
}
if (move.getRow() == 1 && move.getColumn() == 1) {
canWin |= (state[0][0] == state[2][2] && state[0][0] == getPlayerNum())
|| (state[2][0] == state[0][2] && state[2][0] == getPlayerNum());
}
return canWin;
}
/**
* Determine if the bot thinks that a move will give block three in a row for
* the opponent
*
* @param move the move the bot is considering
* @return true iff this move would block opponent from winning
*/
public boolean canBlockWithMove(Move move) {
// Check if available
if (!isAvailable(move))
return false;
// Check row
boolean canWin = true;
for (int i = 1; i < 3 && canWin; i++) {
int row = (move.getRow() + i) % 3;
if (state[row][move.getColumn()] != getPlayerNum()) {
canWin = false;
}
}
if (canWin)
return true;
// Check column
canWin = true;
for (int i = 1; i < 3 && canWin; i++) {
int col = (move.getColumn() + i) % 3;
if (state[move.getRow()][col] != getPlayerNum()) {
canWin = false;
}
}
if (canWin)
return true;
// Check diagonals
canWin = false;
if (move.getRow() == 0 && move.getColumn() == 0) {
canWin |= state[1][1] == state[2][2] && state[1][1] == getPlayerNum();
}
if (move.getRow() == 2 && move.getColumn() == 2) {
canWin |= state[0][0] == state[1][1] && state[1][1] == getPlayerNum();
}
if (move.getRow() == 0 && move.getColumn() == 2) {
canWin |= state[2][0] == state[1][1] && state[1][1] == getPlayerNum();
}
if (move.getRow() == 2 && move.getColumn() == 0) {
canWin |= state[0][2] == state[1][1] && state[1][1] == getPlayerNum();
}
if (move.getRow() == 1 && move.getColumn() == 1) {
canWin |= (state[0][0] == state[2][2] && state[0][0] == getPlayerNum())
|| (state[2][0] == state[0][2] && state[2][0] == getPlayerNum());
}
return canWin;
}
/**
* Count the number of marked symbols in the same row, column, or diagonal as a
* move
*
* @param move the move the bot is considering
* @return a count of the number of X's or O's in the same row, column or
* diagonal as this move
*/
public int countColinearPieces(Move move) {
// Check if available
if (!isAvailable(move))
return 0;
// Check row
int count = 0;
for (int i = 1; i < 3; i++) {
int row = (move.getRow() + i) % 3;
if (state[row][move.getColumn()] != 0) {
count++;
}
}
// Check column
for (int i = 1; i < 3; i++) {
int col = (move.getColumn() + i) % 3;
if (state[move.getRow()][col] != 0) {
count++;
}
}
// Check diagonals
if (move.getRow() == 0 && move.getColumn() == 0) {
if (state[1][1] != 0)
count++;
if (state[2][2] != 0)
count++;
}
if (move.getRow() == 2 && move.getColumn() == 2) {
if (state[1][1] != 0)
count++;
if (state[0][0] != 0)
count++;
}
if (move.getRow() == 0 && move.getColumn() == 2) {
if (state[1][1] != 0)
count++;
if (state[2][0] != 0)
count++;
}
if (move.getRow() == 2 && move.getColumn() == 0) {
if (state[1][1] != 0)
count++;
if (state[0][2] != 0)
count++;
}
if (move.getRow() == 1 && move.getColumn() == 1) {
if (state[2][0] != 0)
count++;
if (state[0][2] != 0)
count++;
if (state[0][0] != 0)
count++;
if (state[2][2] != 0)
count++;
}
return count;
}
/**
* Record the result of the last move in the state array
*
* @param lastMove the last move made by the human
*/
public void updateState(Move lastMove) {
state[lastMove.getRow()][lastMove.getColumn()] = lastMove.getResult();
moveNumber++;
}
/**
* Reset the state of the bot so that it can play a new game
*
* @param playerNum it's player number in the new game
*/
public void reset(int playerNum) {
this.playerNum = playerNum;
moveNumber = 0;
state = new int[3][3];
}
/**
* Get the current move number. Before any moves have been made, this will
* return 0. After the first move has been made, it returns 1. After the second,
* 2, and so on.
*
* @return the current move number
*/
public int getMoveNumber() {
return moveNumber;
}
/**
* Get the symbol in a particular square according to the bot's view of the game
*
* @param move the square in question.
*
* @return a 0 if the square is empty, otherwise the player number that controls
* the square.
*/
public int getState(Move move) {
return state[move.getRow()][move.getColumn()];
}
/**
* @return the player number (either 1 or 2)
*/
public int getPlayerNum() {
return playerNum;
}
/**
* Update the appropriate count based on whether a game ended in a win, tie, or
* loss.
*
* @param winner the player number who won, or 0 if it was a tie.
*/
public void recordGameResult(int winner) {
if (winner == 0)
ties++;
else if (winner == getPlayerNum())
wins++;
else
losses++;
}
/**
* Get the winning percentage of a player. Note that ties count as half a win.
*
* @return the winning percentage.
*/
public double getWinPercent() {
return (wins + 0.5 * ties) / (wins + losses + ties);
}
/**
* Use the state array and System.out to display the state of the board as known
* to the bot player.
*
* You should start by indicating whether the bo is X's or O's. Then display a
* board with X's, O's, and ?'s, where '?' is used for squares that are unknown.
*/
public void printState() {
System.out.println("Bot is " + (playerNum == 1 ? "X" : "O") + "'s");
for (int r = 0; r < state.length; r++) {
for (int c = 0; c < state[r].length; c++) {
switch (state[r][c]) {
case 0:
System.out.print(" ?");
break;
case 1:
System.out.print(" X");
break;
case 2:
System.out.print(" O");
break;
}
}
System.out.println();
}
}
}