-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompPlayer.java
563 lines (468 loc) · 14.1 KB
/
CompPlayer.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
import java.util.ArrayList;
/**
* Computer player, designed to win Othello Game. Impelments Player interface
*
* @author Chae Jubb
* @version 1.1
*/
public class CompPlayer implements Player {
private int identifier;
private Board board, simBoard, simBoard2;
public CompPlayer(int ident) {
this.identifier = ident;
this.board = new Board(Game.SIZE, Game.SIZE);
this.board.setUpBoard();
this.simBoard = Board.copy(this.board);
this.simBoard2 = Board.copy(this.simBoard2);
}
/**
* Algorithm used to place next piece. Note, this method (hopefully) doesn't
* use the retry param as the validity of the move is internally checked
*
* @param retry
* Is this a second (or more) attempt for a given turn?
* @return Location of algorithmically determined best move
*/
@Override
public Location placeTile(boolean retry) {
// System.out.println("Comp Board \n" + this.board.toString());
System.out.println("Thinking...");
// Opening moves first
if (this.getBoardCount() == 4) {
boolean end = false;
// repeat until valid move
while (!end) {
int random = (int) (4 * Math.random() + 2);
int random2 = (int) (4 * Math.random() + 2);
Location attempt = new Tile(random, random2);
if (this.isValid(attempt, this.identifier)) {
this.executeMove(this.board, attempt, this.identifier, 0);
return attempt;
}
}
} else if (this.getBoardCount() == 5) {
// find where only own tile located
Location onlyTile = null;
Location attempt;
for (int i = 0; i < Game.SIZE; i++) {
for (int j = 0; j < Game.SIZE; j++) {
if (this.board.getTile(i, j).getColor() == this.identifier) {
onlyTile = new Tile(i, j);
}
}
}
// choose one of two particular openings: diagonal or perpendicular
for (int i = -2; i <= 2; i += 2) {
for (int j = -2; j <= 2; j += 2) {
attempt = new Tile(onlyTile.getHorizontal() + i,
onlyTile.getVertical() + j);
if (this.isValid(attempt, this.identifier)) {
if (this.oppAround(attempt) == 2) {
this.executeMove(this.board, attempt,
this.identifier, 0);
return attempt;
}
}
}
}
// general play instructions
} else if ((this.getBoardCount() >= 6) || (this.getBoardCount() <= 62)) {
Location attempt, tester;
ArrayList<Location> maxStableMove = new ArrayList<Location>();
int stableCount = 0;
int maxStableCount = 0;
// find moves that give maximum number of stable pieces
for (int i = 0; i < Game.SIZE; i++) {
for (int j = 0; j < Game.SIZE; j++) {
attempt = new Tile(i, j);
if (this.isValid(attempt, this.identifier)) {
this.simBoard2 = Board.copy(this.board);
this.executeMove(this.simBoard2, attempt,
this.identifier, 0);
// check if board combination wins the board for the
// player
if (this.isWinner(this.simBoard2, this.identifier)) {
return attempt;
}
stableCount = 0;
for (int k = 0; k < Game.SIZE; k++) {
for (int l = 0; l < Game.SIZE; l++) {
tester = new Tile(k, l);
if (this.isValid(tester, this.identifier)) {
if (this.isStable(this.simBoard2, tester,
this.identifier)) {
stableCount++;
}
}
}
}
if (stableCount > maxStableCount) {
;
maxStableCount = stableCount;
maxStableMove.clear();
maxStableMove.add(attempt);
} else if (stableCount == maxStableCount) {
maxStableCount = stableCount;
maxStableMove.add(attempt);
}
}
}
}
Location response = null;
if (maxStableMove.size() == 1) {
response = maxStableMove.get(0);
this.executeMove(this.board, response, this.identifier, 0);
return response;
} else {
// return a corner if exists
int maxCount = 0;
int count;
Location test;
for (int i = 0; i < maxStableMove.size(); i++) {
int hor = maxStableMove.get(i).getHorizontal();
int vert = maxStableMove.get(i).getVertical();
test = new Tile(hor, vert);
if ((hor == 0 && vert == 0) || (hor == 0 && vert == 7)
|| (hor == 7 && vert == 0)
|| (hor == 7 && vert == 7)) {
count = this.executeMove(this.board, test,
this.identifier, 1);
if (count > maxCount) {
response = test;
}
}
}
}
// continue if no response so far
if (response == null) {
// next look for an edge
int maxCount = 0;
int count;
Location test;
for (int i = 0; i < maxStableMove.size(); i++) {
int hor = maxStableMove.get(i).getHorizontal();
int vert = maxStableMove.get(i).getVertical();
test = new Tile(hor, vert);
// looks for edge, not directly next to corner
if ((((hor == 0) || (hor == 7)) && !((vert == 1) || (vert == 6)))
|| (((vert == 0) || (vert == 7)) && !((hor == 1) || (hor == 6)))) {
count = this.executeMove(this.board, test,
this.identifier, 1);
if (count > maxCount) {
response = test;
}
}
}
}
// pick next "best" moves. Eliminate points diagonal from corners.
// BAD PLACES
ArrayList<Location> transferForward = new ArrayList<Location>();
if (response == null) {
int count;
Location test;
int bestCount;
if (this.getBoardCount() <= 40) {
bestCount = 10000;
} else {
bestCount = 0;
}
for (int i = 0; i < maxStableMove.size(); i++) {
int hor = maxStableMove.get(i).getHorizontal();
int vert = maxStableMove.get(i).getVertical();
if (((hor == 1) && (vert == 1))
|| ((hor == 6) && (vert == 6))
|| ((hor == 1) && (vert == 6))
|| ((hor == 6) && (vert == 1))) {
transferForward.add(maxStableMove.remove(i));
} else {
test = new Tile(hor, vert);
count = this.executeMove(this.board, test,
this.identifier, 1);
if (this.getBoardCount() <= 40) {
if (count < bestCount) {
bestCount = count;
response = test;
}
} else {
if (count > bestCount) {
bestCount = count;
response = test;
}
}
}
}
}
// use X-corners as last resort
if (transferForward.size() != 0) {
int maxCount = 0;
int count = 0;
Location test;
for (int i = 0; i < transferForward.size(); i++) {
test = transferForward.get(i);
count = this.executeMove(this.board, test, this.identifier,
1);
if (count > maxCount) {
response = test;
}
}
}
if (response != null) {
this.executeMove(this.board, response, this.identifier, 0);
return response;
} else {
// if something above breaks, return random valid placement
System.out.println("E-1");
boolean end = false;
while (!end) {
int hor = (int) (8 * Math.random());
int vert = (int) (8 * Math.random());
Location test = new Tile(hor, vert);
if (this.isValid(test, this.identifier)) {
end = true;
this.executeMove(this.board, test, this.identifier, 0);
return test;
}
}
}
}
return null; // needed for error checking. Should never reach this
// point, theoretically
}
/**
* Updates own board to reflect opponent's most previous move
*
* @param move
* Location of opponent's most previous move
*/
@Override
public void setResult(Location move) {
if (move != null) {
this.executeMove(this.board, move, (this.identifier % 2) + 1, 0);
}
}
/**
* Checks if attempted move location is valid
*
* @param attempt
* Location to be checked for valid move
* @return true if location is valid; false otherwise
*/
private boolean isValid(Location attempt, int identifier) {
int hor = attempt.getHorizontal();
int vert = attempt.getVertical();
// check if piece already exists
if (this.board.getTile(hor, vert).getColor() != 0) {
return false;
}
// check that new piece flips old piece of other color
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (((hor + i) < Game.SIZE) && ((hor + i) >= 0)
&& ((vert + j) < Game.SIZE) && ((vert + j) >= 0)) {
// check if one of paths has other color
if (this.board.getTile(hor + i, vert + j).getColor() == (identifier % 2 + 1)) {
int k = 2;
boolean found = false;
boolean end = false;
// move in all 8 directions out from attempted piece
// placement
while (k < Game.SIZE && !found && !end) {
// check next position to be checked in on the board
if (((hor + k * i) < Game.SIZE)
&& ((hor + k * i) >= 0)
&& ((vert + k * j) < Game.SIZE)
&& ((vert + k * j) >= 0)) {
// check for piece of own color
if (this.board.getTile(hor + k * i,
vert + k * j).getColor() == identifier) {
found = true;
return true;
} else if (this.board.getTile(hor + k * i,
vert + k * j).getColor() == 0) {
end = true;
}
}
k++;
}
}
}
}
}
// if above conditions not met, return invalid
return false;
}
/**
* Executes supplied move by supplied player on this.board
*
* @param attempt
* Location of move
* @param identifier
* Identifier for player making move
* @param testCode
* 0 = execute; 1 = return count, DO NOT execute
* @return Number of opponents chips that would be flipped. 0 for actual
* execution; non-zero for simulation
*/
private int executeMove(Board board, Location attempt, int identifier,
int testCode) {
int hor = attempt.getHorizontal();
int vert = attempt.getVertical();
int oppID = (identifier % 2) + 1;
int count = 0;
int oldColor = board.getTile(hor, vert).getColor();
board.getTile(hor, vert).setColor(identifier);
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
// make sure direction is on board
if (((hor + i) < Game.SIZE) && ((hor + i) >= 0)
&& ((vert + j) < Game.SIZE) && ((vert + j) >= 0)) {
// check if one of paths has other color
if (board.getTile(hor + i, vert + j).getColor() == oppID) {
int k = 2;
boolean found = false;
boolean end = false;
// move in all 8 directions out from attempted piece
// placement
while ((k < Game.SIZE) && (!found) && (!end)) {
// check next position to be checked in on the board
if (((hor + k * i) < Game.SIZE)
&& ((hor + k * i) >= 0)
&& ((vert + k * j) < Game.SIZE)
&& ((vert + k * j) >= 0)) {
// check for piece of own color
if (board.getTile(hor + k * i, vert + k * j)
.getColor() == identifier) {
// entering this portion of the loop means
// pieces are being "flipped"
for (int l = 1; l < k; l++) {
if (testCode == 0) {
board.getTile(hor + l * i,
vert + l * j).setColor(
identifier);
} else if (testCode == 1) {
count++;
}
}
found = true;
} else if (this.board.getTile(hor + k * i,
vert + k * j).getColor() == 0) {
end = true;
}
}
k++;
}
}
}
}
}
if (testCode == 1) {
board.getTile(hor, vert).setColor(oldColor);
}
return count;
}
/**
* Simple accessor method for number of pieces placed
*
* @return Integer count of number of pieces played on board
*/
private int getBoardCount() {
int count = 0;
int color = 0;
for (int i = 0; i < Game.SIZE; i++) {
for (int j = 0; j < Game.SIZE; j++) {
color = this.board.getTile(i, j).getColor();
if ((color == 1) || (color == 2)) {
count++;
}
}
}
return count;
}
/**
*
* @return count of opponent pieces around provided tile
*/
private int oppAround(Location move) {
int count = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (this.board.getTile(move.getHorizontal() + i,
move.getVertical() + j).getColor() != 0) {
count++;
}
}
}
return count;
}
/**
* Checks if position is stable. move need not already be occupied by
* identifier
*
* @param move
* Location to piece to check if stable
* @param identifier
* Check if stable for player denoted by identifier
* @return true if stable for identifier; false otherwise
*/
private boolean isStable(Board board, Location move, int identifier) {
// create copy of board to sim
// this.simBoard = Board.copy(board);
int oppID = (identifier % 2) + 1;
Location attempt;
// this.simBoard.getTile(move.getHorizontal(),
// move.getVertical()).setColor(identifier);
for (int i = 0; i < Game.SIZE; i++) {
for (int j = 0; j < Game.SIZE; j++) {
if (this.simBoard.getTile(i, j).getColor() == 0) {
// this.simBoard.getTile(i, j).setColor(oppID);
for (int k = 0; k < Game.SIZE; k++) {
for (int l = 0; l < Game.SIZE; l++) {
this.simBoard = Board.copy(board);
this.simBoard.getTile(move.getHorizontal(),
move.getVertical()).setColor(identifier);
this.simBoard.getTile(i, j).setColor(oppID);
attempt = new Tile(k, l);
if (this.isValid(attempt, oppID)) {
this.executeMove(simBoard, attempt, oppID, 0);
if (this.simBoard.getTile(move.getHorizontal(),
move.getVertical()).getColor() != identifier) {
return false;
}
}
}
}
}
}
}
return true;
}
/**
* Checks if given player has won the board
*
* @param board
* Board for winning condition to be checked on
* @param identifier
* Identifier for player to check for winning condition
* @return true if player specified wins on specified board; false otherwise
*/
private boolean isWinner(Board board, int identifier) {
int oppID = (identifier % 2) + 1;
for (int i = 0; i < Game.SIZE; i++) {
for (int j = 0; j < Game.SIZE; j++) {
if (board.getTile(i, j).getColor() == oppID) {
return false;
}
}
}
// only returns true if no opponent piece on the board
return true;
}
/**
* Getter method for identifier of player
*
* @return Integer identifier
*/
public int getIdentifier() {
return this.identifier;
}
}