-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessGUI.java
More file actions
124 lines (113 loc) · 5.24 KB
/
Copy pathChessGUI.java
File metadata and controls
124 lines (113 loc) · 5.24 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* ChessGUI.java
* A Swing-based graphical interface for the console Chess game.
* Uses the existing Board and Piece classes to render the game and handle moves.
*/
public class ChessGUI extends JFrame {
private int selectedRow = -1, selectedCol = -1;
private final int squareSize = 80;
public ChessGUI() {
// Initialize the game board
Board.setBoard();
setTitle("Chess");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BoardPanel panel = new BoardPanel();
panel.setPreferredSize(new Dimension(8 * squareSize, 8 * squareSize));
add(panel);
// Handle mouse clicks for selecting and moving pieces
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int col = e.getX() / squareSize;
int row = e.getY() / squareSize;
if (selectedRow == -1 && selectedCol == -1) {
// First click: select a piece if one exists
Piece p = Board.getPiece(col, row);
if (p != null) {
selectedRow = row;
selectedCol = col;
panel.repaint();
}
} else {
// Second click: attempt move
Piece p = Board.getPiece(selectedCol, selectedRow);
if (p != null) {
String move = p.getColour().toString() + " "
+ p.getID() + " "
+ (row + 1) + " "
+ (col + 1);
if (Board.checkValidMove(move)) {
Board.executeMove(move);
if (Board.inCheck()) {
if (Board.isCheckmate()) {
JOptionPane.showMessageDialog(panel,
"Checkmate! " + (p.getColour() == Colour.WHITE ? "White" : "Black") + " wins.");
} else {
JOptionPane.showMessageDialog(panel, "King is in check!");
}
}
} else {
JOptionPane.showMessageDialog(panel, "Illegal move.");
}
}
// Reset selection and repaint
selectedRow = -1;
selectedCol = -1;
panel.repaint();
}
}
});
pack();
setLocationRelativeTo(null);
setVisible(true);
}
// Panel that draws the chess board and pieces
private class BoardPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw squares and pieces
for (int r = 0; r < 8; r++) {
for (int c = 0; c < 8; c++) {
boolean light = (r + c) % 2 == 0;
g.setColor(light ? Color.LIGHT_GRAY : Color.DARK_GRAY);
g.fillRect(c * squareSize, r * squareSize, squareSize, squareSize);
Piece p = Board.getPiece(c, r);
if (p != null) {
String label = getUnicodeForPiece(p);
g.setColor(p.getColour() == Colour.WHITE ? Color.WHITE : Color.BLACK);
g.setFont(new Font("SansSerif", Font.PLAIN, squareSize - 20));
FontMetrics fm = g.getFontMetrics();
int textWidth = fm.stringWidth(label);
int textHeight = fm.getAscent();
int x = c * squareSize + (squareSize - textWidth) / 2;
int y = r * squareSize + (squareSize + textHeight) / 2;
g.drawString(label, x, y);
}
}
}
// Highlight selected square
if (selectedRow != -1) {
g.setColor(Color.RED);
g.drawRect(selectedCol * squareSize, selectedRow * squareSize, squareSize, squareSize);
g.drawRect(selectedCol * squareSize + 1, selectedRow * squareSize + 1, squareSize - 2, squareSize - 2);
}
}
// Maps Piece instances to Unicode chess characters
private String getUnicodeForPiece(Piece p) {
if (p instanceof King) return p.getColour() == Colour.WHITE ? "\u2654" : "\u265A";
if (p instanceof Queen) return p.getColour() == Colour.WHITE ? "\u2655" : "\u265B";
if (p instanceof Rook) return p.getColour() == Colour.WHITE ? "\u2656" : "\u265C";
if (p instanceof Bishop) return p.getColour() == Colour.WHITE ? "\u2657" : "\u265D";
if (p instanceof Knight) return p.getColour() == Colour.WHITE ? "\u2658" : "\u265E";
if (p instanceof Pawn) return p.getColour() == Colour.WHITE ? "\u2659" : "\u265F";
return "";
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(ChessGUI::new);
}
}