-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
318 lines (275 loc) · 10.5 KB
/
GUI.java
File metadata and controls
318 lines (275 loc) · 10.5 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
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.Timer;
import javax.swing.JOptionPane;
public class GUI {
private JFrame frame;
private JPanel topPanel; // panel of labels
private JLabel timerLabel;
private JLabel flagLabel;
private JPanel buttonPanel; // grid panel
private GridButton[][] buttonGrid;
private Timer timer; // game timer
private int seconds; // timer count
private int score;
private Color[][] tanGrid;
private int rows;
private int cols;
private MineSweeper game;
private int numClicks;
private int flags;
private int correctFlags;
private int totalMines;
public GUI(int rows, int cols, MineSweeper game) {
// initialize rows, cols, MineSweeper game
this.rows = rows;
this.cols = cols;
this.game = game;
// reset instance variables
numClicks = 0;
flags = 0;
correctFlags = 0;
totalMines = game.getNumMines();
// instantiate the frame and grid
frame = new JFrame();
buttonGrid = new GridButton[rows][cols]; // NEED TO ADJUST FOR CUSTOM SIZE
// set up timer
seconds = 0;
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// update the label with the current time
seconds++;
timerLabel.setText("TIME: " + seconds);
}
});
timerLabel = new JLabel("TIME: 0"); // label for timer
flagLabel = new JLabel("FLAGS LEFT: 0"); // label for flags left
// center the labels
timerLabel.setHorizontalAlignment(JLabel.CENTER);
flagLabel.setHorizontalAlignment(JLabel.CENTER);
// setup panel of labels
topPanel = new JPanel();
topPanel.setBorder(BorderFactory.createEmptyBorder(15, 30, 10, 30));
topPanel.setLayout(new GridLayout(1, 2));
topPanel.add(timerLabel);
topPanel.add(flagLabel);
// grid panel
buttonPanel = new JPanel();
buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 30, 10, 30));
buttonPanel.setLayout(new GridLayout(rows, cols, 0, 0));
// create buttons in grid
for (int r = 0; r < rows; r++) { // NEED TO ADJUST FOR CUSTOM SIZE
for (int c = 0; c < cols; c++) {
GridButton gridButton = new GridButton(" ", r, c, game, this);
buttonPanel.add(gridButton);
gridButton.setOpaque(true);
gridButton.setContentAreaFilled(true);
gridButton.setBorder(new LineBorder(new Color(96, 96, 96), 0));
buttonGrid[r][c] = gridButton;
// green checkerboard
if ((r+c)%2==0) {
gridButton.setBackground(new Color(0, 153, 76)); // lighter green
buttonGrid[r][c].setBaseBackground(new Color(0, 153, 76));
}
else {
gridButton.setBackground(new Color(0, 131, 65)); // darker green
buttonGrid[r][c].setBaseBackground(new Color(0, 131, 65));
}
}
}
// creates tan checkerboard using color values
tanGrid = new Color[rows][cols];
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if ((r+c)%2==0) {
tanGrid[r][c] = new Color(251, 221, 181); // lighter tan
}
else {
tanGrid[r][c] = new Color(226, 196, 156); // darker tan
}
}
}
frame.setLayout(new BorderLayout());
frame.add(topPanel, BorderLayout.NORTH); // panel of labels
frame.add(buttonPanel, BorderLayout.CENTER); // panel of buttons
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("MineSweeper"); // set frame title
frame.pack();
frame.setSize(600, 400); // set default frame size to 600x400 pixel
frame.setMinimumSize(new Dimension(60+cols*54, 120+rows*40)); // set minimum size depending on frame size
frame.setVisible(true); // frame is visible
}
public void firstClick(GridButton btn) {
timer.start(); // start the timer
game.runGame(rows, cols, btn.getRow(), btn.getCol()); // run the game using the first click location
}
// stop the timer at end of game
public void stopTimer() {
timer.stop();
}
// returns timer value
public int getTime() {
return seconds;
}
// switch the button's color depending on tanGrid color values
public void turnTan (int row, int col, int num) {
if (num != 0) {
buttonGrid[row][col].setText("" + num);
}
buttonGrid[row][col].setBackground(tanGrid[row][col]);
buttonGrid[row][col].setBaseBackground(tanGrid[row][col]);
buttonGrid[row][col].tan();
}
// flag the designated location and update the count
public void flag(int row, int col) {
buttonGrid[row][col].flag();
updateFlagCount(1);
}
// remove flag from the designated location and update the count
public void removeFlag(int row, int col) {
buttonGrid[row][col].removeFlag();
updateFlagCount(-1);
}
// set the button's value
public void setValue(int row, int col, int val) {
buttonGrid[row][col].setValue(val);
}
// get the button's value
public int getValue(int row, int col) {
return buttonGrid[row][col].getValue();
}
// reset the values of all buttons
public void reset() {
for (int i = 0; i < buttonGrid.length; i++) {
for (int j = 0; j < buttonGrid[0].length; j++) {
buttonGrid[i][j].setValue(0);
}
}
}
// update the number of flags label
public void updateFlagCount(int flagChange) {
flags += flagChange;
flagLabel.setText("FLAGS LEFT: " + (totalMines - flags));
}
// end the game
public void gameOver() {
stopTimer();
// disable button interaction
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
buttonGrid[r][c].endGame();
}
}
// display dialog
// the score is the number of flags placed correctly
// times 1000 divided by the number of seconds played
score = correctFlags*1000/seconds;
Object[] options = {"Restart", "Quit"};
int option = JOptionPane.showOptionDialog(frame,
"Game over! You hit a mine!\nScore: " + score +
"\nTime: " + seconds, "MineSweeper",
JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, options[1]);
// if "Restart" is clicked
if (option == 0) {
frame.dispose(); // close frame
new MineSweeper(); // create a new game object
}
// if "Quit" is clicked
else if (option == 1) {
System.exit(0); // quit
}
}
// display success dialog
public void success() {
stopTimer();
// the score is the number of flags placed correctly
// times 1000 divided by the number of seconds played
score = correctFlags*1000/seconds;
Object[] options = {"Restart", "Quit"};
int option = JOptionPane.showOptionDialog(frame,
"Congratulations! You've completed the game!\nScore: " + score +
"\nTime: " + seconds, "MineSweeper",
JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, options[1]);
// if "Restart" is clicked
if (option == 0) {
frame.dispose(); // close frame
new MineSweeper(); // create new game object
}
// if "Quit" is clicked
else if (option == 1) {
System.exit(0); // quit
}
}
// get the tan color value of a location
public Color getTan(int row, int col) {
return tanGrid[row][col];
}
// hndle a left click on a button
public void handleClick(GridButton btn) {
if (numClicks == 0) {
firstClick(btn);
game.revealSurrounding(btn.getRow(), btn.getCol());
}
else {
game.revealCell(btn.getRow(), btn.getCol());
}
numClicks++;
}
// check if a location is flagged
public boolean isFlag(int row, int col) {
return buttonGrid[row][col].isFlag();
}
// add to correctly placed flags
public void addCorrectFlag() {
correctFlags++;
}
// subtract from correctly placed flags
public void subtractCorrectFlag() {
correctFlags--;
}
// reveal a red mine at location
public void revealMine(int r, int c) {
buttonGrid[r][c].revealMine();
}
// ask what size grid to create
public static int[] startPrompt() {
// row selections
String[] rowOptions = {"6", "7", "8", "9", "10",
"11", "12", "13", "14", "15", "16"};
JComboBox<String> rowSelection = new JComboBox<>(rowOptions);
rowSelection.setSelectedItem("10"); // set default row to 10
// column selections
String[] colOptions = {"6", "7", "8", "9", "10",
"11", "12", "13", "14", "15", "16"};
JComboBox<String> colSelection = new JComboBox<>(colOptions);
colSelection.setSelectedItem("10"); // set default col to 10
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(new JLabel("Rows:")); // label rows prompt
panel.add(rowSelection);
panel.add(new JLabel("Columns:")); // label columns prompt
panel.add(colSelection);
int result = JOptionPane.showConfirmDialog(null, panel, "MineSweeper",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
// set rows and cols by turning the selections into Integers
if (result == JOptionPane.OK_OPTION) {
int rows = Integer.parseInt((String) rowSelection.getSelectedItem());
int cols = Integer.parseInt((String) colSelection.getSelectedItem());
return new int[]{rows, cols};
} else {
return null;
}
}
}