-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
166 lines (144 loc) · 3.77 KB
/
Copy pathGame.java
File metadata and controls
166 lines (144 loc) · 3.77 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
/**
* The game that uses a n x m board of tiles or cards.
*
* Player chooses two random tiles from the board. The chosen tiles
* are temporarily shown face up. If the tiles match, they are removed from board.
*
* Play continues, matching two tiles at a time, until all tles have been matched.
*
* @author PLTW
* @version 2.0
*/
import java.util.Scanner;
/**
* A game class to play concentration
*/
public class Game
{
private Scanner in = new Scanner(System.in);
private Board board;
private int row1, col1;
private int row2, col2;
public void play()
{
// instructions
System.out.println("Welcome!");
System.out.println("Select the tile locations you want to match,");
System.out.println("or enter any non-integer character to quit.");
System.out.println("(You will need to know 2D arrays to play!)");
System.out.println("\nPress Enter to continue...");
in.nextLine();
board = new Board();
// play until all tiles are matched
while (!board.allTilesMatch())
{
// get player's first selection, if not an integer, quit
row1 = -1;
col1 = -1;
boolean validTile = false;
while (!validTile)
{
displayBoard();
System.out.print("First choice (row col): ");
validTile = getTile(true);
}
// display first tile
board.showValue(row1, col1);
// get player's second selection, if not an integer, quit
row2 = -1;
col2 = -1;
validTile = false;
while (!validTile)
{
displayBoard();
System.out.print("Second choice (row col): ");
validTile = getTile(false);
// check if user chosen same tile twice
if ((row1 == row2) && (col1 == col2))
{
System.out.println("You mush choose a different second tile");
wait(2);
validTile = false;
}
}
// display second tile
board.showValue(row2, col2);
displayBoard();
// determine if tiles match
String matched = board.checkForMatch(row1, col1, row2, col2);
System.out.println(matched);
// wait 2 seconds to start the next turn
wait(2);
}
displayBoard();
System.out.println("Game Over!");
}
/**
* Get tile choices from the user, validating that
* the choice falls within the range of rows and columns on the board.
*
* @param firstChoice if true, saves the user's first row/col choice, otherwise sets the user's second choice
* @return true if user has made a valid choice, false otherwise
*/
private boolean getTile(boolean firstChoice)
{
int num1 = 0;
int num2 = 0;
if (in.hasNextInt())
num1 = in.nextInt();
else
quitGame();
if (in.hasNextInt())
num2 = in.nextInt();
else
quitGame();
in.reset(); // ignore any extra input
if (!board.validateSelection(num1, num2))
{
System.out.print("Invalid input, please try again. ");
wait(2);
return false;
}
if (firstChoice)
{
row1 = num1;
col1 = num2;
}
else
{
row2 = num1;
col2 = num2;
}
return true;
}
/**
* Clear the console and show the game board
*/
public void displayBoard()
{
// scroll current board off screen
for (int x = 0; x < 50; x++) {
System.out.println();
}
System.out.println(board);
}
/**
* Wait n seconds before clearing the console
*/
private void wait(int n)
{
// a try is like an if statement, "throwing" an error if the body of the try fails
try
{
Thread.sleep(n * 1000);
} catch (InterruptedException e) { /* do nothing */ }
}
/**
* User quits game
*/
private void quitGame()
{
System.out.println("Quit game!");
System.exit(0);
}
}