-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathChessBoard.java
More file actions
430 lines (390 loc) · 14.1 KB
/
ChessBoard.java
File metadata and controls
430 lines (390 loc) · 14.1 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
import java.util.ArrayList;
import javax.swing.*;
import Pieces.*;
public class ChessBoard {
private ChessPiece[][] board;
protected boolean whiteToMove;
protected boolean whiteInCheck;
protected boolean blackInCheck;
protected int moveCounter = 1;
//for undo
ArrayList<UndoMoveObject> undoArray = new ArrayList<UndoMoveObject>();
//for redo
ArrayList<RedoMoveObject> redoArray = new ArrayList<RedoMoveObject>();
//JTextArea stuff for toolbar
protected JTextArea checkNotice;
protected JTextArea showMoves;
public ChessBoard() {
board = new ChessPiece[8][8];
whiteToMove = true;
setupDefault();
}
public ChessBoard(int row, int col) {
board = new ChessPiece[row][col];
whiteToMove = true;
setupDefault();
}
public ChessPiece getPiece(int row, int col) {return board[row][col];}
public ChessPiece[][] getBoard() {return board;}
public void changeBoard(ChessPiece[][] board) {this.board = board;}
public boolean getWhitesMove() {return whiteToMove;}
public JTextArea getMoves() {return showMoves;}
public void setMoveText(String text) {showMoves.setText(text);}
public int getMoveCount() {return moveCounter;}
public void setMoveCount(int count) {moveCounter = count;}
public boolean isSameTeam(int initialRow, int initialCol, int finalRow, int finalCol) {
return (
board[initialRow][initialCol] != null &&
board[finalRow][finalCol] != null &&
(board[initialRow][initialCol].getClass().getName().charAt(7) ==
board[finalRow][finalCol].getClass().getName().charAt(7))
);
}
public void flipWhiteToMove() {
whiteToMove = !whiteToMove;
}
public void setPiece(ChessPiece piece, int row, int col) {
board[row][col] = piece;
piece.setRow(row);
piece.setCol(col);
}
public void movePiece(int initialRow, int initialCol, int finalRow, int finalCol) {
ChessPiece aPiece = board[initialRow][initialCol];
aPiece.setRow(finalRow);
aPiece.setCol(finalCol);
board[finalRow][finalCol] = aPiece;
board[initialRow][initialCol] = null;
}
//no jtextarea update
public boolean miniIsMoveValid(int initialRow, int initialCol, int finalRow, int finalCol) {
if (board[finalRow][finalCol] != null &&
(board[initialRow][initialCol].getClass().getName().charAt(7) ==
board[finalRow][finalCol].getClass().getName().charAt(7))
) {
return false;
}
// can-the-piece-even-move-there test
if (!board[initialRow][initialCol].canMoveToLocation(board, finalRow, finalCol)) return false;
// is the king in check before/after the move?!
boolean beforeInCheck = false;
boolean afterInCheck = false;
ChessPiece whiteKing = null;
ChessPiece blackKing = null;
ChessPiece temp = board[finalRow][finalCol];
//get king pointers
for (int r = 0; r < 8; r++) {
for (int c = 0; c < 8; c++) {
if (board[r][c] == null) continue;
String name = board[r][c].getClass().getName().substring(7);
if (name.equals("whiteKing")) whiteKing = board[r][c];
if (name.equals("blackKing")) blackKing = board[r][c];
}
}
//is white king in check before/after?
if (whiteKing.amIInCheck(board)) {
movePiece(initialRow, initialCol, finalRow, finalCol);
if (whiteKing.amIInCheck(board)) {
movePiece(finalRow, finalCol, initialRow, initialCol);
board[finalRow][finalCol] = temp;
return false;
}
movePiece(finalRow, finalCol, initialRow, initialCol);
board[finalRow][finalCol] = temp;
}
//is black king in check before/after?
if (blackKing.amIInCheck(board)) {
movePiece(initialRow, initialCol, finalRow, finalCol);
if (blackKing.amIInCheck(board)) {
movePiece(finalRow, finalCol, initialRow, initialCol);
board[finalRow][finalCol] = temp;
return false;
}
movePiece(finalRow, finalCol, initialRow, initialCol);
board[finalRow][finalCol] = temp;
}
//is king in check after the move?
movePiece(initialRow, initialCol, finalRow, finalCol);
if (whiteToMove && whiteKing.amIInCheck(board)) {
movePiece(finalRow, finalCol, initialRow, initialCol);
board[finalRow][finalCol] = temp;
return false;
}
if (!whiteToMove && blackKing.amIInCheck(board)) {
movePiece(finalRow, finalCol, initialRow, initialCol);
board[finalRow][finalCol] = temp;
return false;
}
movePiece(finalRow, finalCol, initialRow, initialCol);
board[finalRow][finalCol] = temp;
return true;
}
public boolean isMoveValid(int initialRow, int initialCol, int finalRow, int finalCol) {
// here we go
// same-color test
if (board[finalRow][finalCol] != null &&
(board[initialRow][initialCol].getClass().getName().charAt(7) ==
board[finalRow][finalCol].getClass().getName().charAt(7))
) {
return false;
}
// can-the-piece-even-move-there test
if (!board[initialRow][initialCol].canMoveToLocation(board, finalRow, finalCol)) return false;
// is the king in check before/after the move?!
boolean beforeInCheck = false;
boolean afterInCheck = false;
ChessPiece whiteKing = null;
ChessPiece blackKing = null;
ChessPiece temp = board[finalRow][finalCol];
//get king pointers
for (int r = 0; r < 8; r++) {
for (int c = 0; c < 8; c++) {
if (board[r][c] == null) continue;
String name = board[r][c].getClass().getName().substring(7);
if (name.equals("whiteKing")) whiteKing = board[r][c];
if (name.equals("blackKing")) blackKing = board[r][c];
}
}
//is white king in check before/after?
if (whiteKing.amIInCheck(board)) {
movePiece(initialRow, initialCol, finalRow, finalCol);
if (whiteKing.amIInCheck(board)) {
movePiece(finalRow, finalCol, initialRow, initialCol);
board[finalRow][finalCol] = temp;
return false;
}
movePiece(finalRow, finalCol, initialRow, initialCol);
board[finalRow][finalCol] = temp;
}
//is black king in check before/after?
if (blackKing.amIInCheck(board)) {
movePiece(initialRow, initialCol, finalRow, finalCol);
if (blackKing.amIInCheck(board)) {
movePiece(finalRow, finalCol, initialRow, initialCol);
board[finalRow][finalCol] = temp;
return false;
}
movePiece(finalRow, finalCol, initialRow, initialCol);
board[finalRow][finalCol] = temp;
}
//is king in check after the move?
movePiece(initialRow, initialCol, finalRow, finalCol);
if (whiteToMove && whiteKing.amIInCheck(board)) {
movePiece(finalRow, finalCol, initialRow, initialCol);
return false;
}
if (!whiteToMove && blackKing.amIInCheck(board)) {
movePiece(finalRow, finalCol, initialRow, initialCol);
return false;
}
//update check notice
if (!whiteToMove && whiteKing.amIInCheck(board)) {
checkNotice.setText("");
checkNotice.append("White is in check!");
}
else if (whiteToMove && blackKing.amIInCheck(board)) {
checkNotice.setText("");
checkNotice.append("Black is in check!");
}
else checkNotice.setText("");
//movePiece(finalRow, finalCol, initialRow, initialCol);
//board[finalRow][finalCol] = temp;
//update if white or black is in check
//movePiece(initialRow, initialCol, finalRow, finalCol);
if (whiteKing.amIInCheck(board)) {
whiteInCheck = true;
}
else whiteInCheck = false;
if (blackKing.amIInCheck(board)) {
blackInCheck = true;
}
else blackInCheck = false;
//for JTextArea
String pieceMove = board[finalRow][finalCol].toString();
movePiece(finalRow, finalCol, initialRow, initialCol);
board[finalRow][finalCol] = temp;
if (board[finalRow][finalCol] != null) {
pieceMove = pieceMove.substring(0, pieceMove.length() - 2) + "x" + pieceMove.substring(pieceMove.length() - 2, pieceMove.length());
}
if (whiteInCheck || blackInCheck) pieceMove = pieceMove + "+";
//is it a castle?
if (board[initialRow][initialCol] instanceof whiteKing || board[initialRow][initialCol] instanceof blackKing) {
if (finalCol - initialCol == 2) pieceMove = "0-0";
if (initialCol - finalCol == 2) pieceMove = "0-0-0";
}
//update move JTextArea
if (whiteToMove) {
showMoves.append(String.format("%-10s", moveCounter + ". " + pieceMove));
}
else {
showMoves.append(pieceMove + "\n");
moveCounter++;
}
//add to undo move array
UndoMoveObject newUndo = new UndoMoveObject(board[initialRow][initialCol], initialRow, initialCol, board[finalRow][finalCol]);
if (!(board[initialRow][initialCol].haveIMoved())) newUndo.setNotMoved();
if (board[initialRow][initialCol] instanceof whiteKing || board[initialRow][initialCol] instanceof blackKing) {
if (Math.abs(initialCol - finalCol) == 2) {
newUndo.isCastle();
}
}
undoArray.add(newUndo);
flipWhiteToMove();
return true;
}
public void undo() {
UndoMoveObject undo = undoArray.get(undoArray.size() - 1);
int row = undo.getUndoPiece().getRow();
int col = undo.getUndoPiece().getCol();
movePiece(row, col, undo.getLastRow(), undo.getLastCol());
board[row][col] = undo.getTakenPiece();
if (undo.hadNotMoved()) undo.getUndoPiece().undoHasMoved();
if (undo.checkIfCastle()) {
if (col - undo.getLastCol() == 2) {
movePiece(row, col - 1, row, col + 1);
board[row][col + 1].undoHasMoved();
}
if (undo.getLastCol() - col == 2) {
movePiece(row, col + 1, row, col - 2);
board[row][col - 2].undoHasMoved();
}
}
//make redo object
RedoMoveObject newRedo = new RedoMoveObject(board[undo.getLastRow()][undo.getLastCol()], row, col);
if (undo.checkIfCastle()) newRedo.isCastle();
redoArray.add(newRedo);
undoArray.remove(undoArray.size() - 1);
// truncate move JTextArea
//undo black's move
String moveText = showMoves.getText();
int truncateLocation = 0;
if (whiteToMove) {
for (truncateLocation = moveText.length() - 1; truncateLocation > moveText.length() - 20; truncateLocation--) {
if (moveText.charAt(truncateLocation) == ' ') break;
}
moveText = moveText.substring(0, truncateLocation + 1);
moveCounter--;
}
//undo white's move
if (!whiteToMove) {
for (truncateLocation = moveText.length() - 1; truncateLocation > moveText.length() - 20; truncateLocation--) {
if (moveText.charAt(truncateLocation) == '.') break;
}
moveText = moveText.substring(0, truncateLocation - 1);
}
showMoves.setText(moveText);
flipWhiteToMove();
}
public void redo() {
if (redoArray.size() == 0) return;
RedoMoveObject redoObject = redoArray.get(redoArray.size() - 1);
if (whiteToMove && redoObject.getRedoPiece().getClass().getName().charAt(7) == 'b') return;
if (!whiteToMove && redoObject.getRedoPiece().getClass().getName().charAt(7) == 'w') return;
ChessPiece redoPiece = redoObject.getRedoPiece();
UndoMoveObject undoObject = new UndoMoveObject(redoPiece, redoPiece.getRow(), redoPiece.getCol(), board[redoObject.getFutureRow()][redoObject.getFutureCol()]);
if (redoObject.checkIfCastle()) undoObject.isCastle();
undoArray.add(undoObject);
//for JTextArea
boolean pieceWasTaken = (board[redoObject.getFutureRow()][redoObject.getFutureCol()] != null);
if (redoObject.checkIfCastle()) {
if (redoPiece.getCol() - redoObject.getFutureCol() == 2) {
movePiece(redoPiece.getRow(), redoPiece.getCol() - 4, redoPiece.getRow(), redoPiece.getCol() - 1);
}
if (redoObject.getFutureCol() - redoPiece.getCol() == 2) {
movePiece(redoPiece.getRow(), redoPiece.getCol() + 3, redoPiece.getRow(), redoPiece.getCol() + 1);
}
}
int initialCol = redoPiece.getCol();
movePiece(redoPiece.getRow(), redoPiece.getCol(), redoObject.getFutureRow(), redoObject.getFutureCol());
String pieceMove = redoObject.getRedoPiece().toString();
if (pieceWasTaken) {
pieceMove = pieceMove.substring(0, pieceMove.length() - 2) + "x" + pieceMove.substring(pieceMove.length() - 2, pieceMove.length());
}
if (whiteInCheck || blackInCheck) pieceMove = pieceMove + "+";
//is it a castle?
if (redoPiece instanceof whiteKing || redoPiece instanceof blackKing) {
if (redoObject.getFutureCol() - initialCol == 2) pieceMove = "0-0";
if (initialCol - redoObject.getFutureCol() == 2) pieceMove = "0-0-0";
}
//update move JTextArea
if (whiteToMove) {
showMoves.append(String.format("%-15s", moveCounter + ". " + pieceMove));
}
else {
showMoves.append(pieceMove + "\n");
moveCounter++;
}
flipWhiteToMove();
redoArray.remove(redoArray.size() - 1);
}
public void resetBoard() {
moveCounter = 1;
showMoves.setText(String.format("%-10s", "White") + "Black");
checkNotice.setText("");
if (!whiteToMove) flipWhiteToMove();
}
public ArrayList<Integer> getPossibleMoves(ChessPiece piece) {
ArrayList<Integer> possibleMoves = new ArrayList<Integer>();
for (int r = 0; r < 8; r++) {
for (int c = 0; c < 8; c++) {
if (miniIsMoveValid(piece.getRow(), piece.getCol(), r, c)) {
Integer a = (10 * r) + c;
possibleMoves.add(a);
}
}
}
return possibleMoves;
}
public boolean isCheckmate() {
ChessPiece wKing = null;
ChessPiece bKing = null;
//get pointers for kings
for (int r = 0; r < 8; r++) {
for (int c = 0; c < 8; c++) {
if (board[r][c] == null) continue;
if (board[r][c] instanceof blackKing) bKing = board[r][c];
if (board[r][c] instanceof whiteKing) wKing = board[r][c];
}
}
for (int r = 0; r < 8; r++) {
for (int c = 0; c < 8; c++) {
if (board[r][c] == null) continue;
if (whiteToMove && board[r][c].getClass().getName().charAt(7) == 'b') continue;
if (!whiteToMove && board[r][c].getClass().getName().charAt(7) == 'w') continue;
ArrayList<Integer> possMoves = getPossibleMoves(board[r][c]);
if (possMoves.size() > 0) {
return false;
}
}
}
return true;
}
// default setup
public void setupDefault() {
//preliminary clearing of board
for (int r = 0; r < 8; r++) {
for (int c = 0; c < 8; c++) {
board[r][c] = null;
}
}
for (int i = 0; i < 8; i++) {
board[1][i] = new whitePawn(1, i);
board[6][i] = new blackPawn(6, i);
}
board[0][0] = new whiteRook(0, 0);
board[0][1] = new whiteKnight(0, 1);
board[0][2] = new whiteBishop(0, 2);
board[0][3] = new whiteQueen(0, 3);
board[0][4] = new whiteKing(0, 4);
board[0][5] = new whiteBishop(0, 5);
board[0][6] = new whiteKnight(0, 6);
board[0][7] = new whiteRook(0, 7);
board[7][0] = new blackRook(7, 0);
board[7][1] = new blackKnight(7, 1);
board[7][2] = new blackBishop(7, 2);
board[7][3] = new blackQueen(7, 3);
board[7][4] = new blackKing(7, 4);
board[7][5] = new blackBishop(7, 5);
board[7][6] = new blackKnight(7, 6);
board[7][7] = new blackRook(7, 7);
}
}