-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessGame.java
More file actions
432 lines (393 loc) · 17.2 KB
/
Copy pathChessGame.java
File metadata and controls
432 lines (393 loc) · 17.2 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
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/** The rules and state for one chess game, independent of its user interface. */
public class ChessGame {
public enum Side { WHITE, BLACK }
public enum PieceType { KING, QUEEN, ROOK, BISHOP, KNIGHT, PAWN }
public enum Result { ONGOING, CHECKMATE, STALEMATE, DRAW_INSUFFICIENT_MATERIAL }
/** An immutable piece; row zero is Black's home rank. */
public static final class Piece {
public final Side side;
public final PieceType type;
private Piece(Side side, PieceType type) {
this.side = side;
this.type = type;
}
}
/** An immutable move. Promotions must name the new piece explicitly. */
public static final class Move {
public final int fromRow;
public final int fromCol;
public final int toRow;
public final int toCol;
public final PieceType promotion;
public Move(int fromRow, int fromCol, int toRow, int toCol, PieceType promotion) {
this.fromRow = fromRow;
this.fromCol = fromCol;
this.toRow = toRow;
this.toCol = toCol;
this.promotion = promotion;
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (!(other instanceof Move move)) return false;
return fromRow == move.fromRow && fromCol == move.fromCol
&& toRow == move.toRow && toCol == move.toCol
&& promotion == move.promotion;
}
@Override
public int hashCode() {
return Objects.hash(fromRow, fromCol, toRow, toCol, promotion);
}
@Override
public String toString() {
return "(" + fromRow + "," + fromCol + ") to (" + toRow + "," + toCol + ")"
+ (promotion == null ? "" : "=" + promotion);
}
}
private static final int[][] KNIGHT_STEPS = {
{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2},
{1, -2}, {1, 2}, {2, -1}, {2, 1}
};
private static final int[][] DIAGONALS = {{-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
private static final int[][] STRAIGHTS = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
private static final PieceType[] PROMOTIONS = {
PieceType.QUEEN, PieceType.ROOK, PieceType.BISHOP, PieceType.KNIGHT
};
private Piece[][] board = new Piece[8][8];
private Side turn;
private boolean whiteKingSide;
private boolean whiteQueenSide;
private boolean blackKingSide;
private boolean blackQueenSide;
private int enPassantRow;
private int enPassantCol;
/** Starts with the standard chess position. */
public ChessGame() {
reset();
}
/** Restores the standard position, including all special move rights. */
public void reset() {
board = new Piece[8][8];
PieceType[] home = {
PieceType.ROOK, PieceType.KNIGHT, PieceType.BISHOP, PieceType.QUEEN,
PieceType.KING, PieceType.BISHOP, PieceType.KNIGHT, PieceType.ROOK
};
for (int col = 0; col < 8; col++) {
board[0][col] = new Piece(Side.BLACK, home[col]);
board[1][col] = new Piece(Side.BLACK, PieceType.PAWN);
board[6][col] = new Piece(Side.WHITE, PieceType.PAWN);
board[7][col] = new Piece(Side.WHITE, home[col]);
}
turn = Side.WHITE;
whiteKingSide = whiteQueenSide = blackKingSide = blackQueenSide = true;
enPassantRow = enPassantCol = -1;
}
/** Returns the piece on a square, or null for an empty square. */
public Piece pieceAt(int row, int col) {
requireSquare(row, col);
return board[row][col];
}
/** Returns the side whose move is next. */
public Side sideToMove() {
return turn;
}
/** Returns every legal move from the square for the side to move. */
public List<Move> legalMovesFrom(int row, int col) {
if (!onBoard(row, col)) return List.of();
Piece piece = board[row][col];
if (piece == null || piece.side != turn) return List.of();
List<Move> legal = new ArrayList<>();
for (Move move : pseudoMoves(row, col, piece)) {
Piece[][] next = copyBoard();
moveOnBoard(next, move);
if (!isInCheck(next, turn)) legal.add(move);
}
return List.copyOf(legal);
}
/** Returns all legal moves for the side to move. */
public List<Move> legalMoves() {
List<Move> moves = new ArrayList<>();
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (board[row][col] != null && board[row][col].side == turn) {
moves.addAll(legalMovesFrom(row, col));
}
}
}
return List.copyOf(moves);
}
/** Applies a legal move and returns true, or leaves the position unchanged and returns false. */
public boolean makeMove(Move move) {
if (move == null || !onBoard(move.fromRow, move.fromCol)
|| result() != Result.ONGOING
|| !legalMovesFrom(move.fromRow, move.fromCol).contains(move)) return false;
Piece moving = board[move.fromRow][move.fromCol];
Piece captured = board[move.toRow][move.toCol];
updateCastlingRights(moving, move, captured);
moveOnBoard(board, move);
enPassantRow = enPassantCol = -1;
if (moving.type == PieceType.PAWN && Math.abs(move.toRow - move.fromRow) == 2) {
enPassantRow = (move.toRow + move.fromRow) / 2;
enPassantCol = move.fromCol;
}
turn = opposite(turn);
return true;
}
/** Returns whether the named side's king is attacked. */
public boolean isInCheck(Side side) {
Objects.requireNonNull(side, "side");
return isInCheck(board, side);
}
/** Returns the current game result. */
public Result result() {
if (legalMoves().isEmpty()) return isInCheck(turn) ? Result.CHECKMATE : Result.STALEMATE;
if (hasInsufficientMaterial()) return Result.DRAW_INSUFFICIENT_MATERIAL;
return Result.ONGOING;
}
private List<Move> pseudoMoves(int row, int col, Piece piece) {
List<Move> moves = new ArrayList<>();
switch (piece.type) {
case PAWN -> pawnMoves(moves, row, col, piece.side);
case KNIGHT -> {
for (int[] step : KNIGHT_STEPS) {
addIfAvailable(moves, row, col, row + step[0], col + step[1], piece.side);
}
}
case BISHOP -> addSlidingMoves(moves, row, col, piece.side, DIAGONALS);
case ROOK -> addSlidingMoves(moves, row, col, piece.side, STRAIGHTS);
case QUEEN -> {
addSlidingMoves(moves, row, col, piece.side, DIAGONALS);
addSlidingMoves(moves, row, col, piece.side, STRAIGHTS);
}
case KING -> {
for (int dr = -1; dr <= 1; dr++) {
for (int dc = -1; dc <= 1; dc++) {
if (dr != 0 || dc != 0) {
addIfAvailable(moves, row, col, row + dr, col + dc, piece.side);
}
}
}
addCastlingMoves(moves, row, col, piece.side);
}
}
return moves;
}
private void pawnMoves(List<Move> moves, int row, int col, Side side) {
int direction = side == Side.WHITE ? -1 : 1;
int start = side == Side.WHITE ? 6 : 1;
int next = row + direction;
if (onBoard(next, col) && board[next][col] == null) {
addPawnMove(moves, row, col, next, col);
int doubleStep = row + 2 * direction;
if (row == start && board[doubleStep][col] == null) {
moves.add(new Move(row, col, doubleStep, col, null));
}
}
for (int delta : new int[]{-1, 1}) {
int targetCol = col + delta;
if (!onBoard(next, targetCol)) continue;
Piece target = board[next][targetCol];
if (target != null && target.side != side && target.type != PieceType.KING) {
addPawnMove(moves, row, col, next, targetCol);
} else if (next == enPassantRow && targetCol == enPassantCol && target == null) {
Piece victim = board[row][targetCol];
if (victim != null && victim.side != side && victim.type == PieceType.PAWN) {
moves.add(new Move(row, col, next, targetCol, null));
}
}
}
}
private void addPawnMove(List<Move> moves, int fromRow, int fromCol, int toRow, int toCol) {
if (toRow == 0 || toRow == 7) {
for (PieceType promotion : PROMOTIONS) {
moves.add(new Move(fromRow, fromCol, toRow, toCol, promotion));
}
} else {
moves.add(new Move(fromRow, fromCol, toRow, toCol, null));
}
}
private void addIfAvailable(List<Move> moves, int row, int col, int toRow, int toCol, Side side) {
if (!onBoard(toRow, toCol)) return;
Piece target = board[toRow][toCol];
if (target == null || (target.side != side && target.type != PieceType.KING)) {
moves.add(new Move(row, col, toRow, toCol, null));
}
}
private void addSlidingMoves(List<Move> moves, int row, int col, Side side, int[][] directions) {
for (int[] direction : directions) {
int toRow = row + direction[0];
int toCol = col + direction[1];
while (onBoard(toRow, toCol)) {
Piece target = board[toRow][toCol];
if (target == null) {
moves.add(new Move(row, col, toRow, toCol, null));
} else {
if (target.side != side && target.type != PieceType.KING) {
moves.add(new Move(row, col, toRow, toCol, null));
}
break;
}
toRow += direction[0];
toCol += direction[1];
}
}
}
private void addCastlingMoves(List<Move> moves, int row, int col, Side side) {
int home = side == Side.WHITE ? 7 : 0;
if (row != home || col != 4 || isInCheck(board, side)) return;
Side attacker = opposite(side);
boolean kingSide = side == Side.WHITE ? whiteKingSide : blackKingSide;
boolean queenSide = side == Side.WHITE ? whiteQueenSide : blackQueenSide;
if (kingSide && isRook(home, 7, side) && board[home][5] == null && board[home][6] == null
&& !isSquareAttacked(board, home, 5, attacker)
&& !isSquareAttacked(board, home, 6, attacker)) {
moves.add(new Move(home, 4, home, 6, null));
}
if (queenSide && isRook(home, 0, side) && board[home][1] == null
&& board[home][2] == null && board[home][3] == null
&& !isSquareAttacked(board, home, 3, attacker)
&& !isSquareAttacked(board, home, 2, attacker)) {
moves.add(new Move(home, 4, home, 2, null));
}
}
private boolean isRook(int row, int col, Side side) {
Piece piece = board[row][col];
return piece != null && piece.side == side && piece.type == PieceType.ROOK;
}
private void updateCastlingRights(Piece moving, Move move, Piece captured) {
if (moving.type == PieceType.KING) {
if (moving.side == Side.WHITE) whiteKingSide = whiteQueenSide = false;
else blackKingSide = blackQueenSide = false;
}
if (moving.type == PieceType.ROOK) removeRookRight(moving.side, move.fromRow, move.fromCol);
if (captured != null && captured.type == PieceType.ROOK) {
removeRookRight(captured.side, move.toRow, move.toCol);
}
}
private void removeRookRight(Side side, int row, int col) {
if (side == Side.WHITE && row == 7) {
if (col == 0) whiteQueenSide = false;
if (col == 7) whiteKingSide = false;
} else if (side == Side.BLACK && row == 0) {
if (col == 0) blackQueenSide = false;
if (col == 7) blackKingSide = false;
}
}
private void moveOnBoard(Piece[][] position, Move move) {
Piece piece = position[move.fromRow][move.fromCol];
if (piece.type == PieceType.PAWN && move.fromCol != move.toCol
&& position[move.toRow][move.toCol] == null) {
position[move.fromRow][move.toCol] = null; // en passant
}
position[move.toRow][move.toCol] = move.promotion == null
? piece : new Piece(piece.side, move.promotion);
position[move.fromRow][move.fromCol] = null;
if (piece.type == PieceType.KING && Math.abs(move.toCol - move.fromCol) == 2) {
int rookFrom = move.toCol == 6 ? 7 : 0;
int rookTo = move.toCol == 6 ? 5 : 3;
position[move.fromRow][rookTo] = position[move.fromRow][rookFrom];
position[move.fromRow][rookFrom] = null;
}
}
private Piece[][] copyBoard() {
Piece[][] copy = new Piece[8][8];
for (int row = 0; row < 8; row++) {
System.arraycopy(board[row], 0, copy[row], 0, 8);
}
return copy;
}
private static boolean isInCheck(Piece[][] position, Side side) {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
Piece piece = position[row][col];
if (piece != null && piece.side == side && piece.type == PieceType.KING) {
return isSquareAttacked(position, row, col, opposite(side));
}
}
}
throw new IllegalStateException("Position has no " + side + " king");
}
private static boolean isSquareAttacked(Piece[][] position, int row, int col, Side bySide) {
int pawnSourceRow = row + (bySide == Side.WHITE ? 1 : -1);
for (int dc : new int[]{-1, 1}) {
if (isPiece(position, pawnSourceRow, col + dc, bySide, PieceType.PAWN)) return true;
}
for (int[] step : KNIGHT_STEPS) {
if (isPiece(position, row + step[0], col + step[1], bySide, PieceType.KNIGHT)) return true;
}
for (int dr = -1; dr <= 1; dr++) {
for (int dc = -1; dc <= 1; dc++) {
if ((dr != 0 || dc != 0)
&& isPiece(position, row + dr, col + dc, bySide, PieceType.KING)) return true;
}
}
return attackedAlong(position, row, col, bySide, DIAGONALS, PieceType.BISHOP)
|| attackedAlong(position, row, col, bySide, STRAIGHTS, PieceType.ROOK);
}
private static boolean attackedAlong(Piece[][] position, int row, int col, Side bySide,
int[][] directions, PieceType type) {
for (int[] direction : directions) {
int scanRow = row + direction[0];
int scanCol = col + direction[1];
while (onBoard(scanRow, scanCol)) {
Piece piece = position[scanRow][scanCol];
if (piece != null) {
if (piece.side == bySide && (piece.type == type || piece.type == PieceType.QUEEN)) {
return true;
}
break;
}
scanRow += direction[0];
scanCol += direction[1];
}
}
return false;
}
private static boolean isPiece(Piece[][] position, int row, int col, Side side, PieceType type) {
if (!onBoard(row, col)) return false;
Piece piece = position[row][col];
return piece != null && piece.side == side && piece.type == type;
}
private boolean hasInsufficientMaterial() {
int minorPieces = 0;
int knights = 0;
int bishopSquareColor = -1;
boolean bishopsOnOneColor = true;
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
Piece piece = board[row][col];
if (piece == null || piece.type == PieceType.KING) continue;
switch (piece.type) {
case PAWN, ROOK, QUEEN -> { return false; }
case BISHOP -> {
minorPieces++;
int squareColor = (row + col) & 1;
if (bishopSquareColor >= 0 && squareColor != bishopSquareColor) {
bishopsOnOneColor = false;
}
bishopSquareColor = squareColor;
}
case KNIGHT -> {
minorPieces++;
knights++;
}
default -> { }
}
}
}
if (minorPieces <= 1) return true; // Bare kings, or one minor piece.
return knights == 0 && bishopsOnOneColor;
}
private static Side opposite(Side side) {
return side == Side.WHITE ? Side.BLACK : Side.WHITE;
}
private static boolean onBoard(int row, int col) {
return row >= 0 && row < 8 && col >= 0 && col < 8;
}
private static void requireSquare(int row, int col) {
if (!onBoard(row, col)) throw new IllegalArgumentException("Square is outside the board");
}
}