-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.java
631 lines (561 loc) · 19.2 KB
/
Board.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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/* Skeleton Copyright (C) 2015, 2020 Paul N. Hilfinger and the Regents of the
* University of California. All rights reserved.
* @author Heming Wu
*/
package loa;
import java.util.ArrayList;
import java.util.Stack;
import java.util.List;
import java.util.Formatter;
import java.util.Set;
import java.util.HashSet;
import java.util.Arrays;
import java.util.Collections;
import java.util.regex.Pattern;
import static loa.Piece.*;
import static loa.Square.*;
/** Represents the state of a game of Lines of Action.
* @author Heming Wu
*/
class Board {
/** Default number of moves for each side that results in a draw. */
static final int DEFAULT_MOVE_LIMIT = 60;
/** Pattern describing a valid square designator (cr). */
static final Pattern ROW_COL = Pattern.compile("^[a-h][1-8]$");
/** A Board whose initial contents are taken from INITIALCONTENTS
* and in which the player playing TURN is to move. The resulting
* Board has
* get(col, row) == INITIALCONTENTS[row][col]
* Assumes that PLAYER is not null and INITIALCONTENTS is 8x8.
*
* CAUTION: The natural written notation for arrays initializers puts
* the BOTTOM row of INITIALCONTENTS at the top.
*/
Board(Piece[][] initialContents, Piece turn) {
initialize(initialContents, turn);
}
/** A new board in the standard initial position. */
Board() {
this(INITIAL_PIECES, BP);
}
/** A Board whose initial contents and state are copied from
* BOARD. */
Board(Board board) {
this();
copyFrom(board);
}
/** Set my state to CONTENTS with SIDE to move. */
void initialize(Piece[][] contents, Piece side) {
for (int r = 0; r < BOARD_SIZE; r++) {
for (int c = 0; c < BOARD_SIZE; c++) {
Piece p = contents[r][c];
Square s = Square.sq(c, r);
for (int i = 0; i < _board.length; i++) {
_board[s.index()] = p;
}
}
}
_winnerKnown = false;
_turn = side;
_moveLimit = DEFAULT_MOVE_LIMIT;
}
/** Set me to the initial configuration. */
void clear() {
initialize(INITIAL_PIECES, BP);
}
/** Set my state to a copy of BOARD.
* Use deep copy, don't shallow copy it.
* */
void copyFrom(Board board) {
if (board == this) {
return;
}
for (int i = 0; i < _board.length; i++) {
_board[i] = board._board[i];
}
_winnerKnown = board._winnerKnown;
_turn = board._turn;
_moveLimit = board._moveLimit;
}
/** Return the contents of the square at SQ. */
Piece get(Square sq) {
return _board[sq.index()];
}
/** Set the square at SQ to V and set the side that is to move next
* to NEXT, if NEXT is not null. */
void set(Square sq, Piece v, Piece next) {
if (next != null) {
_turn = next;
}
_board[sq.index()] = v;
}
/** Set the square at SQ to V, without modifying the side that
* moves next. */
void set(Square sq, Piece v) {
set(sq, v, null);
}
/** Set limit on number of moves by each side that results in a tie to
* LIMIT, where 2 * LIMIT > movesMade(). */
void setMoveLimit(int limit) {
if (2 * limit <= movesMade()) {
throw new IllegalArgumentException("move limit too small");
}
_moveLimit = 2 * limit;
}
/** Copy a board.
* @param b copy from this board
* @return a new board
*/
Piece[] copyBoard(Piece[] b) {
Piece[] newBoard = new Piece[b.length];
for (int r = 0; r < BOARD_SIZE; r++) {
for (int c = 0; c < BOARD_SIZE; c++) {
Square s = Square.sq(c, r);
for (int i = 0; i < _board.length; i++) {
newBoard[s.index()] = b[s.index()];
}
}
}
return newBoard;
}
/** Assuming isLegal(MOVE), make MOVE. This function assumes that
* MOVE.isCapture() will return false. If it saves the move for
* later retraction, makeMove itself uses MOVE.captureMove() to produce
* the capturing move. */
void makeMove(Move move) {
assert isLegal(move);
try {
_subsetsInitialized = false;
_snapShot.push(copyBoard(_board));
frSq = move.getFrom();
frP = _board[frSq.index()];
toSq = move.getTo();
toP = _board[toSq.index()];
capOrNot = (toP != EMP) && (toP == frP.opposite());
if (capOrNot) {
move = move.captureMove();
}
_mymoves.push(move);
_moves.add(move);
_board[toSq.index()] = frP;
_board[frSq.index()] = EMP;
_turn = frP.opposite();
} catch (AssertionError er) {
Utils.error("Illegal Move");
}
}
/** Undo moves. */
void undo() {
try {
Piece[] prev = _snapShot.pop();
_board = prev;
_winnerKnown = false;
_moves.remove(_mymoves.pop());
_turn = _turn.opposite();
_subsetsInitialized = false;
} catch (AssertionError er) {
Utils.error("No moves to undo");
}
}
/** Retract (unmake) one move, returning to the state immediately before
* that move. Requires that movesMade () > 0. */
void retract() {
try {
assert movesMade() > 0;
_board[frSq.index()] = frP;
_board[toSq.index()] = toP;
_moves.remove(_mymoves.pop());
_turn = frP;
_subsetsInitialized = false;
} catch (AssertionError er) {
Utils.error("No moves to retract");
}
}
/** Return the Piece representing who is next to move. */
Piece turn() {
return _turn;
}
/** Return true iff FROM - TO is a legal move for the player currently on
* move. */
boolean isLegal(Square from, Square to) {
Piece fp = _board[from.index()];
Piece tp = _board[to.index()];
int dis = from.distance(to);
int dir = from.direction(to);
if (fp == tp
|| fp == EMP
|| fp != _turn
|| !from.isValidMove(to)
|| blocked(from, to)
|| countAlone(from, dir) != dis) {
return false;
}
return true;
}
/** Count the number of pieces alone the line of direction from sq.
* @param sq The square it's counting from
* @param dir direction.
* @return number of squares alone the line of direction.
* */
int countAlone(Square sq, int dir) {
int count = 0;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
Square canSq = Square.sq(i, j);
if (sq.sameLine(canSq, dir)) {
if (_board[canSq.index()] != EMP) {
count += 1;
}
}
}
}
return count;
}
/** Return true iff MOVE is legal for the player currently on move.
* The isCapture() property is ignored. */
boolean isLegal(Move move) {
if (move == null) {
return false;
}
return isLegal(move.getFrom(), move.getTo());
}
/** Return a sequence of all _turn's legal moves from this position. */
@SuppressWarnings("unchecked")
List<Move> legalMoves() {
_allLegal.clear();
_eachLegal = new ArrayList[BOARD_SIZE * BOARD_SIZE];
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (_board[Square.sq(i, j).index()] == _turn) {
Square sq = Square.sq(i, j);
_eachLegal[sq.index()] = new ArrayList<Move>();
for (int dir = 0; dir < 8; dir++) {
for (int step = 1; step < BOARD_SIZE; step++) {
Square canSq = sq.moveDest(dir, step);
Move m = Move.mv(sq, canSq);
if (canSq != null
&& isLegal(m)) {
_allLegal.add(m);
_eachLegal[sq.index()].add(m);
}
}
}
}
}
}
return _allLegal;
}
/** Map of each leagal moves.
* @return each legal move.
* */
public ArrayList<Move>[] eachLegalMove() {
legalMoves();
return _eachLegal;
}
/** Return true iff the game is over (either player has all his
* pieces continguous or there is a tie). */
boolean gameOver() {
return winner() != null;
}
/** Return true iff SIDE's pieces are contiguous. */
boolean piecesContiguous(Piece side) {
return getRegionSizes(side).size() == 1;
}
/** Return the winning side, if any. If the game is not over, result is
* null. If the game has ended in a tie, returns EMP. */
Piece winner() {
if (!_winnerKnown) {
if (movesMade() >= _moveLimit) {
_winner = EMP;
_winnerKnown = true;
}
if (getRegionSizes(_turn.opposite()).size() == 1) {
_winner = _turn.opposite();
_winnerKnown = true;
} else if (getRegionSizes(_turn).size() == 1) {
_winner = _turn;
_winnerKnown = true;
}
}
return _winner;
}
/** Return the total number of moves that have been made (and not
* retracted). Each valid call to makeMove with a normal move increases
* this number by 1. */
int movesMade() {
return _moves.size();
}
@Override
public boolean equals(Object obj) {
Board b = (Board) obj;
return Arrays.deepEquals(_board, b._board) && _turn == b._turn;
}
@Override
public int hashCode() {
return Arrays.deepHashCode(_board) * 2 + _turn.hashCode();
}
@Override
public String toString() {
Formatter out = new Formatter();
out.format("===%n");
for (int r = BOARD_SIZE - 1; r >= 0; r -= 1) {
out.format(" ");
for (int c = 0; c < BOARD_SIZE; c += 1) {
out.format("%s ", get(sq(c, r)).abbrev());
}
out.format("%n");
}
out.format("Next move: %s%n===", turn().fullName());
return out.toString();
}
/** Return true if a move from FROM to TO is blocked by an opposing
* piece or by a friendly piece on the target square. */
private boolean blocked(Square from, Square to) {
Piece fp = _board[from.index()];
Piece tp = _board[to.index()];
int dir = from.direction(to);
if ((fp == tp) && (fp != EMP)) {
return true;
}
for (int i = 1; i < from.distance(to); i++) {
Square s = from.moveDest(dir, i);
if (s != null && _board[s.index()] == fp.opposite()) {
return true;
}
}
return false;
}
/** Return how many moves are blocked for p in current state.
* @param p piece of interest
* @return number of moves blocked.
* */
int countBlocked(Piece p) {
int count = 0;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
Square sq = sq(i, j);
if (_board[sq.index()] == p) {
for (int dir = 0; dir < 8; dir++) {
int step = countAlone(sq, dir);
Square to = sq.moveDest(dir, step);
if (to != null) {
if (blocked(sq, to)) {
count++;
}
}
}
}
}
}
return count;
}
/** Count number of piece p in current board.
* @param p piece of interest.
* @return how many pieces are there for p.
*/
int countPiece(Piece p) {
int count = 0;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
Square sq = sq(i, j);
for (int k = 0; k < BOARD_SIZE; k++) {
if (_board[sq.index()] == p) {
count++;
}
}
}
}
return count;
}
/** Return the size of the as-yet unvisited cluster of squares
* containing P at and adjacent to SQ. VISITED indicates squares that
* have already been processed or are in different clusters. Update
* VISITED to reflect squares counted. */
int numContig(Square sq, boolean[][] visited, Piece p) {
if (_board[sq.index()] == p.opposite()
|| _board[sq.index()] == EMP
|| visited[sq.col()][sq.row()]) {
return 0;
}
int count = 1;
visited[sq.col()][sq.row()] = true;
Square[] adj = sq.adjacent();
for (Square adSq : adj) {
count = count + numContig(adSq, visited, p);
}
return count;
}
/** Get a new indicator for numContig method.
* @return a new indicator.
* */
private boolean[][] newInd() {
return new boolean[BOARD_SIZE][BOARD_SIZE];
}
/** Set the values of _whiteRegionSizes and _blackRegionSizes. */
private void computeRegions() {
if (_subsetsInitialized) {
return;
}
_whiteRegionSizes.clear();
_blackRegionSizes.clear();
_hatedList.clear();
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
Square sq = Square.sq(i, j);
Piece p = _board[sq.index()];
if (p == EMP) {
continue;
}
boolean[][] ind = newInd();
int regSize = numContig(sq, ind, p);
if (p == BP && !_hatedList.contains(sq)) {
_blackRegionSizes.add(regSize);
}
if (p == WP && !_hatedList.contains(sq)) {
_whiteRegionSizes.add(regSize);
}
for (int c = 0; c < ind.length; c++) {
for (int r = 0; r < ind.length; r++) {
if (ind[c][r]) {
_hatedList.add(sq(c, r));
}
}
}
}
}
Collections.sort(_whiteRegionSizes, Collections.reverseOrder());
Collections.sort(_blackRegionSizes, Collections.reverseOrder());
_subsetsInitialized = true;
}
/** Return adjacent allies of sq. If it's EMP, return an empty array.
* @param sq sqaure of interest.
* @return An array of square on which lands the same piece on sq.
*/
Object[] adjAllies(Square sq) {
Square[] adj = sq.adjacent();
ArrayList<Square> result = new ArrayList<>();
if (_board[sq.index()] == EMP) {
return result.toArray();
}
for (int i = 0; i < adj.length; i++) {
Square can = adj[i];
if (_board[can.index()] == _board[sq.index()]) {
result.add(can);
}
}
return result.toArray();
}
/** Return the sizes of all the regions in the current union-find
* structure for side S. */
List<Integer> getRegionSizes(Piece s) {
computeRegions();
if (s == WP) {
return _whiteRegionSizes;
} else {
return _blackRegionSizes;
}
}
/** Snapshot of board states. */
private Stack<Piece[]> _snapShot = new Stack<>();
/** Get the board.
* @return board
* */
public Piece[] getBoard() {
return _board;
}
/** Get the moves.
* @return _moves
* */
public ArrayList<Move> getMoves() {
return _moves;
}
/** Get map of legal moves for each square that
* has a piece on it.
* @return _eachLegal
*/
public ArrayList<Move>[] getEachLegal() {
return _eachLegal;
}
/** Return true if it's a capture move. */
private Boolean capOrNot;
/** get toSq.
* @return toSq
*/
public Square getToSq() {
return toSq;
}
/** To Square when making a move. */
private Square toSq;
/** Get to piece.
* @return toP
* */
public Piece getToP() {
return toP;
}
/** The piece at ToSq (could be EMP).
* */
private Piece toP;
/** Get from square.
* @return frSq
* */
public Square getFrSq() {
return frSq;
}
/** From Square when making a move. */
private Square frSq;
/** Get frp.
* @return frP
* */
public Piece getFrP() {
return frP;
}
/** The piece at frSq (could be EMP). */
private Piece frP;
/** All legal moves in this board for current turn. */
private List<Move> _allLegal = new ArrayList<>();
/** All legal moves for each square position. If there aren't
* any legal moves from that square, the corresponding ArrayList
* is empty (not null).
* Square S is at _eachLegal[S.index()]. */
@SuppressWarnings("unchecked")
private ArrayList<Move>[] _eachLegal
= new ArrayList[BOARD_SIZE * BOARD_SIZE];
/** Set of squares that I don't want to compute region on.
* Just a helper global variable for computing regions.
*/
private Set<Square> _hatedList = new HashSet<>();
/** The standard initial configuration for Lines of Action (bottom row
* first). */
static final Piece[][] INITIAL_PIECES = {
{ EMP, BP, BP, BP, BP, BP, BP, EMP },
{ WP, EMP, EMP, EMP, EMP, EMP, EMP, WP },
{ WP, EMP, EMP, EMP, EMP, EMP, EMP, WP },
{ WP, EMP, EMP, EMP, EMP, EMP, EMP, WP },
{ WP, EMP, EMP, EMP, EMP, EMP, EMP, WP },
{ WP, EMP, EMP, EMP, EMP, EMP, EMP, WP },
{ WP, EMP, EMP, EMP, EMP, EMP, EMP, WP },
{ EMP, BP, BP, BP, BP, BP, BP, EMP }
};
/** Current contents of the board. Square S is at _board[S.index()]. */
private Piece[] _board = new Piece[BOARD_SIZE * BOARD_SIZE];
/** List of all unretracted moves on this board, in order. */
private ArrayList<Move> _moves = new ArrayList<>();
/** Also a stack of all unretracted moves, but it's easier to retract
* multiple moves. */
private final Stack<Move> _mymoves = new Stack<>();
/** Current side on move. */
private Piece _turn;
/** Limit on number of moves before tie is declared. */
private int _moveLimit;
/** True iff the value of _winner is known to be valid. */
private boolean _winnerKnown;
/** Cached value of the winner (BP, WP, EMP (for tie), or null (game still
* in progress). Use only if _winnerKnown. */
private Piece _winner;
/** True iff subsets computation is up-to-date. */
private boolean _subsetsInitialized;
/** List of the sizes of continguous clusters of pieces, by color. */
private final ArrayList<Integer>
_whiteRegionSizes = new ArrayList<>(),
_blackRegionSizes = new ArrayList<>();
}