-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.java
347 lines (306 loc) · 8.13 KB
/
Game.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
/*
* A game of Minesweeper.
*/
import java.util.Scanner;
public class Game {
private Board board;
private boolean isOver;
private boolean isFirstMove;
public Game() {
resetGame();
}
// Display the title screen and launch into the game should the user
// request to.
public void start() {
printTitle();
String response = titlePrompt();
if (response.contentEquals("start")) {
gameLoop();
}
printProgramExit();
}
// The main loop of the game.
private void gameLoop() {
String response = "yes";
while (!response.contentEquals("exit") && isYes(response)) {
while (!response.contentEquals("exit") && !isOver) {
board.showRemainingMines();
board.display();
response = turnPrompt();
// Handle validated user input.
if (isScan(response)) {
// User cannot be allowed to lose on the first move of the game
// so we have to make sure that the tile they scanned isn't a
// mine.
if (isFirstMove) {
// Keep generating a new board until we get one that
// doesn't have a mine on the scanned tile.
while (board.tileIsMine(inputToCoordinate(response))) {
board = new Board();
}
isFirstMove = false;
}
board.revealTiles(inputToCoordinate(response));
gameOverCheck(inputToCoordinate(response));
if (isOver) {
if (board.onlyMinesRemain()) {
board.revealAll();
board.display();
System.out.println("Congratulations, you win!");
}
else {
board.revealMines();
board.display();
System.out.println("You hit a mine!");
}
}
}
else if (isMark(response)) {
board.markTile(inputToCoordinate(response.substring(1)));
}
}
if (!response.contentEquals("exit")) {
response = playAgainPrompt();
if (isYes(response)) {
System.out.println("Starting a new game...");
resetGame();
}
}
}
}
// Display the text-art title of the game.
private void printTitle() {
System.out.println("# # ##### # # ##### #### # # ##### ##### #### ##### ####");
System.out.println("## ## # ## # # # # # # # # # # # #");
System.out.println("# # # # # # # ## ### # # # ## ## #### ## ####");
System.out.println("# # # # ## # # # # # # # # # # #");
System.out.println("# # ##### # # ##### #### # # ##### ##### # ##### # #\n");
}
// Prompt user for whether they would like to start a game of Minesweeper,
// exit, or display the help menu
private String titlePrompt() {
Scanner input = new Scanner(System.in);
String response = "";
while (!response.contentEquals("start") && !response.contentEquals("exit")) {
System.out.println("Enter 'start' to start a new game, 'exit' to exit, or 'help' to list in-game controls.");
System.out.print("> ");
response = input.nextLine();
boolean valid = response.contentEquals("start") || response.contentEquals("exit") || response.contentEquals("help");
if (!valid) {
System.out.println("Please enter a valid option.\n");
}
else if (response.contentEquals("help")) {
printHelp();
}
}
return response;
}
// Display a list of valid inputs to the user.
private void printHelp() {
System.out.println("\nCommand list:");
System.out.println("A1 - Where 'A' is a letter from A-I and '1' is a number from 1-9. Scan a tile.");
System.out.println("!A1 - Mark or unmark a tile.");
System.out.println("exit - Quit the game.");
System.out.println("help - Show this menu again.\n");
}
// Display a message on program exit thanking the user for playing the game.
private void printProgramExit() {
System.out.println("\nThank you for playing!");
}
// Ask the user for an input and validate it. Returns the user's move for
// the turn as a string.
private String turnPrompt() {
String response = "";
Scanner input = new Scanner(System.in);
boolean valid = false;
while (!valid || response.contentEquals("help")) {
System.out.print("> ");
response = input.nextLine();
if (response.contentEquals("exit")) {
valid = true;
}
else if (response.contentEquals("help")) {
valid = true;
printHelp();
}
else if (isScan(response)) {
if (!board.tileIsRevealed(inputToCoordinate(response))) {
if (!board.tileIsMarked(inputToCoordinate(response))) {
valid = true;
}
else {
valid = false;
System.out.println("You can't scan a tile that is marked!\n");
}
}
else {
valid = false;
System.out.println("That tile has already been scanned!\n");
}
}
else if (isMark(response)) {
if (!board.tileIsRevealed(inputToCoordinate(response.substring(1)))) {
valid = true;
}
else {
valid = false;
System.out.println("You can't mark a tile that has already been scanned!\n");
}
}
else {
valid = false;
System.out.println("Input not valid! Enter 'help' if needed.\n");
}
}
return response;
}
// Takes a string with a length of two and Returns it as a Coordinate
// matching that tile on the board.
private Coordinate inputToCoordinate(String str) {
Coordinate tile = new Coordinate();
char upper = Character.toUpperCase(str.charAt(0));
switch (upper) {
case 'A':
tile.x = 0;
break;
case 'B':
tile.x = 1;
break;
case 'C':
tile.x = 2;
break;
case 'D':
tile.x = 3;
break;
case 'E':
tile.x = 4;
break;
case 'F':
tile.x = 5;
break;
case 'G':
tile.x = 6;
break;
case 'H':
tile.x = 7;
break;
case 'I':
tile.x = 8;
break;
default:
tile.x = -1;
break;
}
// One is subtracted from the user input row because the rows as
// indexed in the array start at zero and the rows shown on the
// board visual presented to the user start at 1.
tile.y = Character.getNumericValue(str.charAt(1)) - 1;
return tile;
}
// Returns true if the passed string is in the form of a scan command.
private boolean isScan(String str) {
if (str.length() == 2) {
if (isColumn(str.charAt(0)) && isRow(str.charAt(1))) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
// Returns true if the passed string is in the form of a mark command.
private boolean isMark(String str) {
if (str.length() == 3) {
if (str.charAt(0) == '!' && isColumn(str.charAt(1)) && isRow(str.charAt(2))) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
// Takes a character and returns true if the character is within the valid
// range for columns.
private boolean isColumn(char ch) {
boolean isColumn;
char upper = Character.toUpperCase(ch);
switch (upper) {
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
isColumn = true;
break;
default:
isColumn = false;
break;
}
return isColumn;
}
// Takes a character and returns true if that character is within the valid
// range for rows.
private boolean isRow(char ch) {
boolean isRow = false;
switch (ch) {
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
isRow = true;
break;
default:
isRow = false;
break;
}
return isRow;
}
// End the game if the scanned tile was a mine or if all non-mine tiles
// have been scanned.
private void gameOverCheck(Coordinate tile) {
if (board.tileIsMine(tile) || board.onlyMinesRemain()) {
isOver = true;
}
}
// Create a new board and reset relevant variables.
private void resetGame() {
board = new Board();
isOver = false;
isFirstMove = true;
}
// Returns true if the passed string can be taken to mean yes.
private boolean isYes(String str) {
String upper = str.toUpperCase();
switch (upper) {
case "YES":
case "Y":
return true;
default:
return false;
}
}
// Ask player if they would like to play again and return their response.
private String playAgainPrompt() {
String response = "";
Scanner input = new Scanner(System.in);
System.out.println("\nWould you like to play again?");
System.out.print("> ");
response = input.nextLine();
return response;
}
}