-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI.java
193 lines (172 loc) · 7.38 KB
/
GUI.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
// chess pieces - https://commons.wikimedia.org/wiki/Category:PNG_chess_pieces/Standard_transparent
// chess article - https://hackr.io/blog/how-to-build-a-java-chess-game-app
public class GUI extends JFrame {
private Square[][] squares = new Square[8][8];
private Game game = new Game();
public GUI() {
setTitle("Chess Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(8, 8));
initializeBoard();
pack(); // Adjusts window size to fit the chessboard
setLocationRelativeTo(null);
setVisible(true);
}
public String getFile(String c, String type) {
return "images/" + c + "_" + type + ".png";
}
private void initializeBoard() {
for (int i=0;i<squares.length;i++) {
for (int j=0;j<squares[i].length;j++) {
final int r = i;
final int c = j;
Square s = new Square(r, c);
s.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (game.handleSquareSelection(r, c)) {
refreshBoard();
checkGameState();
checkGameOver();
}
}
});
add(s);
squares[r][c] = s;
}
}
refreshBoard();
}
// sets each square to the correct image based on the type and color
private void refreshBoard() {
// System.out.println("in refresh board");
Board board = game.getBoard();
for (int row = 0; row < 8; row++){
for (int col = 0; col < 8; col++){
Piece piece = board.getPiece(row, col);
if (piece != null){
String type = piece.getType();
String color = piece.getColor();
String filepath = getFile(color, type);
squares[row][col].setPieceSymbol(filepath);
} else {
squares[row][col].clearPiece();
}
}
}
}
private void checkGameState() {
String player = game.getTurn(); // This method should return the current player's color
if (game.isInCheck(player)) {
if (game.isInCheckmate(player)){
JOptionPane.showMessageDialog(this, player + " is in checkmate!");
}
else
JOptionPane.showMessageDialog(this, player + " is in check!");
}
for (int j=0;j<8;j++) {
if (game.getBoard().getPiece(0,j) != null && game.getBoard().getPiece(0, j).getType().equals("Pawn") && game.getBoard().getPiece(0, j).getColor().equals("white")) {
String[] options = {"Queen", "Rook", "Bishop", "Knight"};
int response = JOptionPane.showOptionDialog(
this, // Parent component; 'this' can be used if inside an instance method
"What piece would you like to promote to?", // Message
"Pawn Promotion", // Title
JOptionPane.DEFAULT_OPTION, // Option type
JOptionPane.QUESTION_MESSAGE, // Message type
null, // Icon; use default icon
options, // Options array
options[0] // Initial value
);
if (response >= 0) {
String selectedPiece = options[response];
// System.out.println("User choice for promotion: " + selectedPiece);
int[] coords = {0,j};
switch (selectedPiece) {
case "Queen":
game.promote(coords, new Queen(0,j,"white"));
refreshBoard();
break;
case "Rook":
game.promote(coords, new Rook(0, j, "white"));
refreshBoard();
break;
case "Bishop":
game.promote(coords, new Bishop(0, j,"white"));
refreshBoard();
break;
case "Knight":
game.promote(coords, new Knight(0, j, "white"));
refreshBoard();
break;
}
}
}
}
for (int j=0;j<8;j++) {
if (game.getBoard().getPiece(7,j) != null && game.getBoard().getPiece(7, j).getType().equals("Pawn") && game.getBoard().getPiece(7, j).getColor().equals("black")) {
String[] options = {"Queen", "Rook", "Bishop", "Knight"};
int response = JOptionPane.showOptionDialog(
this, // Parent component; 'this' can be used if inside an instance method
"What piece would you like to promote to?", // Message
"Pawn Promotion", // Title
JOptionPane.DEFAULT_OPTION, // Option type
JOptionPane.QUESTION_MESSAGE, // Message type
null, // Icon; use default icon
options, // Options array
options[0] // Initial value
);
if (response >= 0) {
String selectedPiece = options[response];
// System.out.println("User choice for promotion: " + selectedPiece);
int[] coords = {7,j};
switch (selectedPiece) {
case "Queen":
game.promote(coords, new Queen(7,j,"black"));
refreshBoard();
break;
case "Rook":
game.promote(coords, new Rook(7, j, "black"));
refreshBoard();
break;
case "Bishop":
game.promote(coords, new Bishop(7, j,"black"));
refreshBoard();
break;
case "Knight":
game.promote(coords, new Knight(7, j, "black"));
refreshBoard();
break;
}
}
}
}
}
private void checkGameOver() {
if (game.getIsCheckmate()) {
int response = JOptionPane.showConfirmDialog(this, "Checkmate! Would you like to play again?", "Game Over", JOptionPane.YES_NO_OPTION);
if (response == JOptionPane.YES_OPTION) {
String[] args = {"hey"};
main(args);
} else {
System.exit(0);
}
}
}
private static void closeAllDialogs(){
Window[] windows = getWindows();
for (Window window : windows)
{
if (window instanceof JFrame)
{
window.dispose();
}
}
}
public static void main(String[] args) {
closeAllDialogs();
new GUI();
}
}