-
Notifications
You must be signed in to change notification settings - Fork 0
/
Memory.java
277 lines (226 loc) · 8.18 KB
/
Memory.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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Memory extends Game implements Endable {
// SINGLETON
private static Memory memoryInstance = null;
// ATTRIBUTES
private final int minNumTile = 0;
private final int maxNumTile = 9;
// CONSTRUCTOR + SINGLETON PROVIDER
private Memory() {
super();
this.initializeBoard();
super.getPlayers().add(new Player(1, null));
}
public static synchronized Memory getInstance() {
if (memoryInstance == null)
memoryInstance = new Memory();
return memoryInstance;
}
// GETTERS
public int getMinNumTile() {
return minNumTile;
}
public int getMaxNumTile() {
return maxNumTile;
}
// ABSTRACT METHOD IMPLEMENTATIONS
@Override
public void gameLoop(Scanner scanner) {
System.out.println("MEMORY RULES");
System.out.println(" 1. Match the numbers 0-9.");
System.out.println(" 2. You have 3 lives.");
System.out.println(" 3. You loose a life if you mis-match two tiles.");
try {
Thread.sleep(8000); // Pause for 8 seconds
} catch (InterruptedException e) {}
this.showAllValues();
super.display();
System.out.println("MEMORIZE THE BOARD.");
System.out.println("You will have 20 seconds.");
try {
Thread.sleep(20000); // Pause for 20 seconds
} catch (InterruptedException e) {}
boolean gameIsOver = false;
while (!gameIsOver) {
this.hideAllValues();
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
super.display();
this.takeTurn(scanner);
gameIsOver = isGameOver();
}
this.end();
}
@Override
public void takeTurn(Scanner scanner) {
// announce scores
for (Player player : super.getPlayers()) {
int livesLeft = 3-player.getScore();
System.out.println("LIVES LEFT: " + livesLeft + "\n");
}
// get moves from user + check validity
String move = this.getMove(scanner);
// do the move
this.executeMove(move);
super.display();
// check for matches & update status of game
this.checkMatch();
}
@Override
public String getMove(Scanner scanner) {
MemoryBoard board = getMemoryBoard();
if (!(board == null)) {
// allow user to pick two tiles to match together
String tile1 = null;
String tile2 = null;
boolean pickedTwo = false;
while (!pickedTwo) {
String tile;
do {
if (tile1 == null) {
System.out.println("***** Choose your FIRST tile.");
} else {
System.out.println("***** Choose your SECOND tile.");
}
// tile = <ROW> <COLUMN>
tile = this.choose(scanner);
}
while (!board.isValidMove(tile));
// ensure both tiles have been picked
if (tile1 == null) {
tile1 = tile;
} else {
tile2 = tile;
pickedTwo = true;
}
}
// the whole move is a String "<tile1ROW> <tile1COLUMN>,<tile2ROW> <tile2COLUMN>"
return tile1 + "," + tile2;
}
return null; // MAJOR ISSUE IF THIS CODE IS REACHED
}
@Override
public String choose(Scanner scanner) {
// ensures that the chosen move is a valid column number
MemoryBoard board = this.getMemoryBoard();
if (!(board == null)) {
int maxColumns = board.getColumns();
int minColumns = 1;
int maxRows = board.getRows();
int minRows = 1;
int tileRow = super.parseNumber(scanner, "Enter the ROW number of your choice: ", minRows, maxRows);
int tileColumn = super.parseNumber(scanner, "Enter the COLUMN number of your choice: ", minColumns, maxColumns);
return tileRow + " " + tileColumn;
}
return null; // MAJOR ISSUE IF THIS CODE IS REACHED
}
@Override
public void executeMove(String move) {
// move = "<tile1ROW> <tile2COLUMN>,<tile2ROW> <tile2COLUMN>"
String[] chosenTiles = move.split(",");
List<Tile> moves = new ArrayList<>();
MemoryBoard board = this.getMemoryBoard();
if (!(board == null)) {
// parse choices into moves
for (String chosenTile : chosenTiles) {
// chosenTile = "<tile1ROW> <tile2COLUMN>
String[] parts = chosenTile.split(" ");
int row = Integer.parseInt(parts[0])-1;
int col = Integer.parseInt(parts[1])-1;
moves.add(board.getGameBoard().get(row).get(col));
}
board.execute(moves, super.getPlayers().get(0));
}
}
@Override
public void checkMatch() {
MemoryBoard board = this.getMemoryBoard();
if (!(board == null)) {
List<Tile> matches = board.checkMatches();
if (matches != null) {
// make the Tiles disappear
System.out.println("Found a match.");
board.removeMatchedTiles(matches);
} else {
System.out.println("NOT A MATCH");
this.addPlayerPoint(null);
}
try {
// Pause for 3 seconds to give user one second to check displays
Thread.sleep(3000);
} catch (InterruptedException e) {}
}
}
@Override
public void addPlayerPoint(List<Tile> tiles) {
this.getPlayers().get(0).addPoint();
}
@Override
public boolean isGameOver() {
MemoryBoard board = this.getMemoryBoard();
if (!(board == null)) {
boolean noLivesLeft = checkPlayerScore();
boolean allMatched = board.isFull();
if (allMatched) {
System.out.println("SUCCESS! You matched all tiles.");
}
if (noLivesLeft) {
System.out.println("No lives left.");
}
return (noLivesLeft || allMatched);
}
return true; // ERROR: board is not ConnectFourBoard type
}
@Override
public boolean checkPlayerScore() {
// if a score is greater than or equal to 3 then game is over = all 3 lives are lost
return (super.getPlayers().get(0).getScore() >= 3);
}
@Override
public void end() {
System.out.println("\n===================================================\n");
System.out.println("MEMORY: GAME OVER\n");
memoryInstance = null;
}
// HELPER FUNCTIONS
private MemoryBoard getMemoryBoard() {
if (super.getGameBoard() instanceof MemoryBoard) {
return (MemoryBoard) super.getGameBoard();
}
return null;
}
private void initializeBoard() {
List<Integer> numOptions = new ArrayList<>();
for (int i=this.getMinNumTile(); i<=this.getMaxNumTile(); i++) {
numOptions.add(i);
}
super.setGameBoard(new MemoryBoard(numOptions));
}
private void hideAllValues() {
MemoryBoard board = this.getMemoryBoard();
if (!(board == null)) {
for (List<Tile> row : board.getGameBoard()) {
for (Tile tile : row) {
if (tile.getDisplay() != null && !tile.getDisplay().equals("X")) {
if (tile instanceof ValueTile memTile) {
memTile.hideValue();
}
}
}
}
}
}
private void showAllValues() {
MemoryBoard board = this.getMemoryBoard();
if (!(board == null)) {
for (List<Tile> row : board.getGameBoard()) {
for (Tile tile : row) {
if (tile instanceof ValueTile memTile) {
memTile.showValue();
}
}
}
}
}
}