-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
223 lines (202 loc) · 8.54 KB
/
Board.java
File metadata and controls
223 lines (202 loc) · 8.54 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Board extends JFrame implements ActionListener
{
JButton[][] buttons = new JButton[8][8];
Piece[][] boardPieces = new Piece[8][8];
int selRow = -1;
int selCol = -1;
Player whitePlay, blackPlay;
Player player;
Board()
{
super("CHESS BY RIDDHIMA BISHT");
setSize(600, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridLayout(8, 8));
for (int row = 0; row < 8; row++)
{
for (int col = 0; col < 8; col++)
{
buttons[row][col] = new JButton();
buttons[row][col].setFont(new Font("Noto Sans Symbols", Font.PLAIN, 36));
if ((row + col) % 2 == 0) buttons[row][col].setBackground(Color.WHITE);
else buttons[row][col].setBackground(Color.LIGHT_GRAY);
buttons[row][col].addActionListener(this);
add(buttons[row][col]);
}
}
whitePlay = new Player("Player1", "white");
blackPlay = new Player("Player2", "black");
player = whitePlay;
for (int col = 0; col < 8; col++) {
boardPieces[6][col] = new Pawn("white", 6, col);
boardPieces[1][col] = new Pawn("black", 1, col);
}
boardPieces[7][4] = new King("white", 7, 4);
boardPieces[0][4] = new King("black", 0, 4);
boardPieces[7][3] = new Queen("white", 7, 3);
boardPieces[0][3] = new Queen("black", 0, 3);
boardPieces[7][0] = new Rook("white", 7, 0);
boardPieces[7][7] = new Rook("white", 7, 7);
boardPieces[0][0] = new Rook("black", 0, 0);
boardPieces[0][7] = new Rook("black", 0, 7);
boardPieces[0][1] = new Knight("black", 0, 1);
boardPieces[0][6] = new Knight("black", 0, 6);
boardPieces[7][1] = new Knight("white", 7, 1);
boardPieces[7][6] = new Knight("white", 7, 6);
boardPieces[0][2] = new Bishop("black", 0, 2);
boardPieces[0][5] = new Bishop("black", 0, 5);
boardPieces[7][2] = new Bishop("white", 7, 2);
boardPieces[7][5] = new Bishop("white", 7, 5);
update();
setVisible(true);
}
int lastRow = -999;
int lastCol = -999;
public void actionPerformed(ActionEvent e)
{
for (int row = 0; row < 8; row++)
{
for (int col = 0; col < 8; col++)
{
if (e.getSource() == buttons[row][col])
{
if (selRow == -1 && selCol == -1)
{
Piece selected = boardPieces[row][col];
if (selected != null && selected.getColor().equals(player.getColor()))
{
selRow = row;
selCol = col;
buttons[row][col].setBackground(Color.YELLOW); // highlight the selected piece
}
}
else
{
Piece selected = boardPieces[selRow][selCol];
if (selected != null && selected.isValidMove(row, col, boardPieces))
{
// Move is valid
Piece target = boardPieces[row][col];
if (target != null && !target.getColor().equals(player.getColor()))
{
player.capturePiece(target);
buttons[row][col].setBackground(Color.RED); // highlight capture
lastCol = col;
lastRow = row;
// Show captured pieces popup
showCapturedPieces();
}
boardPieces[row][col] = selected;
boardPieces[selRow][selCol] = null;
selected.setPosition(row, col);
if (isGameOver())
{
JOptionPane.showMessageDialog(this, "Game Over! " + player.getName() + " wins!");
System.exit(0); // or disable the board instead of exiting
}
// Switch turn
switchTurn();
}
else if (selected != null)
JOptionPane.showMessageDialog(this, "Illegal move! Try again.","Invalid Move",JOptionPane.ERROR_MESSAGE);
// Reset selection
resetButtonColor(selRow, selCol);
selRow = selCol = -1;
update();
}
return;
}
}
}
}
void resetButtonColor(int row, int col)
{
// Reset the previously selected square
if ((row + col) % 2 == 0)
buttons[row][col].setBackground(Color.WHITE);
else
buttons[row][col].setBackground(Color.LIGHT_GRAY);
// Reset the last captured square (if any)
if (lastRow != -999 && lastCol != -999) {
if ((lastRow + lastCol) % 2 == 0)
buttons[lastRow][lastCol].setBackground(Color.WHITE);
else
buttons[lastRow][lastCol].setBackground(Color.LIGHT_GRAY);
}
// Clear last captured markers
lastRow = lastCol = -999;
}
void update()
{
for (int row = 0; row < 8; row++)
{
for (int col = 0; col < 8; col++)
{
if (boardPieces[row][col] != null)
buttons[row][col].setText(boardPieces[row][col].getSymbol());
else
buttons[row][col].setText("");
}
}
}
public void switchTurn()
{
player = (player == whitePlay) ? blackPlay : whitePlay;
setTitle("Chess -> : " + player.getColor() + "'s turn");
showTurnPopup(player.getColor() + "'s turn!");
}
public void showTurnPopup(String message)
{
// Create a non-modal dialog
JDialog dialog = new JDialog(this, "Turn Switched", false);
dialog.setUndecorated(true); // no close/minimize buttons
dialog.setLayout(new BorderLayout());
JLabel label = new JLabel(message, SwingConstants.CENTER);
label.setFont(new Font("Arial", Font.BOLD, 18));
dialog.add(label, BorderLayout.CENTER);
dialog.setSize(200, 100);
dialog.setLocationRelativeTo(this); // center on main window
dialog.setVisible(true);
new javax.swing.Timer(500, e -> dialog.dispose()).start();
}
public void showCapturedPieces()
{
JDialog dialog = new JDialog(this, "Captured Pieces", false);
dialog.setLayout(new GridLayout(2, 1));
StringBuilder whiteCaptured = new StringBuilder("White captured: ");
for (Piece p : whitePlay.getCapturedPieces())
whiteCaptured.append(p.getSymbol()).append(" ");
JLabel whiteLabel = new JLabel(whiteCaptured.toString(), SwingConstants.CENTER);
StringBuilder blackCaptured = new StringBuilder("Black captured: ");
for (Piece p : blackPlay.getCapturedPieces()) {
blackCaptured.append(p.getSymbol()).append(" ");
}
JLabel blackLabel = new JLabel(blackCaptured.toString(), SwingConstants.CENTER);
dialog.add(whiteLabel);
dialog.add(blackLabel);
dialog.setSize(400, 150);
dialog.setLocationRelativeTo(this);
dialog.setVisible(true);
javax.swing.Timer t = new javax.swing.Timer(1000, e -> dialog.dispose());
t.setRepeats(false);
t.start();
}
public boolean isKingCaptured(String color) {
for (int r = 0; r < 8; r++)
{
for (int c = 0; c < 8; c++)
{
Piece p = boardPieces[r][c];
if (p != null && p instanceof King && p.getColor().equals(color))
return false;
}
}
return true; // King not found -> captured
}
public boolean isGameOver() {
return isKingCaptured("white") || isKingCaptured("black");
}
}