-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMineSweeper.java
More file actions
310 lines (209 loc) · 9.12 KB
/
MineSweeper.java
File metadata and controls
310 lines (209 loc) · 9.12 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
import java.util.Scanner;
import java.util.Random;
// =================================================================================================================================
// =================================================================================================================================
public class MineSweeper {
// =================================================================================================================================
// =============================================================================================================================
public static Scanner keyboard = new Scanner(System.in);
public static final int REVEAL = 1;
public static final int FLAG = 2;
// =============================================================================================================================
// =============================================================================================================================
public static void main (String[] args) {
if (args.length != 2) {
showUsageAndExit();
}
int numMines = 0;
int sideSize = 0;
try {
numMines = Integer.parseInt(args[0]);
sideSize = Integer.parseInt(args[1]);
} catch (NumberFormatException e) {
showUsageAndExit();
}
if ((numMines <= 0) || (sideSize <= 0)) {
showUsageAndExit();
}
Cell[][] grid = populate(sideSize, numMines);
showGrid(grid);
int gameCount = 0;
int [] choice = new int [3];
while(gameCount == 0) {
choice = getCommand(sideSize);
if (choice[0] == REVEAL) {
grid[choice[1]][choice[2]].setVisible();
if (grid[choice[1]][choice[2]].getMined() == true) {
System.out.println("\nBOOOOOM! \nI AM SORRY YOU HAVE LOST THE GAME!");
gameCount = 1;
}
// THIS PART SHOWS EXPANDS THE SURROUNDING BLOCKS OF AN EMPTY SPOT UNTIL WE HIT SPOT THAT NEIGHBOR MINES
if(grid[choice[1]][choice[2]].getMinedNeighbors() == 0) {
revealneighbors(grid, choice[1], choice[2], sideSize);
}
showGrid(grid);
}
if (choice[0] == FLAG) {
grid[choice[1]][choice[2]].toggleFlagged ();
grid[choice[1]][choice[2]].getFlagged();
showGrid(grid);
}
}
//play(mines);
} // main ()
// =============================================================================================================================
// =============================================================================================================================
public static void showUsageAndExit () {
System.err.println("USAGE: java Minesweeper <number of mines> <grid size on each side>");
System.exit(1);
} // showUsageAndExit
// =============================================================================================================================
// =============================================================================================================================
public static Cell[][] populate (int sideSize, int numMines) {
// SET UP THE GRID.
Cell grid [] [] = new Cell [sideSize][sideSize];
for (int row = 0; row < sideSize; row++){
for (int col =0; col < sideSize; col++){
grid[row][col] = new Cell();
}
}// SET UP` MINES
for (int i = 0; i < numMines; i++) {
int s = (int)(Math.random() * (sideSize));
int t = (int)(Math.random() * (sideSize));
grid[s][t].setMined();
}
//SHOWS PLACED MINES
/*for (int row = 0; row < sideSize; row++){
for (int col =0; col < sideSize; col++){
if(grid[row][col].getMined() == true) {
grid[row][col].setVisible();
}
}
}*/
//CALLS METHOD TO CHECK FOR NEIGHBOR MINES
for (int rows = 0; rows < sideSize; rows++){
for (int cols =0; cols < sideSize; cols++){
neighbor(grid, rows, cols, sideSize);
}
}
return grid;
} // populate ()
// =============================================================================================================================
//RECURSIVE METHOD THAT REVEALS ALL EMPTY AND MINE-NEIGHBORING CELLS
public static void revealneighbors (Cell [][] grid, int row, int col, int size) {
System.out.println("Running on: " + row + " | " + col );
if(grid[row][col].getMinedNeighbors() != 0 && grid[row][col].getMined() == false) {
grid[row][col].setVisible();
return;
}
else {
for (int i = row - 1; i <= row + 1; i++){
for (int k = col -1; k <= col + 1; k++){
if(i == row && k == col ) {
continue;
}
if (i <= -1 || k <= -1 || i >=size || k >= size) {
continue;
}
if(grid[i][k].getMinedNeighbors() == 0 && grid[i][k].getVisibility() == false) {
grid[i][k].setVisible();
revealneighbors(grid, i, k, size);
}
grid[i][k].setVisible();
}
}
}
}
//SHOWS # OF ADJACENT MINES
public static int neighbor (Cell [][] grid, int row, int col, int size) {
for (int i = row - 1; i <= row + 1; i++){
for (int k = col -1; k <= col + 1; k++){
if (i <= -1 || k <= -1 || i >=size || k >= size) {
continue;
}
if(grid[i][k].getMined() == true) {
if(i == row && k == col ) {
continue;
}
grid[row][col].countLiveNeighbor();
}
}
}
return grid[row][col].getMinedNeighbors();
}
// =============================================================================================================================
public static void play (Cell[][] mines) {
// PLAY THE GAME.
} // play ()
// =============================================================================================================================
// =============================================================================================================================
public static void showGrid (Cell[][] mines) {
System.out.println();
// Show the column numbers
System.out.print("\t");
for (int col = 0; col < mines[0].length; col = col + 1) {
System.out.print("\t" + col);
}
System.out.println();
System.out.print("\t");
for (int col = 0; col < mines[0].length; col = col + 1) {
System.out.print("--------");
}
System.out.println();
// Show the grid...
for (int row = 0; row < mines.length; row = row + 1) {
// ...with leading row numbers.
System.out.print(row + "\t|\t");
for (int col = 0; col < mines[row].length; col = col + 1) {
System.out.print(mines[row][col].toString() + '\t');
}
System.out.println();
}
System.out.println();
} // showGrid ()
// =============================================================================================================================
// =============================================================================================================================
public static int[] getCommand (int gridSize) {
System.out.print("Enter a move (H for help): ");
String[] entry = keyboard.nextLine().toUpperCase().split("\\s+");
if (entry.length != 3 || entry[0].length() != 1) {
printMenu();
return getCommand(gridSize);
}
int op = -1;
if (entry[0].charAt(0) == 'R') {
op = REVEAL;
} else if (entry[0].charAt(0) == 'F') {
op = FLAG;
} else {
printMenu();
return getCommand(gridSize);
}
int row = -1;
int col = -1;
try {
row = Integer.parseInt(entry[1]);
col = Integer.parseInt(entry[2]);
} catch (NumberFormatException e) {
printMenu();
return getCommand(gridSize);
}
if (! (1 <= row && row <= gridSize) && (1 <= col && col <= gridSize) ) {
printMenu();
return getCommand(gridSize);
}
return new int[] { op, row, col };
}
// =============================================================================================================================
// =============================================================================================================================
public static void printMenu () {
System.out.println("Command format: [R|F|H] [row #] [col #]\n" +
"\tR = Reveal a cell\n" +
"\tF = Flag a cell\n" +
"\tH = Help (this info)\n" +
"\tExample: R 3 6 (to reveal the cell in row 3, column 6");
}
// =============================================================================================================================
// =================================================================================================================================
} // class Minesweeper
// =================================================================================================================================