-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
506 lines (465 loc) · 14.8 KB
/
Main.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
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
/*
* Name: Sean Ruda
* Date: 3/7/22
* Description: Simulates the game of Minesweeper
*/
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
int length = 3; // You can make this any size. . . I will change to rectangle to test
int width = 5; // You can make this any size. . . I recommend 4x4 early for testing
//inital boards and total mines
String[][] key = new String[length][width];
String[][] gameBoard = new String[length][width];
int totalMines = (int)((length * width) * 0.15625);
Scanner input = new Scanner(System.in);
System.out.println("Welcome to Minesweeper!");
//resets both boards, places mines, fills the key board, and prints the game board
resetBoard(gameBoard);
resetBoard(key);
placeMines(key, totalMines);
fillBoard(key);
printBoard(gameBoard);
//prompts the user to pick a row
System.out.println("Pick a cell");
System.out.print("Row: ");
int row = input.nextInt();
//makes sure the row is valid
while(row > gameBoard.length - 1 || row < 0)
{
System.out.println("Pick a cell inbounds");
System.out.print("Row: ");
row = input.nextInt();
}
//prompts the user to pick a column
System.out.print("Col: ");
int col = input.nextInt();
//makes sure the column is valid
while(col > gameBoard[0].length - 1 || col < 0)
{
System.out.println("Pick a cell inbounds");
System.out.print("Col: ");
col = input.nextInt();
}
//makes sure the 1st pick isn't a mine
boolean check = true;
if(!(makeMove(gameBoard, key, row, col)))
{
check = false;
}
//prints the game board
System.out.println();
printBoard(gameBoard);
//makes sure game board isn't full and user didn't pick a mine on first pick
while(gameBoardFull(gameBoard) != true && check)
{
System.out.println("Would you like to place a mine? 1 for yes");
int minePlace = input.nextInt();
//if the user wants to place a mine
if(minePlace == 1)
{
System.out.println("Pick a cell");
System.out.print("Row: ");
row = input.nextInt();
while(row > gameBoard.length - 1 || row < 0)
{
System.out.println("Pick a cell inbounds");
System.out.print("Row: ");
row = input.nextInt();
}
System.out.print("Col: ");
col = input.nextInt();
while(col > gameBoard[0].length - 1 || col < 0)
{
System.out.println("Pick a cell inbounds");
System.out.print("Col: ");
col = input.nextInt();
}
//sets the gameboard to a mine and makes the move at that point
gameBoard[row][col] = "M";
makeMove(gameBoard, key, row, col);
}
//the user doesn't want to pick a mine, just reveal the cell
else
{
System.out.println("Pick a cell");
System.out.print("Row: ");
row = input.nextInt();
while(row > gameBoard.length - 1 || row < 0)
{
System.out.println("Pick a cell inbounds");
System.out.print("Row: ");
row = input.nextInt();
}
System.out.print("Col: ");
col = input.nextInt();
while(col > gameBoard[0].length - 1 || col < 0)
{
System.out.println("Pick a cell inbounds");
System.out.print("Col: ");
col = input.nextInt();
}
//if the cell revealed is an M break out of the for loop
if(!(makeMove(gameBoard, key, row, col)))
{
break;
}
}
//prints the board at the end
System.out.println();
printBoard(gameBoard);
}
//after the loop this cchecks if the boards match
if(checkValidMines(key, gameBoard) && gameBoardFull(gameBoard) == true)
{
System.out.println("\nCongratulations! Winner Winner!");
}
//if the mines are not in the right place
else if(!(checkValidMines(key, gameBoard)) && gameBoardFull(gameBoard) == true)
{
System.out.println("\nYou lose! Your mines were not in the correct place.");
System.out.println("Here's the key:");
printBoard(key);
}
//else the user hit a mine and lost
else
{
System.out.println("\nYOU LOSE!");
printBoard(key);
}
}
/* Description: Places Mines
* Parameters: String[][] theKey, int theTotalMines - needs the total mines and a board to fill
* Return type: void - nothing is returned the key is filled
*/
public static void placeMines(String[][] theKey, int theTotalMines)
{
//creates a variable to keep track of the mines that have been placed
int theMines = 0;
//while loop that randomly places the mines until limit is reached
while(theMines < theTotalMines)
{
int randRow = (int)(Math.random() * theKey.length);
int randCol = (int)(Math.random() * theKey[0].length);
if(theKey[randRow][randCol].equals("_"))
{
theKey[randRow][randCol] = "M";
theMines++;
}
}
}
/* Description: fills key board with space values
* Parameters: String[][] theKey - needs the key board to fill with numbers
* Return type: void - nothing is returned the key is filled
*/
public static void fillBoard(String[][] theKey)
{
for(int row = 0; row < theKey.length; row++)
{
for(int col = 0; col < theKey[row].length; col++)
{
int mineCounter = 0;
if(theKey[row][col] != "M")
{
//first row
if(row == 0)
{
//for first cell in first row
if(col == 0)
{
if(theKey[0][1].equals("M"))
{
mineCounter++;
}
if(theKey[1][1].equals("M"))
{
mineCounter++;
}
if(theKey[1][0].equals("M"))
{
mineCounter++;
}
}
//for last space in first row
else if(col == theKey[row].length - 1)
{
if(theKey[0][theKey[row].length - 2].equals("M"))
{
mineCounter++;
}
if(theKey[1][theKey[row].length - 2].equals("M"))
{
mineCounter++;
}
if(theKey[1][theKey[row].length - 1].equals("M"))
{
mineCounter++;
}
}
//for the rest of the cells in first row
else
{
if(theKey[row][col - 1].equals("M"))
{
mineCounter++;
}
if(theKey[row][col + 1].equals("M"))
{
mineCounter++;
}
if(theKey[row + 1][col - 1].equals("M"))
{
mineCounter++;
}
if(theKey[row + 1][col].equals("M"))
{
mineCounter++;
}
if(theKey[row + 1][col + 1].equals("M"))
{
mineCounter++;
}
}
}
//for the last row
else if(row == theKey.length - 1)
{
//for first space in last row
if(col == 0)
{
if(theKey[theKey.length - 1][1].equals("M"))
{
mineCounter++;
}
if(theKey[theKey.length - 2][1].equals("M"))
{
mineCounter++;
}
if(theKey[theKey.length - 2][0].equals("M"))
{
mineCounter++;
}
}
//for the last space in last row
else if(col == theKey[row].length - 1)
{
if(theKey[theKey.length - 1][theKey[row].length - 2].equals("M"))
{
mineCounter++;
}
if(theKey[theKey.length - 2][theKey[row].length - 1].equals("M"))
{
mineCounter++;
}
if(theKey[theKey.length - 2][theKey[row].length - 2].equals("M"))
{
mineCounter++;
}
}
//for the rest of the cells in last row
else
{
if(theKey[row][col - 1].equals("M"))
{
mineCounter++;
}
if(theKey[row][col + 1].equals("M"))
{
mineCounter++;
}
if(theKey[row - 1][col - 1].equals("M"))
{
mineCounter++;
}
if(theKey[row - 1][col].equals("M"))
{
mineCounter++;
}
if(theKey[row - 1][col + 1].equals("M"))
{
mineCounter++;
}
}
}
//for the first column
else if(col == 0)
{
if(theKey[row - 1][col].equals("M"))
{
mineCounter++;
}
if(theKey[row + 1][col].equals("M"))
{
mineCounter++;
}
if(theKey[row - 1][col + 1].equals("M"))
{
mineCounter++;
}
if(theKey[row][col + 1].equals("M"))
{
mineCounter++;
}
if(theKey[row + 1][col + 1].equals("M"))
{
mineCounter++;
}
}
//for the last column
else if(col == theKey[row].length - 1)
{
if(theKey[row - 1][col].equals("M"))
{
mineCounter++;
}
if(theKey[row + 1][col].equals("M"))
{
mineCounter++;
}
if(theKey[row - 1][col - 1].equals("M"))
{
mineCounter++;
}
if(theKey[row][col - 1].equals("M"))
{
mineCounter++;
}
if(theKey[row + 1][col - 1].equals("M"))
{
mineCounter++;
}
}
//for the rest of the cells
else
{
if(theKey[row - 1][col - 1].equals("M"))
{
mineCounter++;
}
if(theKey[row - 1][col].equals("M"))
{
mineCounter++;
}
if(theKey[row - 1][col + 1].equals("M"))
{
mineCounter++;
}
if(theKey[row][col - 1].equals("M"))
{
mineCounter++;
}
if(theKey[row][col + 1].equals("M"))
{
mineCounter++;
}
if(theKey[row + 1][col - 1].equals("M"))
{
mineCounter++;
}
if(theKey[row + 1][col].equals("M"))
{
mineCounter++;
}
if(theKey[row + 1][col + 1].equals("M"))
{
mineCounter++;
}
}
//sets value in the key equal to how many mines are around the value
theKey[row][col] = "" + mineCounter;
}
}
}
}
/* Description: Prints the board
* Parameters: String[][] arr, the key or the game board needs to be printed
* Return type: void - nothing is returned the boards are filled
*/
public static void printBoard(String[][] arr)
{
//enhanced for loop to print
for(String[] row: arr)
{
for(String space: row)
{
System.out.print(space + " ");
}
System.out.println();
}
}
/* Description: resets the board
* Parameters: String[][] arr, the key or the game board needs to be reset
* Return type: void - nothing is returned the boards are reset
*/
public static void resetBoard(String[][] arr)
{
//sets every cell to an underscore
for(int row = 0; row < arr.length; row++)
{
for(int col = 0; col < arr[row].length; col++)
{
arr[row][col] = "_";
}
}
}
/* Description: checks if the mines in each board match
* Parameters: String[][] theKey, String[][] theGameBoard
* Return type: boolean - returns true if there are
*/
public static boolean checkValidMines(String[][] theKey, String[][] theGameBoard)
{
//checks every cell for if the mines match
for(int row = 0; row < theGameBoard.length; row++)
{
for(int col = 0; col < theGameBoard[row].length; col++)
{
if(theGameBoard[row][col].equals("M"))
{
if(!(theKey[row][col].equals("M")))
{
return false;
}
}
}
}
return true;
}
/* Description: checks if the mines in each board match
* Parameters: String[][] theGameBoard - needs to check the game board
* Return type: boolean - returns true if the mines are in the correct cells
*/
public static boolean gameBoardFull(String[][] theGameBoard)
{
for(int row = 0; row < theGameBoard.length; row++)
{
for(int col = 0; col < theGameBoard[row].length; col++)
{
if(theGameBoard[row][col].equals("_"))
{
return false;
}
}
}
return true;
}
/* Description: makes the move and changes the board
* Parameters: String[][] theGameBoard, String[][] theKey, int row, int col - uncovers the
* Return type: boolean - returns true if the move is successful return false if not.
*/
public static boolean makeMove(String[][] theGameBoard, String[][] theKey, int row, int col)
{
if(theGameBoard[row][col].equals("M"))
{
}
else
{
theGameBoard[row][col] = theKey[row][col];
if(theKey[row][col].equals("M"))
{
return false;
}
}
return true;
}
}