-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMineSweeperGUI.java
123 lines (104 loc) · 3.98 KB
/
MineSweeperGUI.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
/*****************************************************************
Handles GUI of minesweeper, including pop up option panes
@author Xue Hua
@version Winter 2019
*****************************************************************/
package MineSweeper;
import javax.swing.*;
public class MineSweeperGUI {
/* size of board */
static private int size;
/* number of mines */
static private int mine;
/*****************************************************************
Prompts user to enter a valid number. Will default to 10 if
there is no input.
@param void
@return none
@throws NumberFormatException if non integer input
@throws NullPointerException if input is null
*****************************************************************/
static private void gameSizePrompt() {
// sets up the OptionPane buttons
UIManager.put("OptionPane.cancelButtonText", "QUIT");
UIManager.put("OptionPane.okButtonText", "NEXT");
String input = JOptionPane.showInputDialog(null,
"Enter Size of board (3-30)\n" +
"[ Leaving blank defaults to 10 ]", "10");
// Checks if valid size input
try {
// Defaults to 10 if blank
if (input.equals(""))
input = "10";
// Once a button is selected
if ((input != null) && (input.length() > 0))
try {
size = Integer.parseInt(input);
} catch (NumberFormatException e) {
errorMessage();
}
if (size < 2 || size > 30)
errorMessage();
} catch (NullPointerException e) {
System.exit(1);
}
gameMinePrompt();
}
/*****************************************************************
Prompts user to enter a valid number of mines. Will default to 10 if
there is no input.
@param void
@return none
@throws NumberFormatException if non integer input
@throws NullPointerException if input is null
*****************************************************************/
static private void gameMinePrompt() {
String input = JOptionPane.showInputDialog(null,
"Mines (1 - " + size + " )\n " +
"[Leaving blank defaults to 10]", "10");
// Checks if valid size input
try {
// Defaults to 10 if blank
if (input.equals(""))
input = "10";
// Once a button is selected
if ((input != null) && (input.length() > 0))
try {
mine = Integer.parseInt(input);
} catch (NumberFormatException e) {
errorMessage();
}
if (mine < 0 || mine > size * size)
errorMessage();
} catch (NullPointerException e) {
System.exit(1);
}
}
/*****************************************************************
Displays an error message
@param void
@return none
*****************************************************************/
static private void errorMessage() {
Object[] options = {"Gracefully Try Again", "Quit!"};
int n = JOptionPane.showOptionDialog(null,
"Invalid Input\n", "OOPS!",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
null, options, options[0]);
if (n == JOptionPane.YES_OPTION)
gameSizePrompt();
else
System.exit(0);
}
public static void main(String[] args) {
// Prompt user for board size and number of mines
gameSizePrompt();
// Frame setup
JFrame frame = new JFrame("Mine Sweeper!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MineSweeperPanel panel = new MineSweeperPanel(size, mine);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}