-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChessPanel.java
289 lines (254 loc) · 8.81 KB
/
ChessPanel.java
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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ChessPanel extends JPanel {
public enum GUIcodes {
NoMessage, NotYourTurn, inCheck, Checkmate
}
private JButton[][] board;
private ChessModel model;
private ImageIcon bRook;
private ImageIcon bBishop;
private ImageIcon bQueen;
private ImageIcon bKing;
private ImageIcon bPawn;
private ImageIcon bKnight;
private ImageIcon wRook;
private ImageIcon wBishop;
private ImageIcon wQueen;
private ImageIcon wKing;
private ImageIcon wPawn;
private ImageIcon wKnight;
private GUIcodes messageCode;
// you can declare other instance variables as needed
private listener listener;
public ChessPanel() {
messageCode = GUIcodes.NoMessage;
model = new ChessModel();
board = new JButton[model.numRows()][model.numColumns()];
listener = new listener();
createIcons();
JPanel boardpanel = new JPanel();
boardpanel.setLayout(new GridLayout(model.numRows(), model.numColumns(), 1, 1));
for (int r = 0; r < model.numRows(); r++) {
for (int c = 0; c < model.numColumns(); c++) {
if (model.pieceAt(r, c) == null) {
board[r][c] = new JButton("", null);
board[r][c].addActionListener(listener);
} else if (model.pieceAt(r, c).player() == Player.WHITE)
placeWhitePieces(r, c);
else if (model.pieceAt(r, c).player() == Player.BLACK) {
placeBlackPieces(r, c);
}
setBackGroundColor(r, c);
boardpanel.add(board[r][c]);
}
}
add(boardpanel, BorderLayout.WEST);
boardpanel.setPreferredSize(new Dimension(600, 600));
}
private void setBackGroundColor(int r, int c) {
if ((c % 2 == 1 && r % 2 == 0) || (c % 2 == 0 && r % 2 == 1)) {
board[r][c].setBackground(Color.LIGHT_GRAY);
} else if ((c % 2 == 0 && r % 2 == 0) || (c % 2 == 1 && r % 2 == 1)) {
board[r][c].setBackground(Color.WHITE);
}
}
private void placeBlackPieces(int r, int c) {
if (model.pieceAt(r, c).type().equals("Pawn")) {
board[r][c] = new JButton(null, bPawn);
board[r][c].addActionListener(listener);
}
if (model.pieceAt(r, c).type().equals("Rook")) {
board[r][c] = new JButton(null, bRook);
board[r][c].addActionListener(listener);
}
if (model.pieceAt(r, c).type().equals("Knight")) {
board[r][c] = new JButton(null, bKnight);
board[r][c].addActionListener(listener);
}
if (model.pieceAt(r, c).type().equals("Bishop")) {
board[r][c] = new JButton(null, bBishop);
board[r][c].addActionListener(listener);
}
if (model.pieceAt(r, c).type().equals("Queen")) {
board[r][c] = new JButton(null, bQueen);
board[r][c].addActionListener(listener);
}
if (model.pieceAt(r, c).type().equals("King")) {
board[r][c] = new JButton(null, bKing);
board[r][c].addActionListener(listener);
}
}
private void placeWhitePieces(int r, int c) {
if (model.pieceAt(r, c).type().equals("Pawn")) {
board[r][c] = new JButton(null, wPawn);
board[r][c].addActionListener(listener);
}
if (model.pieceAt(r, c).type().equals("Rook")) {
board[r][c] = new JButton(null, wRook);
board[r][c].addActionListener(listener);
}
if (model.pieceAt(r, c).type().equals("Knight")) {
board[r][c] = new JButton(null, wKnight);
board[r][c].addActionListener(listener);
}
if (model.pieceAt(r, c).type().equals("Bishop")) {
board[r][c] = new JButton(null, wBishop);
board[r][c].addActionListener(listener);
}
if (model.pieceAt(r, c).type().equals("Queen")) {
board[r][c] = new JButton(null, wQueen);
board[r][c].addActionListener(listener);
}
if (model.pieceAt(r, c).type().equals("King")) {
board[r][c] = new JButton(null, wKing);
board[r][c].addActionListener(listener);
}
}
private void createIcons() {
// Sets the ImageIcon for the Black Player Pieces
bRook = new ImageIcon("./src/bRook.png");
bBishop = new ImageIcon("./src/bBishop.png");
bQueen = new ImageIcon("./src/bQueen.png");
bKing = new ImageIcon("./src/bKing.png");
bPawn = new ImageIcon("./src/bPawn.png");
bKnight = new ImageIcon("./src/bKnight.png");
// Sets the Image for white player pieces
wRook = new ImageIcon("./src/wRook.png");
wBishop = new ImageIcon("./src/wBishop.png");
wQueen = new ImageIcon("./src/wQueen.png");
wKing = new ImageIcon("./src/wKing.png");
wPawn = new ImageIcon("./src/wPawn.png");
wKnight = new ImageIcon("./src/wKnight.png");
}
// method that updates the board
private void displayBoard() {
for (int r = 0; r < 8; r++) {
for (int c = 0; c < 8; c++)
if (model.pieceAt(r, c) == null)
board[r][c].setIcon(null);
else
if (model.pieceAt(r, c).player() == Player.WHITE) {
if (model.pieceAt(r, c).type().equals("Pawn"))
board[r][c].setIcon(wPawn);
if (model.pieceAt(r, c).type().equals("Rook"))
board[r][c].setIcon(wRook);
if (model.pieceAt(r, c).type().equals("Knight"))
board[r][c].setIcon(wKnight);
if (model.pieceAt(r, c).type().equals("Bishop"))
board[r][c].setIcon(wBishop);
if (model.pieceAt(r, c).type().equals("Queen"))
board[r][c].setIcon(wQueen);
if (model.pieceAt(r, c).type().equals("King"))
board[r][c].setIcon(wKing);
} else
if (model.pieceAt(r, c).player() == Player.BLACK) {
if (model.pieceAt(r, c).type().equals("Pawn"))
board[r][c].setIcon(bPawn);
if (model.pieceAt(r, c).type().equals("Rook"))
board[r][c].setIcon(bRook);
if (model.pieceAt(r, c).type().equals("Knight"))
board[r][c].setIcon(bKnight);
if (model.pieceAt(r, c).type().equals("Bishop"))
board[r][c].setIcon(bBishop);
if (model.pieceAt(r, c).type().equals("Queen"))
board[r][c].setIcon(bQueen);
if (model.pieceAt(r, c).type().equals("King"))
board[r][c].setIcon(bKing);
}
}
repaint();
}
// inner class that represents action listener for buttons
private class listener implements ActionListener {
boolean pieceChosen = false;
int fromRow = -1;
int fromColumn = -1;
int toRow = -1;
int toColumn = -1;
Move tempMove;
Color tempColor;
public void actionPerformed(ActionEvent event) {
for (int r = 0; r < model.numRows(); r++) {
for (int c = 0; c < model.numColumns(); c++) {
if (board[r][c] == event.getSource()) {
if (!pieceChosen) {
if (model.pieceAt(r, c) != null)
if (model.pieceAt(r, c).player() == model.currentPlayer()) {
selectPiece(r, c);
} else {
messageCode = GUIcodes.NotYourTurn;
displayMessage();
}
}
else
if (pieceChosen) {
tryMoveTo(r, c);
}
}
}
}
}
private void tryMoveTo(int r, int c) {
board[fromRow][fromColumn].setBackground(tempColor);
toRow = r;
toColumn = c;
tempMove = new Move(fromRow, fromColumn, toRow, toColumn);
if (model.isValidMove(tempMove)) {
model.move(tempMove);
//model.setNextPlayer();//NEED TO DISABLE
if (model.inCheck(model.currentPlayer())) {
messageCode = GUIcodes.inCheck;
if (model.isComplete())
messageCode = GUIcodes.Checkmate;
}
displayBoard();
}
pieceChosen = false;
displayMessage();
// After implementing AI method in ChessModel.java, uncomment to turn on AI
//AITurn();
}
public void AITurn() {
if (model.currentPlayer() == Player.BLACK) {
model.AI();
messageCode = GUIcodes.NoMessage;
model.setNextPlayer();
displayBoard();
if (model.inCheck(model.currentPlayer())) {
messageCode = GUIcodes.inCheck;
if (model.isComplete())
messageCode = GUIcodes.Checkmate;
}
displayBoard();
pieceChosen = false;
displayMessage();
}
}
private void selectPiece(int r, int c) {
tempColor = board[r][c].getBackground();
board[r][c].setBackground(Color.CYAN);
fromRow = r;
fromColumn = c;
messageCode = GUIcodes.NoMessage;
pieceChosen = true;
}
private void displayMessage() {
if (messageCode == GUIcodes.NotYourTurn) {
JOptionPane.showConfirmDialog(null, "It is not your turn.",
"Warning", JOptionPane.CLOSED_OPTION);
}
// TODO: add other code here to display other dialogs
//if (messageCode == GUIcodes.Checkmate) {
// JOptionPane.showConfirmDialog(null, "Checkmate", "Game Over", JOptionPane.CLOSED_OPTION);
//}
//if (messageCode == GUIcodes.inCheck) {
// JOptionPane.showConfirmDialog(null, "You're in Check", "Warning", JOptionPane.CLOSED_OPTION);
//}
//if (messageCode == GUIcodes.NoMessage) {
// JOptionPane.showConfirmDialog(null, "Invalid move", "Warning", JOptionPane.CLOSED_OPTION);
//}
}
}
}