-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessBoard.java
More file actions
483 lines (446 loc) · 14.3 KB
/
ChessBoard.java
File metadata and controls
483 lines (446 loc) · 14.3 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class ChessBoard extends JLayeredPane {
private static int BOARD_WIDTH = 600;
private static int BOARD_HEIGHT = 600;
private Dimension size = new Dimension(BOARD_WIDTH,BOARD_HEIGHT);
private JPanel chessBoard;
private JPanel[][] chessBoardGrid = new JPanel[8][8];
private Chess chessGame;
private Square[][] gameBoard;
boolean playerTurn = true;
int currentKingX = 0;
int currentKingY = 0;
private boolean inCheck = false;
private MouseControl adapter;
int numMoves = 0;
public ChessBoard() {
chessGame = new Chess();
chessBoard = new JPanel(new GridLayout(8,8));
chessBoard.setPreferredSize(size);
chessBoard.setBounds(0, 0, BOARD_WIDTH, BOARD_HEIGHT);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
chessBoardGrid[i][j] = new JPanel(new GridBagLayout());
chessBoard.add(chessBoardGrid[i][j]);
if (i % 2 == 0) {
if (j % 2 == 0) {
chessBoardGrid[i][j].setBackground(Color.GRAY);
} else {
chessBoardGrid[i][j].setBackground(Color.WHITE);
}
} else {
if (j % 2 == 0) {
chessBoardGrid[i][j].setBackground(Color.WHITE);
} else {
chessBoardGrid[i][j].setBackground(Color.GRAY);
} }
}
}
adapter = new MouseControl();
addMouseListener(adapter);
addMouseMotionListener(adapter);
addPieces();
add(chessBoard);
setVisible(true);
}
public void addPieces() {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
JPanel chessSquare = (JPanel) chessBoardGrid[row][col];
if (row == 0) {
if (col == 0 || col == 7) {
JLabel blackR = new JLabel(new ImageIcon("b-pawn.png"));
chessSquare.add(blackR);
} else if (col == 1 || col == 6) {
JLabel blackKn = new JLabel(new ImageIcon("b-knight.png"));
chessSquare.add(blackKn);
} else if (col == 2 || col == 5) {
JLabel blackB = new JLabel(new ImageIcon("b-bishop.png"));
chessSquare.add(blackB);
} else if (col == 3) {
JLabel blackQ = new JLabel(new ImageIcon("b-queen.png"));
chessSquare.add(blackQ);
} else if (col == 4) {
JLabel blackK = new JLabel(new ImageIcon("b-king.png"));
chessSquare.add(blackK);
}
} else if (row == 1) {
JLabel blackP = new JLabel(new ImageIcon("b-pawn.png"));
chessSquare.add(blackP);
} else if (row == 6) {
JLabel whiteP = new JLabel(new ImageIcon("w-pawn.png"));
chessSquare.add(whiteP);
} else if (row == 7) {
if (col == 0 || col == 7) {
JLabel whiteR = new JLabel(new ImageIcon("w-rook.png"));
chessSquare.add(whiteR);
} else if (col == 1 || col == 6) {
JLabel whiteKn = new JLabel(new ImageIcon("w-knight.png"));
chessSquare.add(whiteKn);
} else if (col == 2 || col == 5) {
JLabel whiteB = new JLabel(new ImageIcon("w-bishop.png"));
chessSquare.add(whiteB);
} else if (col == 3) {
JLabel whiteQ = new JLabel(new ImageIcon("w-queen.png"));
chessSquare.add(whiteQ);
} else if (col == 4) {
JLabel whiteK = new JLabel(new ImageIcon("w-king.png"));
chessSquare.add(whiteK);
}
}
}
}
}
public void resign() {
if (playerTurn) {
chessGame.setWinner("black");
JOptionPane.showMessageDialog(this, "BLACK WINS!!");
} else {
chessGame.setWinner("white");
JOptionPane.showMessageDialog(this, "WHITE WINS!!");
}
}
private class MouseControl extends MouseAdapter {
private JLabel targetPiece = null;
private JPanel targetPanel = null;
private int adjX;
private int adjY;
private Square startSquare;
private int startRow;
private int startCol;
private Piece currentPiece;
public Square findDestination(JPanel targetPanel) {
gameBoard = chessGame.getBoard();
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (chessBoardGrid[i][j] == targetPanel) {
Square targetSquare = gameBoard[i][j];
targetSquare.addRowIndex(i);
targetSquare.addColIndex(j);
return targetSquare;
}
}
}
return null;
}
@Override
public void mousePressed(MouseEvent e) {
try {
targetPanel = (JPanel) chessBoard.getComponentAt(e.getPoint());
Component component = targetPanel.getComponent(0);
startSquare = findDestination(targetPanel);
currentPiece = startSquare.getPiece();
startRow = startSquare.getRowIndex();
startCol = startSquare.getColIndex();
playerTurn = currentPiece.getPlayer().isWhite;
if (!startSquare.getPiece().getPlayer().isTurn()) {
System.out.println("Its not my turn!");
return;
}
//System.out.println("Starting Space: ("
// + startSquare.getRowIndex() + ", "
// + startSquare.getColIndex() + ")");
if (startSquare.getPiece().getPlayer().isTurn()) {
// Check if component is a chess piece and change its location
if (component instanceof JLabel) {
targetPiece = (JLabel) component;
adjX = targetPiece.getWidth() / 2;
adjY = targetPiece.getHeight() / 2;
targetPiece.setLocation(e.getX() - adjX, e.getY()
- adjY);
add(targetPiece, JLayeredPane.DRAG_LAYER);
repaint();
}
}
} catch (Exception exception) {
}
}
@Override
public void mouseDragged(MouseEvent e) {
if (targetPiece == null) {
return;
}
targetPiece.setLocation(e.getX() - adjX, e.getY() - adjY);
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
if (targetPiece == null) {
return;
}
// remove(targetPiece);
JPanel finalMovePanel = (JPanel) chessBoard.getComponentAt(e
.getPoint());
findDestination(finalMovePanel);
if (targetPanel == finalMovePanel) {
targetPanel.add(targetPiece);
targetPanel.revalidate();
System.out.println("*** RETURN TO SAME SPOT - MOVE ***");
remove(targetPiece);
repaint();
return;
} else {
if (finalMovePanel == null) {
targetPanel.add(targetPiece);
targetPanel.revalidate();
System.out
.println("*** MOVED TO NULL SPOT - OFF BOARD MOVE ***");
remove(targetPiece);
repaint();
return;
} else {
// System.out.println("Ending Space: (" + targetSquare.row +
// ", " + targetSquare.col + ")");
int row = 9;
int col = 9;
for (int roww = 0; roww < 8; roww++) {
for (int coll = 0; coll < 8; coll++) {
if (chessBoardGrid[roww][coll] == finalMovePanel) {
row = roww;
col = coll;
if (currentPiece.isValid(gameBoard, startRow,
startCol, roww, coll)) {
if ("King".equals(currentPiece.getType())) {
if (inCheck) {
if (isKingChecked(getDummyBoard(startRow,
startCol, roww, coll))) {
targetPanel.add(targetPiece);
targetPanel.revalidate();
remove(targetPiece);
repaint();
return;
}
inCheck = false;
System.out.println("KING NOT IN CHECK ANYMORE");
}
}
if (isKingChecked(getDummyBoard(startRow,
startCol, roww, coll))) {
targetPanel.add(targetPiece);
targetPanel.revalidate();
remove(targetPiece);
repaint();
return;
}
if ("Pawn".equals(currentPiece.getType())) {
Pawn tempPawn = (Pawn) currentPiece;
if (!tempPawn.firstMove) {
tempPawn.setMovedOnce();
}
}
if (finalMovePanel.getComponentCount() != 0) {
finalMovePanel.removeAll();
}
finalMovePanel.add(targetPiece);
finalMovePanel.revalidate();
playerTurn = !playerTurn;
gameBoard = chessGame.move(null, startRow,
startCol, roww, coll);
if (kingInCheck(gameBoard, roww, coll)) {
inCheck = true;
}
numMoves++;
} else {
targetPanel.add(targetPiece);
targetPanel.revalidate();
remove(targetPiece);
repaint();
return;
}
}
}
}
// Target location is not on chess board
if (row == 9 || col == 9) {
targetPanel.add(targetPiece);
targetPanel.revalidate();
remove(targetPiece);
repaint();
return;
}
}
}
chessGame.switchTurns();
repaint();
targetPiece = null;
}
}
public void reset() {
setVisible(false);
}
// Finds king and then calls method that checks if king is in check from the last move
public boolean kingInCheck(Square[][] board, int currentRow, int currentCol) {
Player currentPlayer = board[currentRow][currentCol].getPiece().getPlayer();
int row = 0;
int col = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j].getPiece() == null) {
continue;
} else if (board[i][j].getPiece().getPlayer().isWhite == currentPlayer.isWhite) {
continue;
} else if ("King".equals(board[i][j].getPiece().getType())) {
row = i;
col = j;
}
}
}
currentKingX = row;
currentKingY = col;
return checkCheck(board, currentRow, currentCol, row, col);
}
// Checks if last move created check
public boolean checkCheck(Square[][] board, int startRow, int startCol, int kingX, int kingY) {
boolean placeHolder = false;
Player current = board[startRow][startCol].getPiece().getPlayer();
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j].getPiece() == null) {
continue;
} else if (current.isWhite == board[i][j].getPiece()
.getPlayer().isWhite) {
Piece piece = board[i][j].getPiece();
if (piece.isValid(board, i, j, kingX, kingY)) {
System.out.println("KING IN CHECK!");
placeHolder = true;
}
} else if (current.isWhite == board[i][j].getPiece()
.getPlayer().isBlack) {
if (numMoves >= 4) {
if (chessGame.checkCheckMate(board[i][j].getPiece()
.getPlayer(), gameBoard)) {
remove(chessBoard);
adapter = null;
if (playerTurn) {
chessGame.setWinner("black");
JOptionPane.showMessageDialog(this,
"BLACK WINS!!");
} else {
chessGame.setWinner("white");
JOptionPane.showMessageDialog(this,
"WHITE WINS!!");
}
}
}
}
}
}
return placeHolder;
}
public boolean isKingChecked(Square[][] board) {
findKing(board);
Piece currentKing = board[currentKingX][currentKingY].getPiece();
System.out.println("(" + currentKingX + ", " + currentKingY + ")");
if (currentKing == null) {
System.out.println("KING IS NULL");
return true;
}
if (!"King".equals(currentKing.getType())) {
return false;
}
Player currentPlayer = currentKing.getPlayer();
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j].getPiece() == null) {
continue;
} else if (currentPlayer.isWhite == board[i][j].getPiece()
.getPlayer().isBlack) {
Piece piece = board[i][j].getPiece();
if (piece.isValid(board, i, j, currentKingX, currentKingY)) {
System.out.println(piece.getType() + " KING STILL IN CHECK!");
return true;
}
}
}
}
System.out.println("KING NOT IN CHECK ANYMORE");
return false;
}
private void findKing(Square[][] board) {
if (!playerTurn) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j].getPiece() == null) {
continue;
}
if (board[i][j].getPiece().getPlayer().isBlack) {
if ("King".equals(board[i][j].getPiece().getType())) {
currentKingX = i;
currentKingY = j;
}
}
}
}
} else {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j].getPiece() == null) {
continue;
}
if (board[i][j].getPiece().getPlayer().isWhite) {
if ("King".equals(board[i][j].getPiece().getType())) {
currentKingX = i;
currentKingY = j;
}
}
}
}
}
}
public Square[][] getDummyBoard(int startRow,
int startCol, int finalRow, int finalCol) {
Square[][] board = new Square[8][8];
Player WHITE = new Player(true);
Player BLACK = new Player(false);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (gameBoard[i][j].getPiece() == null) {
board[i][j] = new Square(null);
} else if (gameBoard[i][j].getPiece().getPlayer().isWhite) {
if ("Pawn".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new Pawn(WHITE, "Pawn"));
} else if ("Rook".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new Rook(WHITE, "Rook"));
} else if ("Knight".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new Knight(WHITE, "Knight"));
} else if ("Bishop".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new Bishop(WHITE, "Bishop"));
} else if ("Queen".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new Queen(WHITE, "Queen"));
} else if ("King".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new King(WHITE, "King"));
}
} else {
if ("Pawn".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new Pawn(BLACK, "Pawn"));
} else if ("Rook".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new Rook(BLACK, "Rook"));
} else if ("Knight".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new Knight(BLACK, "Knight"));
} else if ("Bishop".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new Bishop(BLACK, "Bishop"));
} else if ("Queen".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new Queen(BLACK, "Queen"));
} else if ("King".equals(gameBoard[i][j].getPiece().getType())) {
board[i][j] = new Square(new King(BLACK, "King"));
}
}
}
}
Piece targetPiece = board[startRow][startCol].getPiece();
Square startSquare = board[startRow][startCol];
Square targetSquare = board[finalRow][finalCol];
Piece potentialOpponentPiece = targetSquare.getPiece();
if (potentialOpponentPiece != null) {
targetSquare.removePiece();
targetSquare.placePiece(targetPiece);
} else {
targetSquare.placePiece(targetPiece);
}
startSquare.removePiece();
return board;
}
}