-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMineWalkerPanel.java
More file actions
539 lines (484 loc) · 16.5 KB
/
MineWalkerPanel.java
File metadata and controls
539 lines (484 loc) · 16.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.Timer;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
/**
* Main game GUI. All elements are added to this panel which is added to the JFrame of MineWalker.java
* Has control over all buttons and labels of the game. Houses all event listeners.
* @author Zach Luciano
*
*/
@SuppressWarnings("serial")
public class MineWalkerPanel extends JPanel {
private final int DELAY = 400;
private int gridSize = 10;
private int width;
private int height;
private ArrayList<Point> path;
private MineFieldPanel mfp;
private JPanel main = this;
private JPanel east = new JPanel();
private JPanel south = new JPanel();
private JPanel west = new JPanel();
private RandomWalk rw;
private Random rand = new Random();
private Timer timer;
private boolean on = false;
private MineFieldButton previouslyClicked;
private MineFieldButton mineClicked = new MineFieldButton();
private boolean mineWasClicked = false;
private int minesNearby;
private ArrayList<Color> previousColors = new ArrayList<Color>();
private int previousColorsIndex = 0;
private int livesLeft = 5;
private int playerScore = -100;
/**
* Creates the JPanel that all game elements go into and adds all of them to its BorderLayout.
* Also starts the first game as soon as it is run.
* @param width - The width of the panel
* @param height - The height of the panel
*/
public MineWalkerPanel(int width, int height) {
this.height = height - 300;
this.width = width - 300;
setLayout(new BorderLayout());
east.setPreferredSize(new Dimension(150, height));
east.setLayout(new GridLayout(2,1));
east.setBackground(Color.LIGHT_GRAY);
fillEastPanel();
west.setPreferredSize(new Dimension(150, height));
west.setLayout(new GridLayout(8, 1));
west.setBackground(Color.LIGHT_GRAY);
fillWestPanel();
south.setPreferredSize(new Dimension(width, 50));
south.setLayout(new FlowLayout());
south.setBackground(Color.LIGHT_GRAY);
fillSouthPanel();
mfp = new MineFieldPanel(new MineButtonListener(), width - 300, height - 300, gridSize);
this.add(mfp, BorderLayout.CENTER);
this.add(south, BorderLayout.SOUTH);
this.add(east, BorderLayout.EAST);
this.add(west, BorderLayout.WEST);
newGridAndWalk(gridSize);
}
/**
* Adds buttons to the South Border of the MineWalkerPanel, these buttons can be used by the player
* Was created as a separate method for readability
*/
private void fillSouthPanel() {
JButton showOrHideMines = new JButton("Show Mines");
showOrHideMines.addActionListener(new ShowHideMinesListener());
showOrHideMines.setPreferredSize(new Dimension(120, 30));
south.add(showOrHideMines);
JButton showOrHidePath = new JButton("Show Path");
showOrHidePath.addActionListener(new ShowHidePathListener());
showOrHidePath.setPreferredSize(new Dimension(120, 30));
south.add(showOrHidePath);
JButton newOrGiveUp = new JButton("Give Up?");
newOrGiveUp.addActionListener(new NewOrGiveUpListener());
newOrGiveUp.setPreferredSize(new Dimension(120, 30));
south.add(newOrGiveUp);
JLabel gridSizeLabel = new JLabel("Grid Size:");
south.add(gridSizeLabel);
JTextField gridSizeField = new JTextField();
gridSizeField.setPreferredSize(new Dimension(40, 30));
gridSizeField.addActionListener(new TextListener());
south.add(gridSizeField);
}
/**
* Adds elements to the East Border of the MineWalkerPanel, these elements pertain to the scoreboard
*/
private void fillEastPanel() {
east.setBorder(BorderFactory.createTitledBorder("Score Board"));
JLabel lives = new JLabel("Lives: " + livesLeft);
lives.setVerticalAlignment(SwingConstants.BOTTOM);
lives.setHorizontalAlignment(SwingConstants.CENTER);
lives.setFont(new Font("Comic Sans MS", Font.PLAIN, 18));
east.add(lives);
JLabel score = new JLabel("Score: " + playerScore);
score.setVerticalAlignment(SwingConstants.TOP);
score.setHorizontalAlignment(SwingConstants.CENTER);
score.setFont(new Font("Comic Sans MS", Font.PLAIN, 18));
east.add(score);
}
/**
* Adds the color key to the West Border of the MineWalkerPanel with necessary JLabels
*/
private void fillWestPanel() {
west.setBorder(BorderFactory.createTitledBorder("Color Key"));
JLabel green = new JLabel("0 Nearby Mines");
green.setBackground(Color.GREEN);
green.setOpaque(true);
green.setHorizontalAlignment(SwingConstants.CENTER);
green.setFont(new Font("Comic Sans MS", Font.PLAIN, 18));
west.add(green);
JLabel yellow = new JLabel("1 Nearby Mine");
yellow.setBackground(Color.YELLOW);
yellow.setOpaque(true);
yellow.setHorizontalAlignment(SwingConstants.CENTER);
yellow.setFont(new Font("Comic Sans MS", Font.PLAIN, 18));
west.add(yellow);
JLabel orange = new JLabel("2 Nearby Mines");
orange.setBackground(Color.ORANGE);
orange.setOpaque(true);
orange.setHorizontalAlignment(SwingConstants.CENTER);
orange.setFont(new Font("Comic Sans MS", Font.PLAIN, 18));
west.add(orange);
JLabel red = new JLabel("3 Nearby Mines");
red.setBackground(Color.RED);
red.setOpaque(true);
red.setHorizontalAlignment(SwingConstants.CENTER);
red.setFont(new Font("Comic Sans MS", Font.PLAIN, 18));
west.add(red);
JLabel black = new JLabel("Exploded Mine");
black.setBackground(Color.BLACK);
black.setForeground(Color.WHITE);
black.setOpaque(true);
black.setHorizontalAlignment(SwingConstants.CENTER);
black.setFont(new Font("Comic Sans MS", Font.PLAIN, 18));
west.add(black);
JLabel emptyCell = new JLabel();
emptyCell.setOpaque(false);
west.add(emptyCell);
JLabel start = new JLabel("Start");
start.setBackground(Color.CYAN);
start.setOpaque(true);
start.setHorizontalAlignment(SwingConstants.CENTER);
start.setFont(new Font("Comic Sans MS", Font.PLAIN, 18));
west.add(start);
JLabel finish = new JLabel("Finish");
finish.setBackground(Color.MAGENTA);
finish.setOpaque(true);
finish.setHorizontalAlignment(SwingConstants.CENTER);
finish.setFont(new Font("Comic Sans MS", Font.PLAIN, 18));
west.add(finish);
}
/**
* Listener for each MineFieldButton in the game panel.
* Checks to see if it is a valid move, as well as if the space that was clicked on was a mine or not
* Creates JOptionPanes based on if the game was won or lost.
* @author Zach Luciano
*/
private class MineButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
MineFieldButton mb = (MineFieldButton) e.getSource();
minesNearby = 0;
mineWasClicked = false;
previouslyClicked.setBackground(previousColors.get(previousColorsIndex));
mineClicked.setBackground(Color.BLACK);
if((mb.x == previouslyClicked.x+1 || mb.x == previouslyClicked.x - 1) && mb.y == previouslyClicked.y) {
mb.setBackground(Color.PINK);
mb.setCurrentPosition();
if(mb.wasAlreadyClicked() == false) {
playerScore += 100;
}
mb.setClicked();
previouslyClicked.setCurrentPosition();
previouslyClicked = mb;
}
else if((mb.y == previouslyClicked.y+1 || mb.y == previouslyClicked.y - 1) && mb.x == previouslyClicked.x) {
mb.setBackground(Color.PINK);
mb.setCurrentPosition();
if(mb.wasAlreadyClicked() == false) {
playerScore += 100;
}
mb.setClicked();
previouslyClicked.setCurrentPosition();
previouslyClicked = mb;
}
System.out.println(playerScore); //remove before submitting
for (int row = 0; row < gridSize; row++) {
for (int column = 0; column < gridSize; column++) {
if(mfp.buttons[row][column].isCurrentPosition()) {
if(row+1 < gridSize && mfp.buttons[row+1][column].isMine())
minesNearby++;
if(row-1 >= 0 && mfp.buttons[row-1][column].isMine())
minesNearby++;
if(column+1 < gridSize && mfp.buttons[row][column+1].isMine())
minesNearby++;
if(column-1 >= 0 && mfp.buttons[row][column-1].isMine())
minesNearby++;
}
if(previouslyClicked.equals(mfp.buttons[0][gridSize-1])) {
playerScore += 300;
int gameWinPane = JOptionPane.showConfirmDialog(null,"You Win!\nWant to play again?", "You Win!", JOptionPane.YES_NO_OPTION);
if(gameWinPane == 0) {
timer.stop();
newGridAndWalk(gridSize);
}
else {
System.exit(0);
}
}
if(previouslyClicked.equals(mfp.buttons[row][column]) && mfp.buttons[row][column].isMine()) {
livesLeft--;
playerScore -= 300;
mfp.buttons[row][column].setMine();
mineClicked = mfp.buttons[row][column];
mineWasClicked = true;
if(livesLeft == 0) {
int gameOverPane = JOptionPane.showConfirmDialog(null, "Game Over, Care to try again?", "Game Over", JOptionPane.YES_NO_OPTION);
if(gameOverPane == 0) {
timer.stop();
newGridAndWalk(gridSize);
}
else {
System.exit(0);
}
}
}
main.remove(east);
east.removeAll();
fillEastPanel();
main.add(east, BorderLayout.EAST);
main.invalidate();
main.revalidate();
}
}
}
}
/**
* Button listener for showing and hiding the mines in the game, should be disabled for play but
* is enabled during play for debugging purposes.
* @author Zach Luciano
*
*/
private class ShowHideMinesListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton shm = (JButton) e.getSource();
if (shm.getText() == "Show Mines") {
for (int row = 0; row < gridSize; row++) {
for (int column = 0; column < gridSize; column++) {
if (mfp.buttons[row][column].isMine()) {
mfp.buttons[row][column].setBackground(Color.BLACK);
}
}
}
shm.setText("Hide Mines");
} else {
for (int row = 0; row < gridSize; row++) {
for (int column = 0; column < gridSize; column++) {
if (mfp.buttons[row][column].isMine()) {
mfp.buttons[row][column].setBackground(Color.WHITE);
}
}
}
shm.setText("Show Mines");
}
}
}
/**
* Button Listener for showing and hiding the path created by RandomWalk. This is used to ensure
* that there is a way to finish the grid every time. Path is created to show the user the ensured
* route to finish the game, should probably be disabled during gameplay but will stay enabled
* for debugging purposes.
* @author Zach Luciano
*
*/
private class ShowHidePathListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton shp = (JButton) e.getSource();
if (shp.getText() == "Show Path") {
for (int row = 0; row < gridSize; row++) {
for (int column = 0; column < gridSize; column++) {
if (mfp.buttons[row][column].isOnPath())
mfp.buttons[row][column].setBackground(Color.BLUE);
}
}
shp.setText("Hide Path");
} else {
for (int row = 0; row < gridSize; row++) {
for (int column = 0; column < gridSize; column++) {
if (mfp.buttons[row][column].isOnPath())
mfp.buttons[row][column].setBackground(Color.WHITE);
}
}
mfp.buttons[gridSize - 1][0].setBackground(Color.CYAN);
mfp.buttons[gridSize - 1][0].setStartOrEnd();
mfp.buttons[0][gridSize - 1].setBackground(Color.MAGENTA);
mfp.buttons[0][gridSize - 1].setStartOrEnd();
shp.setText("Show Path");
}
}
}
/**
* Button Listener for the "New Game" or "Give Up?" button. How it is currently set up,
* a new game is always automatically created so there is no need for the new game button.
* @author Zach Luciano
*
*/
private class NewOrGiveUpListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton ngl = (JButton) e.getSource();
if (ngl.getText() == "New Game") {
timer.stop();
newGridAndWalk(gridSize);
ngl.setText("Give Up?");
}
else {
int gaveUpPane = JOptionPane.showConfirmDialog(null, "Sad to see you couldn't make it\n Want to try again?", "You Gave Up", JOptionPane.YES_NO_OPTION);
if(gaveUpPane == 0) {
timer.stop();
ngl.setText("Give Up?");
newGridAndWalk(gridSize);
}
else {
System.exit(0);
}
}
}
}
/**
* Listener for the text field that resizes the playing grid, default is a 10x10 grid. Number
* entered must be a single number between 4 and 20. Will create a square grid.
* @author Zach Luciano
*
*/
private class TextListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
int gridSize;
JTextField jtf = (JTextField) e.getSource();
try {
gridSize = Integer.parseInt(jtf.getText());
jtf.setText(null);
if (gridSize < 4 || gridSize > 20) {
System.out.println("Grid size can only be from 4-20");
return;
}
} catch (NumberFormatException nfe) {
System.out.println("You must enter a number for the gridSize");
return;
}
timer.stop();
newGridAndWalk(gridSize);
}
}
/**
* Removes the current grid of buttons and recreates it, invalidates and revalidates the pane.
* it then resets the game by creating a path and setting the mines.
* @param gridSize - Grid Size of the game board
*/
private void newGridAndWalk(int gridSize) {
this.gridSize = gridSize;
livesLeft = 5;
playerScore = 0;
main.remove(mfp);
mfp.removeAll();
mfp = new MineFieldPanel(new MineButtonListener(), width - 300, height - 300, gridSize);
main.add(mfp, BorderLayout.CENTER);
main.invalidate();
main.revalidate();
previouslyClicked = mfp.buttons[gridSize - 1][0];
previouslyClicked.setCurrentPosition();
previousColors.add(Color.CYAN);
rw = new RandomWalk(gridSize);
rw.createWalk();
this.path = rw.getPath();
setPath();
placeMines();
startAnimation();
previouslyClicked.doClick();
}
/**
* Creates a random point and checks if it is on the path or already a mine; if not then it will
* become a mine. It runs through this loop for a quarter of the buttons in the grid.
*/
private void placeMines() {
for (int i = 0; i < ((double) gridSize * gridSize) * 0.25; i++) {
String randomCoords = "(" + rand.nextInt(gridSize) + ", " + rand.nextInt(gridSize) + ")";
if (mfp.findButton(randomCoords).isOnPath() || mfp.findButton(randomCoords).isMine()) {
i--;
} else {
mfp.findButton(randomCoords).setMine();
}
}
}
/**
* Takes the path created by the RandomWalk and finds every button on that path.
* Changes the boolean value for that specific button to set it as being on the path.
*/
private void setPath() {
for (int i = 0; i < rw.getPath().size(); i++) {
String pathPos = "(" + path.get(i).x + ", " + path.get(i).y + ")";
for (int row = 0; row < gridSize; row++) {
for (int column = 0; column < gridSize; column++) {
if (pathPos.equals(mfp.buttons[row][column].getName())) {
mfp.buttons[row][column].setOnPath();
}
}
}
}
}
/**
* Performs action when timer event fires.
*/
private class TimerActionListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
on = !on;
for (int row = 0; row < gridSize; row++) {
for (int column = 0; column < gridSize; column++) {
if (mfp.buttons[row][column].isCurrentPosition()) {
if (on) {
if(minesNearby == 0) {
mfp.buttons[row][column].setBackground(Color.GREEN);
previousColors.add(Color.GREEN);
previousColorsIndex++;
}
if(minesNearby == 1) {
mfp.buttons[row][column].setBackground(Color.YELLOW);
previousColors.add(Color.YELLOW);
previousColorsIndex++;
}
if(minesNearby == 2) {
mfp.buttons[row][column].setBackground(Color.ORANGE);
previousColors.add(Color.ORANGE);
previousColorsIndex++;
}
if(minesNearby == 3) {
mfp.buttons[row][column].setBackground(Color.RED);
previousColors.add(Color.RED);
previousColorsIndex++;
}
if(mineWasClicked) {
mfp.buttons[row][column].setBackground(Color.BLACK);
previousColors.add(Color.BLACK);
previousColorsIndex++;
}
}
else {
mfp.buttons[row][column].setBackground(Color.WHITE);
}
}
}
}
}
}
/**
* Create an animation thread that runs periodically
*/
private void startAnimation() {
TimerActionListener taskPerformer = new TimerActionListener();
timer = new Timer(DELAY, taskPerformer);
timer.start();
}
}