-
Notifications
You must be signed in to change notification settings - Fork 0
/
Minesweeper.java
83 lines (72 loc) · 2.82 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
/*----------------------------------------------------------------
* Author: Apurva Gandhi
* Email: [email protected]
* Written: 11/21/2018
* Update:
*
* A game of minesweeper. This class only really holds the
* main() fucntion. Most of the interesting stuff happens in
* the Game and Cell classes.
*
* Example: java Minesweeper # This runs a fresh, randomized game
*
* Example: java Minesweeper 1234 # This repeats the game with seed 1234
*
*----------------------------------------------------------------*/
public class Minesweeper
{
/**
* The main() program for the minesweeper game. It takes one optional
* parameter, an integer seed used for randomizing the mines. If you use the
* same seed twice, you should get the exact same deployment of mines.
*/
public static void main(String args[])
{
// Print a customized welcome message.
String username = System.getenv("USER");
StdOut.println("--=== Welcome to Minesweeper, " + username + "! ===--");
// Initialize the random number generator. We either use a random seed
// provided as an optional command-line argument, or we print out the
// seed being used so the user can play the exact same game again if
// they like.
int seed;
if (args.length == 0)
{
seed = StdRandom.uniform(1000, 9999);
System.out.println("The seed for this game is: " + seed);
System.out.println("If you wish to play this exact same game");
System.out.println("again, use this command: java Minesweeper " + seed);
}
else
{
seed = Integer.parseInt(args[0]);
System.out.println("You are re-playing the game with seed: " + seed);
}
// Create the "splash" title screen, and wait for the user to make a
// selection.
TitleScreen t = new TitleScreen();
t.showAndAnimate(1.0);
// If the user wants to quit, do so.
String selection = t.getSelection();
if (selection.equals("quit"))
{
System.out.println("I'm sorry you don't want to play. Maybe later?");
return;
}
StdRandom.setSeed(seed);
// Create a game, and deploy the correct number of mines.
Game game = new Game(username, selection);
StdRandom.setSeed(seed);
if (selection.equals("hard"))
game.deployMines(100);
else if (selection.equals("medium"))
game.deployMines(20);
else if (selection.equals("easy"))
game.deployMines(5);
else
game.deployMines(StdRandom.uniform(5, 100));
// Wait for the user to play the game. We turn on animation so the timer
// display is updated properly.
game.showAndAnimate(10);
}
}