-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
49 lines (44 loc) · 1.72 KB
/
Main.java
File metadata and controls
49 lines (44 loc) · 1.72 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
package minesweeper;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
boolean validEntry;
int numMines;
int boardWidth;
int boardHeight;
do {
System.out.println("What size board would you like to play on? (width x height)");
boardWidth = scan.nextInt();
boardHeight = Integer.parseInt(scan.nextLine().trim());
System.out.print("How many mines do you want on the field? ");
numMines = Integer.parseInt(scan.nextLine().trim());
validEntry = boardWidth * boardHeight > numMines;
} while (!validEntry);
Minesweeper minesweeper = new Minesweeper(boardWidth, boardHeight, numMines);
while (!minesweeper.isGameOver()) {
System.out.print("Set/unset mines marks or claim a cell as free: ");
String[] input = scan.nextLine().split(" ");
int x = Integer.parseInt(input[0]);
int y = Integer.parseInt(input[1]);
String command = input[2].toLowerCase();
switch (command) {
case "free":
if (minesweeper.doMove(y, x)) {
minesweeper.printBoard();
}
break;
case "mine":
if (minesweeper.placeFlag(y, x)) {
minesweeper.printBoard();
}
break;
}
}
if (minesweeper.isPlayerWin()) {
System.out.println("Congratulations! You found all mines!");
} else {
System.out.println("You stepped on a mine and failed!");
}
}
}