Repository navigation
Expand file tree
/
Copy pathmineSweeper.java
More file actions
executable file
·471 lines (448 loc) · 21.4 KB
/
Copy pathmineSweeper.java
File metadata and controls
executable file
·471 lines (448 loc) · 21.4 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
/**
* Minesweeper is a basically a single-player video game. The object is to clear an abstract minefield without denoting a mine.
* In the board, there are three types of information stored in each grid.
* The first is a mine graphic, which means there is a mine in that square.
* The second is a digit between 1 and 8 (containing 1 and 8), indicating the number of adjacent squares (typically, out of the possible 8) which contain mines.
* However, if the grid is at the edge of the board, only part of the adjacent squares count.
* The last type is a blank grid, which means there is no mines at the square and there are no mines around the grid either.
* For every click, the user can either clear the square, or put a flag on there to mark a mine.
* The user can also change the flag back to a nomal grid by right clicking again.
* If the user hits a mine, game will be over and the whole board will be shown.
* If the whole board is cleared, and at the same time the mines are marked correctly, the user will win the game.
* The clock will stop counting as soon the game is over or the user wins.
*
* @Jack Zhang
* @June 16th, 2010
*/
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class mineSweeper extends JFrame
implements MouseListener,ActionListener
{
protected static int size; // The size of the board
protected static int mineNum; // The number of mines
protected static int[][] board= new int[100][100]; // The array which stores the whole board, consisting of a number from 0 to 9
protected static int[][] status=new int[100][100]; // The array which tracks the status of the board (cleard, unopend, marked), consisting of a number from 1 to 2
private boolean ifSetUp; // True if the board has already been set up; False if the board has not been set up
private JPanel buttonPanel,panel; // The two panels: panel has the time counter and the number of mines/ buttonPanel has all the grids of the board
private static JButton button[]=new JButton[1000]; // The "graphic board", contains images
private JButton button_start=new JButton(); // The smiling face icon
private JTextField text1=new JTextField("",3); // The number of mines
private JTextField text2=new JTextField("",3); // Time counter
private static Container c; // Contains components such as buttons, textfields
private static Icon icon; // The icon that will be put on the buttons
javax.swing.Timer timer; // Declear the timer
/**
* Constructor. Initialize all the variables, arrays, and buttons depending on the level
*/
public mineSweeper(int level)
{
super("Minesweeper");
// Set up the size and the number of mines depends on the level user chooses
if (level==1){
size=9;
mineNum=10;
} else if (level==2){
size=16;
mineNum=40;
} else {
size=20;
mineNum=70;
}
text1.setText(Integer.toString(mineNum)); // Display the correct number of mines in textfield 1
text2.setText("0"); // Timer starts at 0
c=getContentPane();
buttonPanel=new JPanel();
buttonPanel.setLayout(new GridLayout(size,size,1,1)); // Display the board with the size*size and 1 seperation distance
// Add a mouselistener to each element of the button array
for (int i=0;i<size*size;i++)
{
button[i]=new JButton();
buttonPanel.add(button[i]);
button[i].addMouseListener(this);
}
c.add(buttonPanel);
panel=new JPanel();
panel.add(text1);
text1.setFont(new Font("Serif",Font.BOLD,16)); // Set up the font and the size of the numbers
text1.setEditable(false); // Prevent user from changing the display numbers
panel.add(button_start);
panel.add(text2);
text2.setFont(new Font("Serif",Font.BOLD,16));// Set up the font and the size of the numbers
text2.setEditable(false); // Prevent user from changing the display numbers
button_start.addActionListener(this); // Add a action listener to the smiling face
icon=new ImageIcon("j14.gif");
button_start.setIcon(icon);
c.add(panel,BorderLayout.NORTH);
timer=new javax.swing.Timer(1000,this); // Set the timer
// Set up the size of the form in order to fit the board
if (level==1)
setSize(200,280);
else if (level==2)
setSize(340,430);
else
setSize(400,500);
setVisible(true); // Make the window visible to the user
ifSetUp=false; // Initialise ifSetUp variable to false
for (int i=0; i<size; i++){
for (int j=0; j<size; j++){
board[i][j]=0;
status[i][j]=0;
icon=new ImageIcon("j10.gif"); // icon receives the image
button[i*size+j].setIcon(icon);// Set the buttons as unopened
}
}
}
/**
* Start the game by giving the user instructions in a message box
* Let the user choose a level from level, level 2, level 3
*/
public static void main(String[] args){
JOptionPane.showMessageDialog(null,
"Now you are able clear any grid on the board by left clicking on it. \n"+
"After single left click, a single number may be revealed, or an area consists of numbers and blanks may be exhibited. \n"+
"The blank grid represents there is neither a number nor mine on that square. \n"+
"A digit indicating the number of adjacent squares (typically, out of the possible 8) which contain mines. \n"+
"However, if the grid is at the edge of the board, only part of the adjacent squares count. \n"+
"By using logic, you can figure out the positions of the mines and mark them.\n"+
"You can place a flag graphic on any square believed to contain a mine by right-clicking on the square. \n"+
"Every time you place a flag, the number on the top which indicates how many mines are left will decrease by 1. \n"+
"Notice that you cannot mark more mines than the given number. \n"+
"You can always change a flag back to the gray grid simply by right clicking on it again. \n"+
"You task is to clear the whole board and mark the mines correctly. \n"+
"Once you accidently hit a mine, game will be over and the whole board will be revealed. \n"+
"Remember there is a time counter on the upper right corner. Therefore, be quick! Good luck.\n"+
"Next,please choose level.",
"Game Rules",
JOptionPane.PLAIN_MESSAGE);
String s=JOptionPane.showInputDialog("Please enter 1,2 or 3\n"+
"Level1: 9*9 board with 10 mines\n"+
"Level2: 16*16 board with 40 mines\n"+
"Level3: 20*20 board with 70 mines");
if (s!=null){ // If user presses cancel, the return type is a null. Therefore, this conditional statement avoids this problem
if (s.equals("1"))
{
mineSweeper app=new mineSweeper(1); // Create a new object of the class
app.addWindowListener(new MyWindowListener()); // Add the listener to windows
}
else if (s.equals("2"))
{
mineSweeper app=new mineSweeper(2);
app.addWindowListener(new MyWindowListener());
}
else if (s.equals("3"))
{
mineSweeper app=new mineSweeper(3);
app.addWindowListener(new MyWindowListener());
}
}
}
/** My major method, where most of the work is done
* Get user's click and perform the corresponding actions
*/
public void mouseClicked(MouseEvent e)
{
// index: the position of user's click
// bestRecord: stores the least amount of time
int index,i,j,bestRecord;
boolean ifWin=false; // True if the user wins the game; False if the user loses the game
boolean ifFinished=false; // True if the user has finished the game; False if the user has not finished the game
String mine;
bestRecord=0; // Will be used when comparing two records
Scanner input;
// The loop that finds the position of where the user clicks
for (int k=0;k<size*size;k++)
{
if (e.getSource()==button[k])
{
index=k;// index stores the position in terms of one dimensional array
// i,j also stores the position in terms of two dimensional array
// change index to i and j
i=(int)(k/size);
j=k%size;
// if the program has not set up, then call the mineSetUp method to set up the board
if (ifSetUp==false){
mineSetUp(i,j);
ifSetUp=true;
timer.start(); // Start counting time from user's first click
}
if (status[i][j]!=1){ // If the grid has not been cleared yet
if (e.getButton() == MouseEvent.BUTTON1 && status[i][j]!=2) // If it is a left click, and the grid has not been marked
{
// If there is a mine, show the mine and finish the game
if (board[i][j]==9){
icon=new ImageIcon("j9.gif");
button[index].setIcon(icon);
status[i][j]=1; // Clear the grid
// Finish the game and the user loses
ifFinished=true;
ifWin=false;
printMine(); // Print the rest of the mines
} else {
boardClear(i,j); // If it is not a mine, call the boardClear method to clear the grids
}
} else if (e.getButton() == MouseEvent.BUTTON3) // If it is a right click
{
// If the grid has not been marked, mark it
if (status[i][j]==0 && mineNum>0){
status[i][j]=2;// Change the status to "Marked"
icon=new ImageIcon("j11.gif"); // Change the icon to a flag
button[index].setIcon(icon);
mineNum--; // decrease the number of mines by 1
// Change the display of number of mines. Decrease it by 1
mine=text1.getText();
text1.setText(Integer.toString(Integer.parseInt(mine)-1));
} else if (status[i][j]==2){ // If the grid has been marked, unmark it
status[i][j]=0; // Change the status to "Unmarked"
icon=new ImageIcon("j10.gif"); // Change the icon to the nomal "unopened" appearance
button[index].setIcon(icon);
mineNum++; // increase the number of mines by 1
// Change the display of number of mines. Increase it by 1
mine=text1.getText();
text1.setText(Integer.toString(Integer.parseInt(mine)+1));
}
}
}
}
}
if (ifFinished==false){
// If the user has not lost the game, check if the user has won the game
if (checkWin()) {
ifFinished=true;
ifWin=true;
}
}
// If the user has finished the game, inform the user if he/she has won or lost, and exit the game
if (ifFinished){
timer.stop();// The timer stops counting
if (ifWin==true){
// Read the record from file
try{
input=new Scanner(new File("record.txt"));
bestRecord=input.nextInt();
input.close();
} catch(FileNotFoundException f){
bestRecord=Integer.parseInt(text2.getText());// If there is no file, set the best record to the current time
}
if (Integer.parseInt(text2.getText())<bestRecord){
bestRecord=Integer.parseInt(text2.getText()); // If time is smaller than the best record, save the time
}
PrintWriter out=null;
try {
out = new PrintWriter(new FileWriter("record.txt"));
} catch (Exception f) {
}
out.println(bestRecord); // Store the best record into the same file
out.close(); // Close the output file
// If the user wins the game, congratualates to the user
JOptionPane.showMessageDialog(
null,
"Congratualations, you won the game!\n"+
"Your best record is:"+Integer.toString(bestRecord)+"s",
"Game Won",
JOptionPane.PLAIN_MESSAGE);
} else if (ifWin==false)
// Inform the user if he/she loses the game
JOptionPane.showMessageDialog(
null,
"Sorry, you lost the game. Better luck next time!",
"Game Lost",
JOptionPane.PLAIN_MESSAGE);
System.exit(0); // Close the windows
}
}
/**
* Although I'm not using these mouse events, I have to list them in order to let the program work
*/
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
/**
* This will automatically run every second
*/
public void actionPerformed(ActionEvent e)
{
String time;
// If user does not press the smiling, then increase the time counter by 1
if (e.getSource()!=button_start){
time=text2.getText();
text2.setText(Integer.toString(Integer.parseInt(time)+1));
} else {
String s=JOptionPane.showInputDialog("Please enter 1, 2 or 3\n"+
"Level1: 9*9 board with 10 mines\n"+
"Level2: 16*16 board with 40 mines\n"+
"Level3: 20*20 board with 70 mines");
if (s!=null){
if (s.equals("1"))
{
setVisible(false);
mineSweeper app=new mineSweeper(1);
app.addWindowListener(new MyWindowListener());
}
else if (s.equals("2"))
{
setVisible(false);
mineSweeper app=new mineSweeper(2);
app.addWindowListener(new MyWindowListener());
}
else if (s.equals("3"))
{
setVisible(false);
mineSweeper app=new mineSweeper(3);
app.addWindowListener(new MyWindowListener());
}
}
}
}
/**
*Set up all the mines in random spots
* The methods makes sure that the spot where user clicks will not be a mine
*/
public static void mineSetUp(int i, int j){
int n=mineNum;
int x,y=0; // Coordinates
Random generator=new Random();
while (n>0){
// Generate a random position in the array
do {
x=generator.nextInt(size);
y=generator.nextInt(size);
} while(board[x][y]==9 || (x==i && y==j)); // If there is a mine in the spot already, or the position is where the user clicks, contine generating random numbers
board[x][y]=9;// Put a mine there
numberSetUp(x,y); // Seet up the numbers
n--; // Decrease the number of mines by 1
}
}
/**
* Set up the numbers depending on the mines
*/
public static void numberSetUp(int x, int y){
// Check every connecting grids seperately
// If it is a vivid grid (not outside of the board), then increase its value in board array by 1, as there is a mine
if(x>0 && y>0 && board[x-1][y-1]!=9){
board[x-1][y-1]++;
}
if(x>0 && board[x-1][y]!=9){
board[x-1][y]++;
}
if(x>0 && y<size-1 && board[x-1][y+1]!=9){
board[x-1][y+1]++;
}
if(y<size-1 && board[x][y+1]!=9){
board[x][y+1]++;
}
if(x<size-1 && y<size-1 && board[x+1][y+1]!=9){
board[x+1][y+1]++;
}
if(x<size-1 && board[x+1][y]!=9){
board[x+1][y]++;
}
if(x<size-1 && y>0 && board[x+1][y-1]!=9){
board[x+1][y-1]++;
}
if(y>0 && board[x][y-1]!=9){
board[x][y-1]++;
}
}
/**
* Clear numbers and connecting blank grids
*/
public static void boardClear(int i, int j){
if (i>=0 && i<size && j>=0 && j<size && status[i][j]==0){ // if the coordinate (i,j) is a valid point and it has not been cleared yet
if (board[i][j]!=0){ // If it is not a blank grid
status[i][j]=1; // Change the status to cleared
print(i,j); // Display this grid
} else { // If it is a blank grid
print(i,j); // Display the blank space
status[i][j]=1; // Change the status to cleared
// Clear all eight connecting grids
boardClear(i-1,j-1);
boardClear(i-1,j);
boardClear(i-1,j+1);
boardClear(i,j+1);
boardClear(i+1,j+1);
boardClear(i+1,j);
boardClear(i+1,j-1);
boardClear(i,j-1);
}
}
}
/**
* This method converts the board array (consisting of numbers) to the graph (consisting of images from the file)
*/
public static void print(int i, int j){
int index;
index=i*size+j; // Convert the coordinates in the two dimensional array to the coordinate in a one dimensional array
if (board[i][j]==1){
icon=new ImageIcon("j1.gif");
button[index].setIcon(icon);
} else if (board[i][j]==2){
icon=new ImageIcon("j2.gif");
button[index].setIcon(icon);
} else if (board[i][j]==3){
icon=new ImageIcon("j3.gif");
button[index].setIcon(icon);
} else if (board[i][j]==4){
icon=new ImageIcon("j4.gif");
button[index].setIcon(icon);
} else if (board[i][j]==5){
icon=new ImageIcon("j5.gif");
button[index].setIcon(icon);
} else if (board[i][j]==6){
icon=new ImageIcon("j6.gif");
button[index].setIcon(icon);
} else if (board[i][j]==7){
icon=new ImageIcon("j7.gif");
button[index].setIcon(icon);
} else if (board[i][j]==8){
icon=new ImageIcon("j8.gif");
button[index].setIcon(icon);
} else if (board[i][j]==9){
icon=new ImageIcon("j9.gif");
button[index].setIcon(icon);
} else if (board[i][j]==0){
icon=new ImageIcon("");
button[index].setIcon(icon);
button[index].setBackground(Color.gray); // If the grid is a blank space, print out gray colour
}
}
/**
* Check if the user wins the game
* Return true if the entire board are marked or cleared
* Return false if one grid is unopened
*/
public static boolean checkWin(){
boolean ifWin=true;
for (int i=0; i<size; i++){
for (int j=0; j<size; j++){
if (status[i][j]==0){
ifWin=false;
}
}
}
return ifWin;
}
/**
* Print out all the mines and wrongly marked the mines after user loses the game.
*/
public static void printMine(){
int index;
for (int i=0; i<size; i++){
for (int j=0; j<size; j++){
index=i*size+j; // Convert the coordinates into a one dimensional array index
if (status[i][j]==2 && board[i][j]!=9){ // If the grid is marked as a mine, but it is actually not a mine
icon=new ImageIcon("j12.gif"); // Print out the image with a X on the flag
button[index].setIcon(icon);
};
if (board[i][j]==9 && status[i][j]!=2){ // If there is a mine, but the user has not marked it.
icon=new ImageIcon("j9.gif"); // Display the mine on that grid
button[index].setIcon(icon);
};
}
}
}
}