-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinesweeper.java
346 lines (332 loc) · 9.58 KB
/
Minesweeper.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
/*
* This class creates the very board of Minesweeper.
*/
import java.util.*;
import javax.swing.*; // For swing classes (the "J" classes)
import java.awt.*; // For awt classes (e.g., Dimension)
import java.awt.event.*; // For events (which you will implement)
public class Minesweeper{
//-----------------------------------------------------------------
//---------------------Instance Variables--------------------------
//-----------------------------------------------------------------
private int bombCount; //Number of bombs on the board
int[][] mineBoard; //Our main mine board: Free(0), Mine(1)
int[][] statusBoard; //The state of our element in mineBoard: Empty(0), Flag(1), Clicked(2), Lose(3)
int[][] flagBoard; //If there is a flag: Empty(0), Flag(1)
private int columns;
private int rows;
private int status; //Status of the board: Playing(0), Lose(1) , Win(2)
private int numRevealed; //Number of revealed cells on the board.
//-----------------------------------------------------------------
//------------------------Constructor------------------------------
//-----------------------------------------------------------------
/**
* Creates a new board with input
* @param inColumns. number of Columns
* @param inRows. number of Rows
* @param inBombCounr. number of Bombs
*/
public Minesweeper(int inColumns, int inRows,int inBombCount){
this.bombCount = inBombCount;
this.mineBoard = new int[inColumns][inRows];
this.statusBoard = new int[inColumns][inRows];
this.flagBoard = new int[inColumns][inRows];
this.columns = inColumns;
this.rows = inRows;
}
/**
* No args Constructor
*/
public Minesweeper(){
this.bombCount = 12;
this.mineBoard = new int[10][8];
this.statusBoard = new int[10][8];
this.flagBoard = new int[10][8];
this.columns = 10;
this.rows = 8;
}
//----------------------------------------------------------------
//-------------------------Methods--------------------------------
//----------------------------------------------------------------
//-------------------Getters----------------------
/**
* Returns the number of columns on the board.
* @return int The number of columns on the board.
*/
public int getColumns(){
return this.columns;
}
/**
* Returns the number of rows on the board.
* @return int The number of rows on the board.
*/
public int getRows(){
return this.rows;
}
/**
* Returns the number of bombs on the board.
* @return int The number of bombs on the board.
*/
public int getBombs(){
return this.bombCount;
}
/**
* Returns the number of revealed cells on the board.
* @return int The number of revealed cells on the board.
*/
public int getNumRevealed(){
return numRevealed;
}
/**
* Returns the status of the board. (Playing = 0, Lose = 1 , Win = 2)
* @return int The status of the board.
*/
public int getStatus(){
return this.status;
}
//-------------------Setters----------------------
/**
* Sets the status of the board.
* @param newStat The new status of the board.
*/
public void setStatus(int newStat){
this.status = newStat;
}
//------------------------------------------------
/**
* Returns whether or not we can make a move.
* Dependant on the value of empty, flag, clicked, or lose, or free, or mine.
* @param x Column of cell.
* @param y Row of cell.
* @return boolean If move is allowed.
*/
public boolean allowsMove(int x, int y){
//ifempty, if flag
if(x >= 0 && x < columns && y >= 0 && y< rows){
if (statusBoard[x][y] == 0){
return true;
}
}
return false;
}
//------------------------------------------------
/**
* Checks if the game is won.
* @return boolean Whether or not the player won.
*/
public boolean win(){
if(status != 1)
{
if((getColumns() * getRows() ) - numRevealed == bombCount)
{
System.out.println("Congratulations!!");
revealerAfterGame();
}
}
return false;
}
//------------------------------------------------
/**
* Checks if the game is lost.
* @param x The column that is revealed.
* @param y The row that is revealed.
* @return boolean Whether or not the player lose.
*/
public boolean lose(int x, int y){
//clicked on bomb
if ( mineBoard[x][y] == 1){
revealerAfterGame();
System.out.println("Try Again!");
return true;
}
else return false;
}
//------------------------------------------------
/**
* Returns the number of mines that are around a box.
* @param xPos the x position of the box.
* @param yPos the y position of the box.
* @return int the number of mines that are around the box.
*/
public int numMinesAround(int xPos, int yPos)
{
int numMines = 0;
for(int x = xPos - 1; x <= xPos + 1; x++)
{
for(int y = yPos - 1; y <= yPos + 1; y++)
{
if(x >= 0 && x < columns && y >= 0 && y< rows)
{
if (mineBoard[x][y] == 1)
{
numMines++;
}
}
}
}
return numMines;
}
//------------------------------------------------
/**
* Returns the number of mines that are around a box.
* @param xPos the x position of the box.
* @param yPos the y position of the box.
* @return int the number of mines that are around the box.
*/
public int numFlagsAround(int xPos, int yPos)
{
int numFlags = 0;
for(int x = xPos - 1; x <= xPos + 1; x++)
{
for(int y = yPos - 1; y <= yPos + 1; y++)
{
if(x >= 0 && x < columns && y >= 0 && y< rows)
{
if (flagBoard[x][y] == 1)
{
numFlags++;
}
}
}
}
return numFlags;
}
//------------------------------------------------
/**
* Reveals a box and other boxes around it until it reaches a mine. The 1st method to call.
* @param xPos the x position of the box.
* @param yPos the y position of the box.
*/
public void revealBox(int xPos, int yPos)
{
if(xPos >= 0 && xPos < columns && yPos >= 0 && yPos < rows && statusBoard[xPos][yPos] !=2
&& flagBoard[xPos][yPos] == 0){
statusBoard[xPos][yPos] = 2;
numRevealed++;
if(statusBoard[xPos][yPos] == 2 && mineBoard[xPos][yPos] == 1){
status = 1;
lose(xPos,yPos);
}
if((getColumns() * getRows() ) - numRevealed == bombCount){
status = 2;
win();
}
if(numMinesAround(xPos,yPos) != 0){
return;
}
else{
revealBox(xPos-1, yPos-1);
revealBox(xPos, yPos-1);
revealBox(xPos+1, yPos-1);
revealBox(xPos-1, yPos);
revealBox(xPos+1, yPos);
revealBox(xPos-1, yPos+1);
revealBox(xPos, yPos+1);
revealBox(xPos+1, yPos+1);
}
}
}
//------------------------------------------------
/**
* Clears the board and resets the game.
*/
public void clearReset(){
for (int r = 0; r < rows ; r++){
for(int c = 0 ; c < columns ; c++){
mineBoard[c][r] = 0;
statusBoard[c][r] = 0;
flagBoard[c][r] = 0;
numRevealed = 0;
status = 0;
}
}
}
//------------------------------------------------
/**
* Runs what happen when we click on a cell.
* @param x Column of the cell.
* @param y Row of the cell.
*/
public void click(int x, int y){
if(status == 0){
if(checkerMines() == false){
spawnRandomMines(x,y);
}
if (allowsMove(x,y) == true ){
if(mineBoard[x][y] == 0){
revealBox(x,y);
}
else{
revealBox(x,y);
statusBoard[x][y] = 3;
status = 1;
lose(x,y);
}
}
else if( x >= 0 && x < columns && y >= 0 && y< rows &&
statusBoard[x][y] == 2 && flagBoard[x][y] == 0){
if(numMinesAround(x,y) == numFlagsAround(x,y)){
revealBox(x-1, y-1);
revealBox(x, y-1);
revealBox(x+1, y-1);
revealBox(x-1, y);
revealBox(x+1, y);
revealBox(x-1, y+1);
revealBox(x, y+1);
revealBox(x+1, y+1);
}
}
}
}
//------------------------------------------------
/**
* Spawns randomly mines everywhere.
* @param x The initial column that is clicked.
* @param y The inital row that is clicked.
*/
public void spawnRandomMines(int x, int y){
//Random Mine Generator
Random numMines = new Random();
int nMines = 0;
while (nMines < getBombs()){
int nR = numMines.nextInt(getRows());
int nC = numMines.nextInt(getColumns());
if(nC != x || nR != y){
if (mineBoard[nC][nR] == 0){
mineBoard[nC][nR] = 1;
nMines++;
}
}
}
}
/**
* Checks the whole board and to see if there are any mines.
* @return boolean if there any mines
*/
public boolean checkerMines(){
for(int r = 0; r < getRows() ; r++){
for(int c = 0; c < getColumns(); c++){
if(mineBoard[c][r] == 1){
return true;
}
}
}
return false;
}
//------------------------------------------------
/**
* Reveals the gameboard after the game is won or lost.
*/
public void revealerAfterGame(){
for(int r = 0; r < getRows() ; r++){
for(int c = 0; c < getColumns(); c++){
if(mineBoard[c][r] == 0){
statusBoard[c][r] = 2;
}
else{
statusBoard[c][r] = 3;
}
}
}
}
}