-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
532 lines (453 loc) · 12.6 KB
/
Copy pathBoard.java
File metadata and controls
532 lines (453 loc) · 12.6 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
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
package amazons;
import java.util.Collections;
import java.util.Iterator;
import java.util.Stack;
import java.util.NoSuchElementException;
import static amazons.Piece.*;
/** The state of an Amazons Game.
* @author JaniceNg
*/
class Board {
/**
* Creates a 2D array for board.
*/
private Piece[][] board2d;
/**
* The number of squares on a side of the board.
*/
static final int SIZE = 10;
/**
* Initializes a game board with SIZE squares on a side in the
* initial position.
*/
Board() {
init();
}
/**
* Initializes a copy of MODEL.
*/
Board(Board model) {
copy(model);
}
/**
* Copies MODEL into me.
*/
void copy(Board model) {
this.board2d = new Piece[SIZE][SIZE];
for (int i = 0; i < SIZE; i += 1) {
for (int j = 0; j < SIZE; j += 1) {
this.board2d[i][j] = model.board2d[i][j];
}
}
this._turn = model._turn;
this._winner = model._winner;
}
/**
* Clears the board to the initial position.
*/
void init() {
board2d = new Piece[SIZE][SIZE];
for (int i = 0; i < SIZE; i += 1) {
for (int j = 0; j < SIZE; j += 1) {
board2d[i][j] = Piece.EMPTY;
}
}
for (int w : WHITELIST) {
int col = w % 10;
int row = w / 10;
board2d[col][row] = Piece.WHITE;
}
for (int bl : BLACKLIST) {
int col = bl % 10;
int row = bl / 10;
board2d[col][row] = Piece.BLACK;
}
_turn = WHITE;
_winner = null;
}
/**
* Return the Piece whose move it is (WHITE or BLACK).
*/
Piece turn() {
return _turn;
}
/**
* Return the number of moves (that have not been undone) for this
* board.
*/
int numMoves() {
return _moves;
}
/**
* Return the winner in the current position, or null if the game is
* not yet finished.
*/
Piece winner() {
Iterator<Move> iterator = legalMoves(_turn);
if (!iterator.hasNext()) {
_winner = _turn.opponent();
}
return _winner;
}
/**
* Return the contents the square at S.
*/
final Piece get(Square s) {
return board2d[s.col()][s.row()];
}
/**
* Return the contents of the square at (COL, ROW), where
* 0 <= COL, ROW < 9.
*/
/**
* Return the contents of the square at (COL, ROW), where
* 0 <= COL, ROW <= 9.
*/
final Piece get(int col, int row) {
return board2d[col][row];
}
/**
* Return the contents of the square at COL ROW.
*/
final Piece get(char col, char row) {
return get(col - 'a', row - '1');
}
/**
* Set square S to P.
*/
final void put(Piece p, Square s) {
board2d[s.col()][s.row()] = p;
}
/**
* Set square (COL, ROW) to P.
*/
final void put(Piece p, int col, int row) {
board2d[col][row] = p;
_winner = null;
}
/**
* Set square COL ROW to P.
*/
final void put(Piece p, char col, char row) {
put(p, col - 'a', row - '1');
}
/**
* Return true iff FROM - TO is an unblocked queen move on the current
* board, ignoring the contents of ASEMPTY, if it is encountered.
* For this to be true, FROM-TO must be a queen move and the
* squares along it, other than FROM and ASEMPTY, must be
* empty. ASEMPTY may be null, in which case it has no effect.
*/
boolean isUnblockedMove(Square from, Square to, Square asEmpty) {
if (!from.isQueenMove(to)) {
throw new NoSuchElementException("Not a move");
}
if (!get(to).equals(EMPTY) && to != asEmpty || to == null) {
return false;
} else {
int direction = from.direction(to);
Square next = from.nextsquare(direction, 1, 1);
while (next != to) {
if (!get(next).equals(EMPTY) && next != asEmpty) {
return false;
} else {
next = next.nextsquare(direction, 1, 1);
}
}
}
return true;
}
/**
* Return true iff FROM is a valid starting square for a move.
*/
boolean isLegal(Square from) {
if (from.equals(WHITE)) {
return turn().equals(WHITE);
}
if (from.equals(BLACK)) {
return turn().equals(BLACK);
}
return false;
}
/**
* Return true iff FROM-TO is a valid first part of move, ignoring
* spear throwing.
*/
boolean isLegal(Square from, Square to) {
return from.isQueenMove(to);
}
/**
* Return true iff FROM-TO(SPEAR) is a legal move in the current
* position.
*/
boolean isLegal(Square from, Square to, Square spear) {
return isLegal(from, to) && to.isQueenMove(spear);
}
/**
* Return true iff MOVE is a legal move in the current
* position.
*/
boolean isLegal(Move move) {
return isLegal(move.from(), move.to(), move.spear());
}
/**
* Move FROM-TO(SPEAR), assuming this is a legal move.
*/
void makeMove(Square from, Square to, Square spear) {
Piece curr = get(from);
put(EMPTY, from);
put(curr, to);
put(SPEAR, spear);
_moves += 1;
if (turn().equals(WHITE)) {
_turn = BLACK;
} else {
_turn = WHITE;
}
Move moves = Move.mv(from, to, spear);
_stack.push(moves);
}
/**
* Move according to MOVE, assuming it is a legal move.
*/
void makeMove(Move move) {
makeMove(move.from(), move.to(), move.spear());
}
/**
* Undo one move. Has no effect on the initial board.
*/
void undo() {
Move remove = _stack.pop();
put(Piece.EMPTY, remove.spear());
put(get(remove.to()), remove.from());
put(Piece.EMPTY, remove.to());
_moves -= 1;
_turn = _turn.opponent();
}
/**
* Return an Iterator over the Squares that are reachable by an
* unblocked queen move from FROM. Does not pay attention to what
* piece (if any) is on FROM, nor to whether the game is finished.
* Treats square ASEMPTY (if non-null) as if it were EMPTY. (This
* feature is useful when looking for Moves, because after moving a
* piece, one wants to treat the Square it came from as empty for
* purposes of spear throwing.)
*/
Iterator<Square> reachableFrom(Square from, Square asEmpty) {
return new ReachableFromIterator(from, asEmpty);
}
/**
* Return an Iterator over all legal moves on the current board.
*/
Iterator<Move> legalMoves() {
return new LegalMoveIterator(_turn);
}
/**
* Return an Iterator over all legal moves on the current board for
* SIDE (regardless of whose turn it is).
*/
Iterator<Move> legalMoves(Piece side) {
return new LegalMoveIterator(side);
}
/**
* An iterator used by reachableFrom.
*/
private class ReachableFromIterator implements Iterator<Square> {
/**
* Iterator of all squares reachable by queen move from FROM,
* treating ASEMPTY as empty.
*/
ReachableFromIterator(Square from, Square asEmpty) {
_from = from;
_dir = 0;
_steps = 0;
_asEmpty = asEmpty;
toNext();
}
@Override
public boolean hasNext() {
return _dir < 8;
}
@Override
public Square next() {
Square next = _from.queenMove(_dir, _steps);
toNext();
return next;
}
/**
* Advance _dir and _steps, so that the next valid Square is
* _steps steps in direction _dir from _from.
*/
private void toNext() {
if (_dir < 1) {
_dir = 0;
}
_steps += 1;
Square moveTo = _from.queenMove(_dir, _steps);
if (moveTo != null && isUnblockedMove(_from, moveTo, _asEmpty)) {
return;
} else {
_steps = 0;
_dir += 1;
if (hasNext()) {
toNext();
}
}
}
/**
* Starting square.
*/
private Square _from;
/**
* Current direction.
*/
private int _dir;
/**
* Current distance.
*/
private int _steps;
/**
* Square treated as empty.
*/
private Square _asEmpty;
}
/**
* An iterator used by legalMoves.
*/
private class LegalMoveIterator implements Iterator<Move> {
/**
* Current Spear.
*/
private Square _spear;
/**
* Square it will land at.
*/
private Square _to;
/**
* Starting square.
*/
private Square _squarestart;
/**
* Current Piece Color iterating through .
*/
private Piece _fromPiece;
/**
* Piece's next square position .
*/
private Square _nextSquare;
/**
* Starting Squares Iterator .
*/
private Iterator<Square> _startingSquares;
/**
* Moves from _start to consider, using Iterator .
*/
private Iterator<Square> _pieceMoves;
/**
* Spear throws from _piece to consider, using Iterator .
*/
private Iterator<Square> _spearThrows;
/**
* Current starting square.
*/
private Square _start;
/**
/**
* All legal moves for SIDE (WHITE or BLACK).
*/
LegalMoveIterator(Piece side) {
_startingSquares = Square.iterator();
_spearThrows = NO_SQUARES;
_pieceMoves = NO_SQUARES;
_fromPiece = side;
toNext();
}
@Override
public boolean hasNext() {
return _startingSquares.hasNext();
}
@Override
public Move next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
Move result = Move.mv(_start, _nextSquare, _spearThrows.next());
toNext();
return result;
}
/**
* Advance so that the next valid Move is
* _start-_nextSquare(sp), where sp is the next value of
* _spearThrows.
*/
private void toNext() {
if (!hasNext()) {
return;
}
if (_spearThrows.hasNext()) {
return;
}
while (!_pieceMoves.hasNext()) {
if (!hasNext()) {
return;
}
_start = _startingSquares.next();
while (get(_start) != _fromPiece) {
if (!hasNext()) {
return;
}
_start = _startingSquares.next();
}
_pieceMoves = reachableFrom(_start, null);
}
_nextSquare = _pieceMoves.next();
_spearThrows = reachableFrom(_nextSquare, _start);
}
}
@Override
public String toString() {
String newstring = "";
for (int i = SIZE - 1; i >= 0; i -= 1) {
newstring += " ";
for (int j = 0; j < SIZE; j += 1) {
if (j == SIZE - 1) {
newstring += board2d[j][i];
} else {
newstring += board2d[j][i] + " ";
}
}
newstring += "\n";
}
return newstring;
}
/**
* An empty iterator for initialization.
*/
private static final Iterator<Square> NO_SQUARES =
Collections.emptyIterator();
/**
* Piece whose turn it is (BLACK or WHITE).
*/
private Piece _turn;
/**
* Cached value of winner on this board, or EMPTY if it has not been
* computed.
*/
private Piece _winner;
/**
* Keep count of number of moves.
*/
private int _moves;
/**
* Stack to keep track of queen and spear moves.
*/
private Stack<Move> _stack = new Stack<>();
/**
* Initial white queens on Board.
*/
private static final int [] BLACKLIST = {60, 69, 93, 96};
/**
* Initial white queens on Board.
*/
private static final int [] WHITELIST = {3, 6, 30, 39};
}