-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameboard.java
More file actions
292 lines (230 loc) · 8.73 KB
/
Copy pathGameboard.java
File metadata and controls
292 lines (230 loc) · 8.73 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
import java.util.Scanner;
import java.util.Random;
import java.util.Stack;
public class Gameboard {
public Cell[][] currentBoard = new Cell[10][10];
Stack expandStack_ToDo = new Stack();
Stack expandStack_Done = new Stack();
// Constructor - defines a game board with random mines placed
public Gameboard() {
setGameboard();
}
private void setGameboard() {
Random generator = new Random();
int mineCount = 0;
// Initialize each cell on 10X10 grid with a dash
for (int i = 0; i < currentBoard.length; i++) {
for (int j = 0; j < currentBoard[i].length; j++) {
currentBoard[i][j] = new Cell("-", false, i, j);
}
}
while (mineCount < 10) {
int randomRow;
int randowmColumn;
Cell mineCell;
randomRow = generator.nextInt(8) + 1;
randowmColumn = generator.nextInt(8) + 1;
mineCell = currentBoard[randomRow][randowmColumn];
if (!mineCell.getMineStatus()) {
currentBoard[randomRow][randowmColumn].setCharacter("M");
currentBoard[randomRow][randowmColumn].setMineStatus(true);
mineCount++;
}
}
}
private boolean checkWinner() {
int unknownCellsCount = 0;
// game over if all cells revealed
for (int i = 1; i < currentBoard.length - 1; i++) {
for (int j = 1; j < currentBoard[i].length - 1; j++) {
if (currentBoard[i][j].getMineCharacter().equals("-")) {
unknownCellsCount++;
}
}
}
return (unknownCellsCount == 0 ? true : false);
}
private void checkExpansionNeighbors(Cell expandCell) {
Cell pushCell;
// add this cells neighbors to the stack for expansion
if ((expandCell.getMineRow() - 1) > 0 && (expandCell.getMineColumn() - 1) > 0) {
pushCell = currentBoard[expandCell.getMineRow() - 1][expandCell.getMineColumn() - 1];
if (!expandStack_ToDo.contains(pushCell) && !expandStack_Done.contains(pushCell)) {
expandStack_ToDo.push(pushCell);
}
}
if ((expandCell.getMineRow() - 1) > 0) {
pushCell = currentBoard[expandCell.getMineRow() - 1][expandCell.getMineColumn()];
if (!expandStack_ToDo.contains(pushCell) && !expandStack_Done.contains(pushCell)) {
expandStack_ToDo.push(pushCell);
}
}
if ((expandCell.getMineRow() - 1) > 0 && (expandCell.getMineColumn() + 1) <= 8) {
pushCell = currentBoard[expandCell.getMineRow() - 1][expandCell.getMineColumn() + 1];
if (!expandStack_ToDo.contains(pushCell) && !expandStack_Done.contains(pushCell)) {
expandStack_ToDo.push(pushCell);
}
}
if ((expandCell.getMineColumn() - 1) > 0) {
pushCell = currentBoard[expandCell.getMineRow()][expandCell.getMineColumn() - 1];
if (!expandStack_ToDo.contains(pushCell) && !expandStack_Done.contains(pushCell)) {
expandStack_ToDo.push(pushCell);
}
}
if ((expandCell.getMineColumn() + 1) <= 8) {
pushCell = currentBoard[expandCell.getMineRow()][expandCell.getMineColumn() + 1];
if (!expandStack_ToDo.contains(pushCell) && !expandStack_Done.contains(pushCell)) {
expandStack_ToDo.push(pushCell);
}
}
if ((expandCell.getMineRow() + 1) <= 8 && (expandCell.getMineColumn() - 1) > 0) {
pushCell = currentBoard[expandCell.getMineRow() + 1][expandCell.getMineColumn() - 1];
if (!expandStack_ToDo.contains(pushCell) && !expandStack_Done.contains(pushCell)) {
expandStack_ToDo.push(pushCell);
}
}
if ((expandCell.getMineRow() + 1) <= 8) {
pushCell = currentBoard[expandCell.getMineRow() + 1][expandCell.getMineColumn()];
if (!expandStack_ToDo.contains(pushCell) && !expandStack_Done.contains(pushCell)) {
expandStack_ToDo.push(pushCell);
}
}
if ((expandCell.getMineRow() + 1) <= 8 && (expandCell.getMineColumn() + 1) <= 8) {
pushCell = currentBoard[expandCell.getMineRow() + 1][expandCell.getMineColumn() + 1];
if (!expandStack_ToDo.contains(pushCell) && !expandStack_Done.contains(pushCell)) {
expandStack_ToDo.push(pushCell);
}
}
}
private void expand(Cell expandCell) {
expandStack_Done.clear(); // new iteration so clear out this stack
checkExpansionNeighbors(expandCell);
while (!expandStack_ToDo.empty()) {
Cell popCell = (Cell) expandStack_ToDo.pop();
if (!popCell.getMineStatus()) {
// process cells that done have a mine
int neighborCount = countNeighbors(popCell.getMineRow(), popCell.getMineColumn());
popCell.setCharacter(String.valueOf(neighborCount));
if (neighborCount == 0) {
checkExpansionNeighbors(popCell); // keep goin as no neighbor is a mine or adjacant to one
}
}
expandStack_Done.push(popCell); // tag this cell as done
}
}
public void run() {
boolean gameOn = true;
printBoard(currentBoard, false);
peekMines();
int userRow = getUserInput("row", 1, 8);
int userColumn = getUserInput("column", 1, 8);
String mineGuess = getUserMineGuess();
while (gameOn) {
if (mineGuess.equalsIgnoreCase("yes") || (mineGuess.equalsIgnoreCase("y"))
&& currentBoard[userRow][userColumn].getMineStatus() == false) {
System.out.println("The guess was incorrect, you lose. There was not a mine located here.");
gameOn = false;
} else if (mineGuess.equalsIgnoreCase("no")
|| (mineGuess.equalsIgnoreCase("n")) && currentBoard[userRow][userColumn].getMineStatus() == true) {
System.out.println("The guess was incorrect, you lose. There was a mine located here.");
gameOn = false;
} else if (mineGuess.equalsIgnoreCase("yes")
|| (mineGuess.equalsIgnoreCase("y")) && currentBoard[userRow][userColumn].getMineStatus() == true) {
System.out.println("The guess was correct. There was a mine located here.");
} else if (mineGuess.equalsIgnoreCase("no") || (mineGuess.equalsIgnoreCase("n"))
&& currentBoard[userRow][userColumn].getMineStatus() == false) {
System.out.println("The guess was correct. There was not a mine located here.");
int neighborCount = countNeighbors(userRow, userColumn);
currentBoard[userRow][userColumn].setCharacter(String.valueOf(neighborCount));
if (neighborCount == 0) {
expand(currentBoard[userRow][userColumn]); // expand the board
}
}
if (gameOn && checkWinner()) {
gameOn = false;
System.out.println("You have WON the world Minesweeper championship!! Congratulations!!!!!.");
}
if (gameOn) {
printBoard(currentBoard, false);
peekMines();
userRow = getUserInput("row", 1, 8);
userColumn = getUserInput("column", 1, 8);
mineGuess = getUserMineGuess();
}
}
}
// Prints the entire game board minus the outer grid, showing location of all
// mines
public void printBoard(Cell[][] board, boolean peek) {
for (int i = 1; i < board.length - 1; i++) {
for (int j = 1; j < board[i].length - 1; j++) {
Cell printCell = board[i][j];
if (!peek && printCell.getMineStatus()) {
System.out.print("-");
} else
System.out.print(printCell);
if (j < board[i].length - 1)
System.out.print(" ");
}
System.out.println();
}
}
private int countNeighbors(int row, int column) {
int counter = 0;
if (currentBoard[row - 1][column - 1].getMineStatus())
counter++;
if (currentBoard[row - 1][column].getMineStatus())
counter++;
if (currentBoard[row - 1][column + 1].getMineStatus())
counter++;
if (currentBoard[row][column - 1].getMineStatus())
counter++;
if (currentBoard[row][column + 1].getMineStatus())
counter++;
if (currentBoard[row + 1][column - 1].getMineStatus())
counter++;
if (currentBoard[row + 1][column].getMineStatus())
counter++;
if (currentBoard[row + 1][column + 1].getMineStatus())
counter++;
return counter;
}
// Gets the user input for row
public int getUserInput(String msg, int minNumber, int maxNumber) {
Scanner scan = new Scanner(System.in);
int row = 0;
System.out.println("Please enter " + msg + " between " + minNumber + " and " + maxNumber + " : ");
try {
row = scan.nextInt();
if (row < minNumber || row > maxNumber)
throw new Exception("Invalid number");
} catch (Exception e) {
System.out.println("Invalid input. Please try again.");
getUserInput(msg, minNumber, maxNumber);
}
return row;
}
// Gets the user input for mine guess
private String getUserMineGuess() {
Scanner scan = new Scanner(System.in);
System.out.println("Does this cell contain a mine? (y/n) ");
String userMineGuess = scan.nextLine();
if (!userMineGuess.matches("(?i)yes|y|no|n")) {
System.out.println("Invalid input. Please try again.");
getUserMineGuess();
}
return userMineGuess;
}
// If user says yes, prints the current game board with location of mines
private void peekMines() {
Scanner scan = new Scanner(System.in);
System.out.println("Would you like to peek the mines? (y/n)");
String userResponse = scan.nextLine();
if (userResponse.matches("(?i)yes|y")) {
printBoard(currentBoard, true);
} else if (!userResponse.matches("(?i)no|n")) {
System.out.println("Invalid input. Please try again.");
peekMines();
}
}
}