-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
602 lines (502 loc) · 18.8 KB
/
Board.java
File metadata and controls
602 lines (502 loc) · 18.8 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
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
import actions.*;
import pieces.*;
import java.util.*;
/**
* Class to represent Shogi Japanese Chess board
*/
public class Board {
Piece[][] board;
final int BOARD_SIZE = 5;
// Movement fields and keeping track of game rules
private static final int MAX_MOVES = 400;
private int turn = 0;
private boolean illegalMove;
private boolean checkMated;
// Captures fields
private List<Piece> capturedByUpper, capturedByLower;
// Initializes board object
public Board() {
board = new Piece[BOARD_SIZE][BOARD_SIZE];
// UPPER player
board[0][4] = new Rook(true);
board[1][4] = new Bishop(true);
board[2][4] = new Silver(true);
board[3][4] = new Gold(true);
board[4][4] = new King(true);
board[4][3] = new Pawn(true);
// lower player
board[0][1] = new Pawn(false);
board[0][0] = new King(false);
board[1][0] = new Gold(false);
board[2][0] = new Silver(false);
board[3][0] = new Bishop(false);
board[4][0] = new Rook(false);
// Captures
capturedByUpper = new ArrayList<>();
capturedByLower = new ArrayList<>();
}
// Initialize board object for test file mode/test cases
public Board(Utils.TestCase tc) {
board = new Piece[BOARD_SIZE][BOARD_SIZE];
// Place pieces on the board
List<Utils.InitialPosition> placements = tc.initialPieces;
for (Utils.InitialPosition placement : placements) {
BoardPosition pos = new BoardPosition(placement.position);
Piece piece = this.makeNewPiece(placement.piece);
this.setAt(pos, piece);
}
// Captures lists
capturedByUpper = new ArrayList<>();
capturedByLower = new ArrayList<>();
// Add the captures from test cases
for (String cap : tc.upperCaptures) {
Piece p = this.makeNewPiece(cap);
if (p != null) {
capturedByUpper.add(p);
}
}
for (String cap : tc.lowerCaptures) {
Piece p = this.makeNewPiece(cap);
if (p != null) {
capturedByLower.add(p);
}
}
}
// Checks whether the current player is in check
public boolean inCheck() {
// Find the position of the king
BoardPosition kingLocation = new BoardPosition(-1, -1); // INVALID!
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
Piece piece = board[row][col];
if (piece instanceof King && piece.isUpper() == isUpperTurn()) {
kingLocation = new BoardPosition(row, col);
}
}
}
// Can enemies pieces reach the king? If so, in check!
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
// Look at each of their pieces
Piece piece = board[row][col];
if (piece != null && piece.isUpper() != isUpperTurn()) {
BoardPosition currentLocation = new BoardPosition(row, col);
// Can this enemy piece reach the king?
if (isValidPath(piece.getPath(currentLocation, kingLocation))) {
return true;
}
}
}
}
return false;
}
// Returns list of possible moves to get out of check
public List<Action> escapeCheck() {
List<Action> actions = new ArrayList<>();
// Try dropping each piece in different places
List<Piece> drops = (isUpperTurn()) ? capturedByUpper : capturedByLower;
for (Piece piece : drops) {
// Look at every possible position to drop it
for (int dropRow = 0; dropRow < BOARD_SIZE; dropRow++) {
for (int dropCol = 0; dropCol < BOARD_SIZE; dropCol++) {
BoardPosition dropLoc = new BoardPosition(dropRow, dropCol);
// Found a possible move
Drop possibleDrop = new Drop(piece.toString(), dropLoc.toString());
if (this.isValidDrop(possibleDrop)) {
// We can drop the piece
setAt(dropLoc, piece);
// See if it is still in check
if (!inCheck()) {
actions.add(possibleDrop);
}
// Finally move back the piece
setAt(dropLoc, null);
}
}
}
}
// Look at every piece on the board
for (int startRow = 0; startRow < BOARD_SIZE; startRow++) {
for (int startCol = 0; startCol < BOARD_SIZE; startCol++) {
// If there is no piece here or it is an enemy piece, then skip over it
BoardPosition startLoc = new BoardPosition(startRow, startCol);
Piece piece = getAt(startLoc);
if (piece == null || piece.isUpper() != isUpperTurn()) {
continue;
}
// Look at every possible position to move it
for (int endRow = 0; endRow < BOARD_SIZE; endRow++) {
for (int endCol = 0; endCol < BOARD_SIZE; endCol++) {
BoardPosition endLoc = new BoardPosition(endRow, endCol);
// Found a possible move,
Move possibleMove = new Move(startLoc.toString(), endLoc.toString());
if (this.isValidMove(possibleMove)) {
// Move the piece
Piece oldEnd = getAt(endLoc);
setAt(endLoc, piece);
setAt(startLoc, null);
// See if it is still in check
if (!inCheck()) {
actions.add(possibleMove);
}
// Then move back the piece
setAt(startLoc, piece);
setAt(endLoc, oldEnd);
}
}
}
}
}
if (actions.isEmpty() && !isIllegalMove()) {
checkMated = true;
}
return actions;
}
// Performs a move or a drop
public boolean makeAction(Action action) {
if (action == null) {
illegalMove = true;
return false;
}
boolean succeeded = false;
if (action instanceof Move) {
Move m = (Move) action;
succeeded = makeMove(m);
} else if (action instanceof Drop) {
Drop d = (Drop) action;
succeeded = makeDrop(d);
}
// Successfully performed a move, on to next turn
if (succeeded) {
turn++;
}
return succeeded;
}
// Performs a move, takes in a Move object
private boolean makeMove(Move m) {
BoardPosition startPosition = m.getStartPosition();
BoardPosition endPosition = m.getEndPosition();
// Take care of illegal cases
if (!this.isValidMove(m)) {
illegalMove = true;
return false;
}
// Handle capturing pieces
Piece capturedPiece = null;
List<Piece> captured = (isUpperTurn()) ? capturedByUpper : capturedByLower;
if (isOccupied(endPosition)) {
capturedPiece = this.getAt(endPosition).getOriginal();
capturedPiece.switchOwner();
captured.add(capturedPiece);
}
// Moving a piece to unoccupied square
Piece piece = this.getAt(startPosition);
this.setAt(startPosition, null);
if (m.isPromoteMove()) {
piece = piece.upgrade();
}
this.setAt(endPosition, piece);
// If you move into check, this is illegal
if (inCheck()) {
setAt(startPosition, piece);
setAt(endPosition, capturedPiece);
captured.remove(capturedPiece);
illegalMove = true;
return false;
}
return true;
}
// Performs a drop, takes in a drop object
private boolean makeDrop(Drop d) {
if (!isValidDrop(d)) {
illegalMove = true;
return false;
}
Piece dropped = null;
String piece = d.getPieceName();
int loc = 0;
List<Piece> captured = (isUpperTurn()) ? capturedByUpper : capturedByLower;
Iterator<Piece> iterator = captured.iterator();
while (iterator.hasNext()) {
Piece p = iterator.next();
if (p.toString().equalsIgnoreCase(piece)) { // check that dropping piece is in captured
dropped = p;
setAt(d.getPosition(), dropped);
iterator.remove();
break;
}
loc++;
}
// Cannot checkmate with Pawn
if (dropped instanceof Pawn) {
turn++;
if (this.inCheck()) {
List<Action> actions = escapeCheck();
if (actions.isEmpty()) {
// Undo the action
captured.add(loc, getAt(d.getPosition()));
setAt(d.getPosition(), null);
turn--;
illegalMove = true;
return false;
}
}
turn--;
}
return true;
}
// Returns whether or not move is valid
private boolean isValidMove(Move m) {
// Handle illegal moves
// 1. Move it off board
// 2. Moving nothing
// 3. Cannot move other player's piece
// 4. Try to capture own piece (moving to same spot)
// 5. Can it be promoted?
// 6. Is it in promotion zone?
// 7. Can piece move that way?
// 8. Was Pawn promoted once in promotion zone?
// 9. Is it reachable on the board (are there pieces in the way)?
BoardPosition startPosition = m.getStartPosition();
BoardPosition endPosition = m.getEndPosition();
// 1. Move it off board
if (!startPosition.isRowInRange(BOARD_SIZE) || !startPosition.isColInRange(BOARD_SIZE) ||
!endPosition.isRowInRange(BOARD_SIZE) || !endPosition.isColInRange(BOARD_SIZE)) {
return false;
}
// 2. Moving nothing
if (!this.isOccupied(startPosition)) {
return false;
}
// 3. Moving other player's piece
String player = this.getCurrentPlayer();
Piece piece = this.getAt(startPosition);
if (this.isUpperTurn() != piece.isUpper()) {
return false;
}
// 4. Try to capture own piece
if (this.isOccupied(endPosition) && this.isUpperTurn() == this.getAt(endPosition).isUpper()) {
return false;
}
// 5. Can the piece be promoted?
if (m.isPromoteMove() && !piece.canPromote()) {
return false;
}
// 6. Is it moving in, within or out of promotion zone
if (m.isPromoteMove()) {
int proZone = (this.isUpperTurn()) ? 0 : BOARD_SIZE - 1;
if (startPosition.getCol() != proZone && endPosition.getCol() != proZone) {
System.err.println("Not in promotion zone. ");
return false;
}
}
// 7. Can specific piece move this way?
if (!piece.isValidMove(startPosition.getRow(), startPosition.getCol(),
endPosition.getRow(), endPosition.getCol())) {
return false;
}
// 8. Pawn pieces must be promoted once they reach the furthest row (otw illegal)
int proZone = (this.isUpperTurn()) ? 0 : BOARD_SIZE - 1;
if (endPosition.getCol() == proZone && piece instanceof Pawn && !m.isPromoteMove()) {
setAt(startPosition, piece.upgrade());
}
// 9. Another piece along path of movement
List<BoardPosition> path = piece.getPath(startPosition, endPosition);
if (!isValidPath(path)) {
return false;
}
return true;
}
// Returns whether or not drop is valid
private boolean isValidDrop(Drop d) {
// Handle illegal moves
// 1. Drop it off board
// 2. Dropping on occupied square
// 3. Dropping a piece not in your captured list
// 4. Cannot drop Pawn piece onto same column as another Pawn
// 5. Pawn cannot be dropped into promotion zone
BoardPosition position = d.getPosition();
// 1. Drop it off board
if (!position.isRowInRange(BOARD_SIZE) && !position.isColInRange(BOARD_SIZE)) {
return false;
}
// 2. Drop it on occupied square
if (isOccupied(position)) {
return false;
}
// 3. Dropping a piece not in your captured list
boolean contains = false;
List<Piece> captures = (isUpperTurn()) ? capturedByUpper : capturedByLower;
for (Piece p : captures) {
if (p.toString().equalsIgnoreCase(d.getPieceName())) {
contains = true;
}
}
if (!contains) {
return false;
}
// 4. Pawn cannot be dropped into a column with another Pawn
String piece = d.getPieceName();
if (piece.equalsIgnoreCase("p")) {
// Search for a pawn in the same column
int pRow = position.getRow();
for (int i = 0; i < BOARD_SIZE; i++) {
Piece testPiece = board[pRow][i];
if (testPiece instanceof Pawn && testPiece.isUpper() == isUpperTurn()) {
return false;
}
}
}
// 5. Pawn cannot be dropped into promotion zone
if (piece.equalsIgnoreCase("p")) {
int proZone = (this.isUpperTurn()) ? 0 : BOARD_SIZE - 1;
if (position.getCol() == proZone) {
return false;
}
}
return true;
}
// Checks if path on board is available - Are there pieces in the way?
private boolean isValidPath(List<BoardPosition> path) {
if (path == null) {
return false;
}
// Look at every location along the path
for (int i = 1; i < path.size() - 1; i++) {
if (this.isOccupied(path.get(i))) {
return false;
}
}
return true;
}
// Returns list of captured pieces by UPPER
public List<Piece> capturesUpper() {
return new ArrayList<>(capturedByUpper);
}
// Returns list of captures pieces by lower
public List<Piece> capturesLower() {
return new ArrayList<>(capturedByLower);
}
// Whose turn is it?
public boolean isUpperTurn() {
return (turn % 2 == 1);
}
// Checks if game is over due to total move count
public boolean hasRemainingMoves() {
return turn < MAX_MOVES;
}
// Checks if game is over due to check mate
public boolean isCheckMated() {
return this.checkMated;
}
// Checks if game is over due to an illegal move
public boolean isIllegalMove() {
return this.illegalMove;
}
// Returns whether or not game has ended via illegal move, checkmate or move count
public boolean gameOver() {
return isIllegalMove() || isCheckMated() || !hasRemainingMoves();
}
// Gets the current player
public String getCurrentPlayer() {
if (turn % 2 == 0) {
return "lower";
}
return "UPPER";
}
// Gets the opponent player
public String getOtherPlayer() {
if (turn % 2 == 0) {
return "UPPER";
}
return "lower";
}
// Checks if square on board is occupied
private boolean isOccupied(int col, int row) {
return board[col][row] != null;
}
// Checks if square on board is occupied (using BoardPosition object for convenience)
private boolean isOccupied(BoardPosition bp) {
return isOccupied(bp.getRow(), bp.getCol());
}
// Gets the piece at specified position
private Piece getAt(BoardPosition bp) {
int row = bp.getRow(), col = bp.getCol();
return board[row][col];
}
// Sets a new piece location on board
private void setAt(BoardPosition bp, Piece p) {
board[bp.getRow()][bp.getCol()] = p;
}
// Returns Piece object from a pieces String representation
private Piece makeNewPiece(String name) {
if (name == null || name.isEmpty()) {
return null;
}
char last = name.charAt(name.length() - 1);
boolean isUpper = Character.isUpperCase(last);
last = Character.toLowerCase(last);
Piece p = null;
switch (last) {
case 'p':
p = new Pawn(isUpper);
break;
case 'd':
p = new King(isUpper);
break;
case 's':
p = new Gold(isUpper);
break;
case 'r':
p = new Silver(isUpper);
break;
case 'g':
p = new Bishop(isUpper);
break;
case 'n':
p = new Rook(isUpper);
break;
default:
throw new IllegalArgumentException("Invalid piece: " + name);
}
// Promoted pieces represented by a '+' in front of their String
if (name.charAt(0) == '+') {
p = p.upgrade();
}
return p;
}
/* Print board */
public String toString() {
String[][] pieces = new String[BOARD_SIZE][BOARD_SIZE];
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
Piece curr = (Piece) board[col][row];
pieces[col][row] = this.isOccupied(col, row) ? board[col][row].toString() : "";
}
}
return stringifyBoard(pieces);
}
private String stringifyBoard(String[][] board) {
String str = "";
for (int row = board.length - 1; row >= 0; row--) {
str += Integer.toString(row + 1) + " |";
for (int col = 0; col < board[row].length; col++) {
str += stringifySquare(board[col][row]);
}
str += System.getProperty("line.separator");
}
str += " a b c d e" + System.getProperty("line.separator");
return str;
}
private String stringifySquare(String sq) {
switch (sq.length()) {
case 0:
return "__|";
case 1:
return " " + sq + "|";
case 2:
return sq + "|";
}
throw new IllegalArgumentException("Board must be an array of strings like \"\", \"P\", or \"+P\"");
}
}