-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMastermindBoardGUI.java
More file actions
263 lines (228 loc) · 9.78 KB
/
MastermindBoardGUI.java
File metadata and controls
263 lines (228 loc) · 9.78 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.util.Arrays;
/**
* MastermindBoardGUI: Interactive interface using Java Swing.
* Connects the GraphBFSNavigator (AI) and MastermindEngine (Logic).
*/
public class MastermindBoardGUI extends JPanel {
// --- UI Scaling Constants ---
private static final int PEG_SZ = 32;
private static final double PEG_GAP = 0.10;
private static final double PEG_HINT = 0.35;
private static final double BORDER_MUL = 0.5;
private final int gapSz = (int) (PEG_SZ * PEG_GAP);
private final int hintSz = (int) (PEG_SZ * PEG_HINT);
private final int border = (int) (PEG_SZ * BORDER_MUL);
private final int pegOff = PEG_SZ + gapSz;
// --- Core Logic and State (Renamed) ---
private MastermindEngine engine; // Formerly MastermindGame
private GraphBFSNavigator navigator; // Formerly MastermindSolver
private int[][] history;
private int[][] feedbackHistory;
private int[] currentGuess = new int[MastermindEngine.SLOTS];
private int nextGo = 0;
private boolean solved = false;
private boolean revealSolution = false;
private boolean isHumanTurn = true;
private int dragColor = -1;
public MastermindBoardGUI() {
initGameSystem();
setBackground(new Color(224, 224, 224));
setPreferredSize(calculateBoardSize());
initInputListeners();
}
private void initGameSystem() {
this.engine = new MastermindEngine();
this.navigator = new GraphBFSNavigator(engine);
this.history = new int[10][MastermindEngine.SLOTS];
this.feedbackHistory = new int[10][2];
for (int[] row : history)
Arrays.fill(row, -1);
Arrays.fill(currentGuess, -1);
this.nextGo = 0;
this.solved = false;
this.revealSolution = false;
this.isHumanTurn = true;
}
private Dimension calculateBoardSize() {
int hintW = (MastermindEngine.SLOTS + 1) / 2;
int w = (int) (PEG_SZ * (BORDER_MUL * 2 + 2.0 + MastermindEngine.SLOTS + (PEG_GAP * MastermindEngine.SLOTS)
+ (PEG_HINT * hintW + 1)));
int h = (int) (PEG_SZ * (BORDER_MUL * 2 + (10 + 2) + (PEG_GAP * (10 + 1))));
return new Dimension(w, h);
}
/**
* Cooperative logic using the Graph-based AI.
*/
private void submitGuess() {
if (nextGo >= 10 || solved)
return;
// 1. Evaluate current move using Engine
int[] fb = engine.evaluateGuess(currentGuess, engine.getSecret());
feedbackHistory[nextGo] = fb;
history[nextGo] = currentGuess.clone();
// 2. PRUNE the Graph and SORT remaining nodes
navigator.traverseToNextState(currentGuess, fb);
if (fb[0] == MastermindEngine.SLOTS) {
solved = true;
revealSolution = true;
String winner = isHumanTurn ? "Human" : "Bot";
repaint();
JOptionPane.showMessageDialog(this, "Goal State reached by " + winner + "!");
} else {
nextGo++;
isHumanTurn = !isHumanTurn;
// 3. Trigger Bot using GREEDY selection from BFS Navigator
if (!isHumanTurn) {
Timer botTimer = new Timer(800, e -> {
int[] botMove = navigator.getImprovedGreedyMove();
if (botMove != null) {
currentGuess = botMove.clone();
submitGuess();
}
});
botTimer.setRepeats(false);
botTimer.start();
}
}
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int guessX = border + PEG_SZ * 2;
int guessY = border;
// Draw Palette (Vertices Colors)
for (int i = 0; i < MastermindEngine.COLORS; i++) {
renderPeg(g2, border, guessY + (i * pegOff), i, PEG_SZ);
}
// Draw Search History (Graph Path)
for (int r = 0; r < 10; r++) {
int rowY = guessY + (r * pegOff);
if (r == nextGo && !solved) {
g2.setColor(new Color(255, 128, 128));
g2.fillRect(guessX - 5, rowY, 2, PEG_SZ);
}
for (int p = 0; p < MastermindEngine.SLOTS; p++) {
int col = (r == nextGo) ? currentGuess[p] : history[r][p];
renderPeg(g2, guessX + (p * pegOff), rowY, col, PEG_SZ);
}
renderHints(g2, guessX + (MastermindEngine.SLOTS * pegOff) + gapSz, rowY, r);
}
// Show Solution Node at the bottom
if (revealSolution) {
int solnY = guessY + (10 + 1) * pegOff;
g2.setColor(Color.BLACK);
g2.drawLine(guessX, solnY - 5, guessX + (MastermindEngine.SLOTS * pegOff), solnY - 5);
int[] secret = engine.getSecret();
for (int p = 0; p < secret.length; p++) {
renderPeg(g2, guessX + (p * pegOff), solnY, secret[p], PEG_SZ);
}
}
}
private void renderPeg(Graphics2D g2, int x, int y, int colorIdx, int size) {
if (colorIdx == -1) {
g2.setColor(new Color(149, 149, 149));
} else {
Color[] colors = { Color.RED, Color.YELLOW, Color.GREEN, new Color(51, 77, 255), Color.ORANGE,
new Color(128, 0, 179) };
g2.setColor(colors[colorIdx % colors.length]);
}
g2.fill(new Ellipse2D.Double(x, y, size, size));
g2.setColor(Color.BLACK);
g2.draw(new Ellipse2D.Double(x, y, size, size));
}
private void renderHints(Graphics2D g2, int x, int y, int r) {
int black = feedbackHistory[r][0];
int white = feedbackHistory[r][1];
int hOff = hintSz + gapSz;
for (int i = 0; i < MastermindEngine.SLOTS; i++) {
int hX = x + (i % 2) * hOff;
int hY = y + (i / 2) * hOff;
if (i < black)
g2.setColor(Color.BLACK);
else if (i < black + white)
g2.setColor(Color.WHITE);
else
g2.setColor(new Color(149, 149, 149));
g2.fill(new Ellipse2D.Double(hX, hY, hintSz, hintSz));
g2.setColor(Color.BLACK);
g2.draw(new Ellipse2D.Double(hX, hY, hintSz, hintSz));
}
}
private void initInputListeners() {
MouseAdapter ma = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (solved || !isHumanTurn)
return;
// Check if clicking the palette (colors on the left)
if (e.getX() >= border && e.getX() <= border + PEG_SZ) {
int idx = (e.getY() - border) / pegOff;
if (idx >= 0 && idx < MastermindEngine.COLORS) {
dragColor = idx;
// CHANGE 1: Change cursor to "Hand" when grabbing
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
// CHANGE 2: Reset cursor back to default when color is dropped
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
if (dragColor != -1) {
int p = (e.getX() - (border + PEG_SZ * 2)) / pegOff;
if ((e.getY() - border) / pegOff == nextGo && p >= 0 && p < MastermindEngine.SLOTS) {
currentGuess[p] = dragColor;
}
dragColor = -1;
repaint();
}
// Check for Submit click
if (e.getX() > border + PEG_SZ * 2 + (MastermindEngine.SLOTS * pegOff) && isHumanTurn) {
submitGuess();
}
}
@Override
public void mouseMoved(MouseEvent e) {
// CHANGE 3: Feedback when hovering over the palette colors
if (e.getX() >= border && e.getX() <= border + PEG_SZ) {
int idx = (e.getY() - border) / pegOff;
if (idx >= 0 && idx < MastermindEngine.COLORS) {
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
} else {
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
} else {
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}
};
// Must add both listeners for the cursor to update while moving
addMouseListener(ma);
addMouseMotionListener(ma);
}
public static void main(String[] args) {
JFrame f = new JFrame("Mastermind: BFS & Greedy Solver");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MastermindBoardGUI board = new MastermindBoardGUI();
JPanel controls = new JPanel();
JButton restartBtn = new JButton("Restart Game");
restartBtn.addActionListener(e -> {
board.initGameSystem();
board.repaint();
});
controls.add(restartBtn);
f.setLayout(new BorderLayout());
f.add(board, BorderLayout.CENTER);
f.add(controls, BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}