-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessBoardPanel.java
More file actions
328 lines (291 loc) · 11.4 KB
/
ChessBoardPanel.java
File metadata and controls
328 lines (291 loc) · 11.4 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
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
//CONNECTIONS ChessGame, ChessPiece, Player
public class ChessBoardPanel extends JPanel implements MouseListener {
private static final int SIZE = 8;
private static final int SQUARE_SIZE = 60;
private Image wRook, wKnight, wBishop, wQueen, wKing, wPawn;
private Image Rook, Knight, Bishop, Queen, King, Pawn;
private ChessBoard chessBoard;
private int selectedRow = -1;
private int selectedCol = -1;
private boolean pieceSelected = false;
private ChessPiece selectedPiece;
private boolean isWhiteTurn = true;
private ChessAI chessAI;
private Position position;
private ThreatState threatState;
public ChessBoardPanel(ChessAI chessAI) {
chessBoard = new ChessBoard();
threatState = new ThreatState();
loadImages();
addMouseListener(this);
this.chessAI = chessAI;
}
private void loadImages() {
try {
String basePath = "C:/Users/hecto/IdeaProjects/Chess/ChessPieces/";
wRook = ImageIO.read(new File(basePath + "wRook.png"));
wKnight = ImageIO.read(new File(basePath + "wKnight.png"));
wBishop = ImageIO.read(new File(basePath + "wBishop.png"));
wQueen = ImageIO.read(new File(basePath + "wQueen.png"));
wKing = ImageIO.read(new File(basePath + "wKing.png"));
wPawn = ImageIO.read(new File(basePath + "wPawn.png"));
Rook = ImageIO.read(new File(basePath + "rook.png"));
Knight = ImageIO.read(new File(basePath + "knight.png"));
Bishop = ImageIO.read(new File(basePath + "bishop.png"));
Queen = ImageIO.read(new File(basePath + "queen.png"));
King = ImageIO.read(new File(basePath + "king.png"));
Pawn = ImageIO.read(new File(basePath + "pawn.png"));
} catch (IOException e) {
e.printStackTrace();
// Handle the error appropriately
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawBoard(g);
initializeBoardPieces(g);
if (pieceSelected) {
highlightTile(g, selectedRow, selectedCol);
drawSelectedPiece(g, selectedRow, selectedCol);
}
}
private void highlightTile(Graphics g, int row, int col) {
g.setColor(new Color(255, 255, 0, 128)); // Semi-transparent yellow, for example
g.fillRect(col * SQUARE_SIZE, row * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE);
}
private void drawSelectedPiece(Graphics g, int row, int col) {
ChessPiece piece = chessBoard.getPiece(row,col);
if (piece != null) {
//Image pieceImage = // get the image for this piece
// Draw the piece larger or with a border
// Adjust the x, y, width, and height as needed for the larger size
//g.drawImage(pieceImage, col * SQUARE_SIZE, row * SQUARE_SIZE, largerWidth, largerHeight, this);
}
}
private void drawBoard(Graphics g) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if ((i + j) % 2 == 0) {
g.setColor(Color.WHITE);
} else {
g.setColor(Color.GRAY);
}
g.fillRect(j * SQUARE_SIZE, i * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE);
}
}
}
private void initializeBoardPieces(Graphics g) {
// Drawing Pawns
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
ChessPiece piece = chessBoard.getPiece(i, j);
if (piece != null) {
// Draw the piece. You need to determine which image to use based on the piece type and color.
Image pieceImage = getPieceImage(piece);
g.drawImage(pieceImage, j * SQUARE_SIZE, i * SQUARE_SIZE, this);
}
}
}
}
private Image getPieceImage(ChessPiece piece) {
//White pawn
if (piece.getColor().equals("White")){
if (piece.getType(piece).equals("Pawn")){
return wPawn;
}
else if (piece.getType(piece).equals("Rook")){
return wRook;
}
else if (piece.getType(piece).equals("Knight")){
return wKnight;
}
else if (piece.getType(piece).equals("Bishop")){
return wBishop;
}
else if (piece.getType(piece).equals("King")){
return wKing;
}
else if (piece.getType(piece).equals("Queen")){
return wQueen;
}
}
else {
if (piece.getType(piece).equals("Pawn")){
return Pawn;
}
else if (piece.getType(piece).equals("Rook")){
return Rook;
}
else if (piece.getType(piece).equals("Knight")){
return Knight;
}
else if (piece.getType(piece).equals("Bishop")){
return Bishop;
}
else if (piece.getType(piece).equals("King")){
return King;
}
else if (piece.getType(piece).equals("Queen")){
return Queen;
}
}
return null;
}
public void mouseClicked(MouseEvent e) {
if (isWhiteTurn) {
int x = e.getX(); // Get the x-coordinate of the mouse click
int y = e.getY(); // Get the y-coordinate of the mouse click
// Calculate which square is clicked
int row = y / SQUARE_SIZE;
int col = x / SQUARE_SIZE;
String pos = squareToLetter(col, row);
System.out.println("Square Selected " + pos);
//ChessPiece tempPiece = chessBoard.getPiece(row, col);
if (selectedPiece != null) {
// VALID MOVE
if (selectedPiece.isValidMove(selectedRow, selectedCol, row, col, chessBoard.getBoard())) {
chessBoard.movePiece(selectedRow, selectedCol, row, col);
//System.out.println("Checking for check/checkmate opponent color: " + " Black");
if (GameRules.isKingInCheck(chessBoard, "Black")) {
JOptionPane.showMessageDialog(this, "Black" + " is in Check!");
chessAI.setInCheck(true);
// GameRules.isKingInCheckmate(selectedPiece,chessBoard,row,col, );
}
pieceSelected = false;
selectedPiece = null;
isWhiteTurn = false;
// }
}
//NOT A VALID MOVE FOR PIECE
else {
//System.out.println("Not a valid move");
pieceSelected = false;
selectedPiece = null;
}
} else {
// Check if the clicked square contains a piece of the current player's color
ChessPiece clickedPiece = chessBoard.getPiece(row, col);
if (clickedPiece != null && clickedPiece.getColor().equals("White")) {
// Only select the piece if it's the same color as the current player's turn
selectedPiece = clickedPiece;
selectedRow = row;
selectedCol = col;
pieceSelected = true;
} else {
//System.out.println("Empty space or opponents color");
}
}
repaint();
if (!isWhiteTurn){
completePlayerMove();
}
}
}
public void completePlayerMove() {
System.out.println();
System.out.println();
System.out.println("\n-------WHITE's TURN OVER-------\n");
System.out.println();
System.out.println();
System.out.println("\n-------BLACK's TURN-------\n");
if (!isWhiteTurn) {
Move tempMove = chessAI.makeMove(chessBoard);
//Move tempMove = chessAI.;
int r,c;
c = tempMove.getNewX();
r = tempMove.getNewY();
//Move the black piece
chessAI.executeMove(tempMove, chessBoard);
List<Move> movesList = chessAI.returnAllLegalMoves(chessBoard);
ChessPiece tempPiece = chessBoard.getPiece(r,c);
if (GameRules.isKingInCheck(chessBoard, "White")) {
JOptionPane.showMessageDialog(this, "White is in Check!" + chessBoard.getPiece(r,c));
System.out.println("Opponent Piece: " + tempMove);
if (GameRules.isKingInCheckmate(tempPiece,chessBoard,r,c, movesList)){
JOptionPane.showMessageDialog(null, "WHITE" + " is in checkmate. Game over.");
int playAgain = JOptionPane.showConfirmDialog(null, "Play again?", "Game Over", JOptionPane.YES_NO_OPTION);
if (playAgain == JOptionPane.YES_OPTION) {
resetGame();
} else {
System.exit(0); // or close the game window
}
}
}
isWhiteTurn = true; // Switch back to player's turn
String opponentColor = isWhiteTurn ? "White" : "Black";
repaint();
System.out.println("\n-------BLACK's TURN OVER-------\n");
System.out.println("\n-------WHITE's TURN-------\n");
}
}
private void checkGameState(String opponentColor) {
System.out.println("Checking for check/checkmate opponent color: " + opponentColor);
// JOptionPane.showMessageDialog(this, opponentColor + " is in Check!");
// }
}
public void resetGame() {
// Reset the chess board to its initial state
chessBoard.initializeBoard();
// Reset any other state variables, like whose turn it is
isWhiteTurn = true;
selectedPiece = null;
pieceSelected = false;
repaint();
}
// Other required methods of MouseListener (empty implementations if not used)
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
public String squareToLetter(int col, int row) {
String loc = "";
// Inverting the row number
int invertedRow = 7 - row;
switch (col) {
case 0:
loc = "A";
break;
case 1:
loc = "B";
break;
case 2:
loc = "C";
break;
case 3:
loc = "D";
break;
case 4:
loc = "E";
break;
case 5:
loc = "F";
break;
case 6:
loc = "G";
break;
case 7:
loc = "H";
break;
case 8:
loc = "I";
break;
default:
break;
}
loc += (invertedRow + 1);
return loc;
}
}