-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInteractive.java
More file actions
83 lines (79 loc) · 2.47 KB
/
Copy pathInteractive.java
File metadata and controls
83 lines (79 loc) · 2.47 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
// Scanner to read player's inputs
import java.util.Scanner;
// This class interacts with player to play MineSweeper
// Ask user for game size and difficulty to create game
// Then ask user for next move repetitively until user win or lose
public class Interactive
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); // Read inputs
// Ask player for game size
System.out.print("Enter game size (n m): ");
int gameWidth = sc.nextInt();
int gameHeight = sc.nextInt();
//Force the player to choose an integer between 8 and 15
while ((gameWidth < 8 || gameWidth > 20) || (gameHeight < 8 || gameHeight > 20) )
{
System.out.println("ERROR! Invalid size");
System.out.print("Enter game size (n m): ");
gameWidth = sc.nextInt();
gameHeight = sc.nextInt();
}
// Ask player for game difficulty
System.out.print("Enter game difficulty (easy/medium/hard): ");
String gameDifficulty = sc.next().toLowerCase();
// Force the player to choose a valid difficulty String
while (!gameDifficulty.equals("easy") && !gameDifficulty.equals("medium") && !gameDifficulty.equals("hard") )
{
System.out.println("ERROR! Invalid difficulty");
System.out.print("Enter game difficulty (easy/medium/hard): ");
gameDifficulty = sc.next().toLowerCase();
}
// Create new game
MineSweeper game = new MineSweeper(gameWidth, gameHeight, gameDifficulty);
System.out.println(game);
//game.cheat();
Interactive.interact(game);
}
// Keep asking player for next move until gameOver or victory
// Player can choose to flag, unlfag or reveal a tile
public static void interact(MineSweeper game)
{
Scanner sc = new Scanner(System.in); // Read inputs
System.out.print("Next move (flag/unflag/reveal r c): ");
game.reveal(sc.nextInt(), sc.nextInt());
System.out.println(game);
//System.out.println(game.numVisible);
while(!game.lose() && !game.win())
{
System.out.print("Next move (flag/unflag/reveal r c): ");
String nextMove = sc.next();
if(nextMove.equals("flag"))
{
game.flag(sc.nextInt(), sc.nextInt());
}
else if(nextMove.equals("unflag"))
{
game.unflag(sc.nextInt(), sc.nextInt());
}
else
{
game.reveal(sc.nextInt(), sc.nextInt());
}
System.out.println(game);
}
// Check if player win or lose
if(game.win())
{
System.out.println("You WIN!");
}
else
{
// Reveal all tile if game is over
game.revealAll();
System.out.println(game);
System.out.println("Game Over! :(");
}
}
}