/
GameCourt.java
279 lines (239 loc) · 9.62 KB
/
GameCourt.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
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
/**
* CIS 120 Game HW
* (c) University of Pennsylvania
* @version 2.0, Mar 2013
*/
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import javax.swing.*;
/**
* GameCourt
*
* This class holds the primary game logic for how different objects interact
* with one another. Take time to understand how the timer interacts with the
* different methods and how it repaints the GUI on every tick().
*
*/
@SuppressWarnings("serial")
public class GameCourt extends JPanel {
private GameBoard board;
public boolean playing = false; // whether the game is running
private JLabel status; // Current status text (i.e. Running...)
public static final int COURT_WIDTH = 600;
public static final int COURT_HEIGHT = 600;
public static final int INTERVAL = 35;
public GameCourt(final JLabel status) {
setFocusable(true);
final Timer timer = new Timer(INTERVAL, new ActionListener() {
public void actionPerformed(ActionEvent e) {
tick();
}
});
timer.start();
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (board.getType((int) (e.getX() / 20), (int) (e.getY() / 20)).equals("Mine")) {
if (SwingUtilities.isLeftMouseButton(e)) {
board.revealMines(e);
status.setText("Game Over!");
playing = false;
highScoreEntry();
} else if (SwingUtilities.isRightMouseButton(e)) {
if (board.isSwept1(e)) {
status.setText("You Win!");
playing = false;
}
}
}
board.handle(e);
}
});
this.status = status;
}
/**
* (Re-)set the game to its initial state.
*/
public void reset() {
board = new GameBoard(COURT_WIDTH, COURT_HEIGHT);
playing = true;
status.setText("Board Swept: " + board.getSwept() + "%");
// Make sure that this component has the keyboard focus
requestFocusInWindow();
repaint();
}
/**
* Opens the instructions window.
*/
public void instructions() {
final JFrame help = new JFrame("Instructions");
help.setLocation(300, 300);
final JPanel help_panel = new JPanel();
help.add(help_panel);
final JLabel text = new JLabel("<html>Welcome to Minesweeper! "
+ "Now you don't need to dual-boot Windows 95!<br><br>The goal of the game is to "
+ "clear the board of mines without tripping any.<br>Click the cells on the board "
+ "to reveal them. The numbers signify the number<br>of mines in the surrounding "
+ "eight cells. If you believe that there is a mine<br>in a particular cell, "
+ "right-click it to flag it as a mine. If you flag all of the<br>mines on the "
+ "board without tripping any, then you win!<br><br>Good luck, Corporal! And watch "
+ "out for them pesky mines!</html>");
help_panel.add(text);
help.pack();
help.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
help.setVisible(true);
}
/**
* Opens the high scores window.
*/
public void highScoreView() {
String result = "";
try (Scanner in = new Scanner(new FileReader("highscores.txt"))) {
Map<String, Integer> scores = new TreeMap<String, Integer>();
while (in.hasNextLine()) {
in.useDelimiter(" ");
String firstname = in.next();
in.useDelimiter("%");
Integer score = Integer.parseInt(in.next().substring(1));
in.useDelimiter("\n");
in.next();
scores.put(firstname, score);
}
Set<Map.Entry<String, Integer>> scoreSet = new TreeSet<Map.Entry<String, Integer>>();
scoreSet = scores.entrySet();
StringBuffer scoreString = new StringBuffer();
for (int i = 1; i < 11; i++) {
Integer max = -1;
String maxperson = "";
for (Map.Entry<String, Integer> x : scoreSet) {
if (x.getValue() > max) {
max = x.getValue();
maxperson = x.getKey();
}
}
Iterator<Map.Entry<String, Integer>> iter = scoreSet.iterator();
while (iter.hasNext()) {
if (iter.next().getKey().equals(maxperson)) {
iter.remove();
}
}
scoreString.append("<br>" + i + ". " + maxperson + " " + max + "%<br>");
}
result = scoreString.toString();
} catch (FileNotFoundException x){
final JFrame error = new JFrame("Error");
error.setLocation(300, 300);
final JPanel error_panel = new JPanel();
error.add(error_panel);
final JLabel text = new JLabel("Internal File Not Found");
error_panel.add(text);
error.pack();
error.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
error.setVisible(true);
}
final JFrame scores = new JFrame("High Scores");
scores.setLocation(300, 300);
final JPanel scores_panel = new JPanel();
scores.add(scores_panel);
final JLabel text = new JLabel("<html>Minesweeper High Scores: "
+ "<br>" + result + "</html>");
scores_panel.add(text);
scores.pack();
scores.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
scores.setVisible(true);
}
public void highScoreEntry() {
final JFrame entry = new JFrame("Congratulations!");
entry.setLocation(300, 300);
final JPanel entry_panel = new JPanel();
entry.add(entry_panel);
final JTextField text = new JTextField("Enter your first name (no spaces)");
entry_panel.add(text);
final JButton enter = new JButton("Enter");
enter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = text.getText();
if (name.contains(" ")) {
final JFrame error = new JFrame("Error");
error.setLocation(300, 300);
final JPanel error_panel = new JPanel();
error.add(error_panel);
final JLabel text = new JLabel("Invalid name format! Try again.");
error_panel.add(text);
error.pack();
error.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
error.setVisible(true);
} else {
try (FileWriter fw = new FileWriter("highscores.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)) {
StringWriter buf = new StringWriter();
buf.write("\n" + name + " " + board.getSwept() + "%");
out.print(buf.toString());
entry.dispose();
reset();
} catch (FileNotFoundException e1) {
final JFrame error = new JFrame("Error");
error.setLocation(300, 300);
final JPanel error_panel = new JPanel();
error.add(error_panel);
final JLabel text = new JLabel("Internal File Not Found");
error_panel.add(text);
error.pack();
error.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
error.setVisible(true);
} catch (IOException e1) {
final JFrame error = new JFrame("Error");
error.setLocation(300, 300);
final JPanel error_panel = new JPanel();
error.add(error_panel);
final JLabel text = new JLabel("Internal File Error");
error_panel.add(text);
error.pack();
error.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
error.setVisible(true);
}
}
}
});
entry_panel.add(enter);
entry.pack();
entry.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
entry.setVisible(true);
}
public int getSwept() {
return board.getSwept();
}
/**
* This method is called every time the timer defined in the constructor
* triggers.
*/
void tick() {
repaint();
if (playing) {
status.setText("Board Swept: " + board.getSwept() + "%");
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
board.draw(g);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(COURT_WIDTH, COURT_HEIGHT);
}
}