-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathBoard.java
More file actions
251 lines (200 loc) · 7.86 KB
/
Board.java
File metadata and controls
251 lines (200 loc) · 7.86 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
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.LinkedList;
import java.util.List;
import javax.swing.*;
@SuppressWarnings("serial")
public class Board extends JPanel implements MouseListener, MouseMotionListener {
// Resource location constants for piece images
private static final String RESOURCES_WBISHOP_PNG = "wbishop.png";
private static final String RESOURCES_BBISHOP_PNG = "bbishop.png";
private static final String RESOURCES_WKNIGHT_PNG = "wknight.png";
private static final String RESOURCES_BKNIGHT_PNG = "bknight.png";
private static final String RESOURCES_WROOK_PNG = "wrook.png";
private static final String RESOURCES_BROOK_PNG = "brook.png";
private static final String RESOURCES_WKING_PNG = "wking.png";
private static final String RESOURCES_BKING_PNG = "bking.png";
private static final String RESOURCES_BQUEEN_PNG = "bqueen.png";
private static final String RESOURCES_WQUEEN_PNG = "wqueen.png";
private static final String RESOURCES_WPAWN_PNG = "wpawn.png";
private static final String RESOURCES_BPAWN_PNG = "bpawn.png";
// Logical and graphical representations of board
private final Square[][] board;
private final GameWindow g;
// List of pieces and whether they are movable
public final LinkedList<Piece> Bpieces;
public final LinkedList<Piece> Wpieces;
public List<Square> movable;
private boolean whiteTurn;
private Piece currPiece;
private int currX;
private int currY;
private CheckmateDetector cmd;
public Board(GameWindow g) {
this.g = g;
board = new Square[8][8];
Bpieces = new LinkedList<Piece>();
Wpieces = new LinkedList<Piece>();
setLayout(new GridLayout(8, 8, 0, 0));
this.addMouseListener(this);
this.addMouseMotionListener(this);
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
int xMod = x % 2;
int yMod = y % 2;
if ((xMod == 0 && yMod == 0) || (xMod == 1 && yMod == 1)) {
board[x][y] = new Square(this, 1, y, x);
this.add(board[x][y]);
} else {
board[x][y] = new Square(this, 0, y, x);
this.add(board[x][y]);
}
}
}
initializePieces();
this.setPreferredSize(new Dimension(400, 400));
this.setMaximumSize(new Dimension(400, 400));
this.setMinimumSize(this.getPreferredSize());
this.setSize(new Dimension(400, 400));
whiteTurn = true;
}
private void initializePieces() {
for (int x = 0; x < 8; x++) {
board[1][x].put(new Pawn(0, board[1][x], RESOURCES_BPAWN_PNG));
board[6][x].put(new Pawn(1, board[6][x], RESOURCES_WPAWN_PNG));
}
board[7][3].put(new Queen(1, board[7][3], RESOURCES_WQUEEN_PNG));
board[0][3].put(new Queen(0, board[0][3], RESOURCES_BQUEEN_PNG));
King bk = new King(0, board[0][4], RESOURCES_BKING_PNG);
King wk = new King(1, board[7][4], RESOURCES_WKING_PNG);
board[0][4].put(bk);
board[7][4].put(wk);
board[0][0].put(new Rook(0, board[0][0], RESOURCES_BROOK_PNG));
board[0][7].put(new Rook(0, board[0][7], RESOURCES_BROOK_PNG));
board[7][0].put(new Rook(1, board[7][0], RESOURCES_WROOK_PNG));
board[7][7].put(new Rook(1, board[7][7], RESOURCES_WROOK_PNG));
board[0][1].put(new Knight(0, board[0][1], RESOURCES_BKNIGHT_PNG));
board[0][6].put(new Knight(0, board[0][6], RESOURCES_BKNIGHT_PNG));
board[7][1].put(new Knight(1, board[7][1], RESOURCES_WKNIGHT_PNG));
board[7][6].put(new Knight(1, board[7][6], RESOURCES_WKNIGHT_PNG));
board[0][2].put(new Bishop(0, board[0][2], RESOURCES_BBISHOP_PNG));
board[0][5].put(new Bishop(0, board[0][5], RESOURCES_BBISHOP_PNG));
board[7][2].put(new Bishop(1, board[7][2], RESOURCES_WBISHOP_PNG));
board[7][5].put(new Bishop(1, board[7][5], RESOURCES_WBISHOP_PNG));
for(int y = 0; y < 2; y++) {
for (int x = 0; x < 8; x++) {
Bpieces.add(board[y][x].getOccupyingPiece());
Wpieces.add(board[7-y][x].getOccupyingPiece());
}
}
cmd = new CheckmateDetector(this, Wpieces, Bpieces, wk, bk);
}
public Square[][] getSquareArray() {
return this.board;
}
public boolean getTurn() {
return whiteTurn;
}
public void setCurrPiece(Piece p) {
this.currPiece = p;
}
public Piece getCurrPiece() {
return this.currPiece;
}
@Override
public void paintComponent(Graphics g) {
// super.paintComponent(g);
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
Square sq = board[y][x];
sq.paintComponent(g);
}
}
if (currPiece != null) {
if ((currPiece.getColor() == 1 && whiteTurn)
|| (currPiece.getColor() == 0 && !whiteTurn)) {
final Image i = currPiece.getImage();
g.drawImage(i, currX, currY, null);
}
}
}
@Override
public void mousePressed(MouseEvent e) {
currX = e.getX();
currY = e.getY();
Square sq = (Square) this.getComponentAt(new Point(e.getX(), e.getY()));
if (sq.isOccupied()) {
currPiece = sq.getOccupyingPiece();
if (currPiece.getColor() == 0 && whiteTurn)
return;
if (currPiece.getColor() == 1 && !whiteTurn)
return;
sq.setDisplay(false);
}
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
Square sq = (Square) this.getComponentAt(new Point(e.getX(), e.getY()));
if (currPiece != null) {
if (currPiece.getColor() == 0 && whiteTurn)
return;
if (currPiece.getColor() == 1 && !whiteTurn)
return;
List<Square> legalMoves = currPiece.getLegalMoves(this);
movable = cmd.getAllowableSquares(whiteTurn);
if (legalMoves.contains(sq) && movable.contains(sq)
&& cmd.testMove(currPiece, sq)) {
sq.setDisplay(true);
currPiece.move(sq);
cmd.update();
if (cmd.blackCheckMated()) {
currPiece = null;
repaint();
this.removeMouseListener(this);
this.removeMouseMotionListener(this);
g.checkmateOccurred(0);
} else if (cmd.whiteCheckMated()) {
currPiece = null;
repaint();
this.removeMouseListener(this);
this.removeMouseMotionListener(this);
g.checkmateOccurred(1);
} else {
currPiece = null;
whiteTurn = !whiteTurn;
movable = cmd.getAllowableSquares(whiteTurn);
}
} else {
currPiece.getPosition().setDisplay(true);
currPiece = null;
}
}
repaint();
}
@Override
public void mouseDragged(MouseEvent e) {
currX = e.getX() - 24;
currY = e.getY() - 24;
repaint();
}
// Irrelevant methods, do nothing for these mouse behaviors
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}