-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matching.java
361 lines (324 loc) · 11.6 KB
/
Matching.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package org.cis120.matching;
import java.io.*;
import java.util.LinkedList;
import java.util.Random;
public class Matching {
private int[][] board;
private boolean[][] boardState; // true = revealed; false = unrevealed
private boolean oddTurn; // true = odd turn; false = even turn
// storing the game state (the coordinates of the current cards chosen to be
// revealed in that round)
private LinkedList<Integer> currentCoordinates;
// situation where the squares are both opened with different values
private boolean unmatchingMove;
private boolean gameOver;
/**
* Constructor sets up game state.
*/
public Matching() {
reset();
}
/**
* playTurn allows players to play a turn. Returns true if the move is
* successful and false if a player tries to play in a location that is
* taken or after the game has ended.
*
* @param c column to play in
* @param r row to play in
* @return whether the turn was successful
*/
public boolean playTurn(int r, int c) {
// if revealed already or game is over, then invalid move
if (boardState[r][c] || gameOver) {
// printGameState();
return false;
}
if (oddTurn) {
unmatchingMove = false;
boardState[r][c] = true;
// storing the row and column for the first card
currentCoordinates.add(0, r);
currentCoordinates.add(1, c);
currentCoordinates.add(2, 0);
currentCoordinates.add(3, 0);
// set the next turn to be an even turn
oddTurn = false;
// printGameState();
return true;
} else {
// storing the row and column for the second card
currentCoordinates.set(2, r);
currentCoordinates.set(3, c);
if (board[r][c] == board[currentCoordinates.get(0)][currentCoordinates.get(1)]) {
unmatchingMove = false;
boardState[r][c] = true;
oddTurn = true;
// printGameState();
return true;
} else {
unmatchingMove = true;
boardState[currentCoordinates.get(0)][currentCoordinates.get(1)] = false;
boardState[r][c] = false;
oddTurn = true;
// printGameState();
return true;
}
}
}
/**
* hasWon checks whether the game has reached a win condition (all cards
* revealed).
*
* @return true if play has won, false otherwise
*/
public boolean hasWon() {
for (int j = 0; j <= 3; j++) {
for (int k = 0; k <= 3; k++) {
if (!boardState[j][k]) {
return false;
}
}
}
gameOver = true;
return true;
}
/**
* printGameState prints the current game state for debugging.
*/
public void printGameState() {
System.out.println("------------------------------");
for (int i = 0; i < board[0].length; i++) {
for (int j = 0; j < board.length; j++) {
System.out.print(board[j][i] + " ");
}
System.out.println();
}
for (int i = 0; i < boardState[0].length; i++) {
for (int j = 0; j < boardState.length; j++) {
System.out.print(boardState[j][i] + " ");
}
System.out.println();
}
System.out.println(
"first card: (" + currentCoordinates.get(0) + ", " + currentCoordinates.get(1) + ")"
);
System.out.println(
"second card: (" + currentCoordinates.get(2) + ", " + currentCoordinates.get(3)
+ ")"
);
System.out.println("oddTurn: " + oddTurn);
for (int i = 0; i < currentCoordinates.size(); i++) {
System.out.print(currentCoordinates.get(i) + " ");
}
System.out.println();
System.out.println("hasWon: " + hasWon());
}
/**
* save saves the current game to file to play later.
*/
public void save() {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("files/saved.txt", false));
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
bw.write(board[j][i] + " ");
}
bw.write("\n");
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
bw.write(boardState[j][i] + " ");
}
bw.write("\n");
}
bw.write(String.valueOf(oddTurn) + "\n");
for (int i = 0; i < currentCoordinates.size(); i++) {
bw.write(String.valueOf(currentCoordinates.get(i)) + " ");
}
bw.write("\n");
bw.write(String.valueOf(unmatchingMove) + "\n");
bw.write(String.valueOf(gameOver) + "\n");
bw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* load loads an existing game from file for the player to continue playing.
*/
public void load() {
try {
BufferedReader br = new BufferedReader((new FileReader("files/saved.txt")));
if (br == null) {
throw new IllegalArgumentException();
}
try {
for (int i = 0; i < 4; i++) {
String[] line = br.readLine().split(" ");
for (int j = 0; j < 4; j++) {
board[j][i] = Integer.parseInt(line[j]);
}
}
for (int i = 0; i < 4; i++) {
String[] line = br.readLine().split(" ");
for (int j = 0; j < 4; j++) {
boardState[j][i] = Boolean.parseBoolean(line[j]);
}
}
oddTurn = Boolean.parseBoolean(br.readLine());
String[] coord = br.readLine().split(" ");
for (int j = 0; j < currentCoordinates.size(); j++) {
currentCoordinates.set(j, Integer.parseInt(coord[j]));
}
unmatchingMove = Boolean.parseBoolean(br.readLine());
gameOver = Boolean.parseBoolean(br.readLine());
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
} catch (FileNotFoundException ex) {
RunMatching.fileNotFound();
}
}
/**
* reset (re-)sets the game state to start a new game.
*/
public void reset() {
board = new int[4][4]; // value of integers
for (int i = 1; i <= 8; i++) {
int x = 0;
while (x < 2) {
Random rand = new Random();
int randRow = rand.nextInt(4);
int randCol = rand.nextInt(4);
if (board[randRow][randCol] == 0) {
board[randRow][randCol] = i;
x++;
}
}
}
boardState = new boolean[4][4];
for (int j = 0; j <= 3; j++) {
for (int k = 0; k <= 3; k++) {
boardState[j][k] = false;
}
}
oddTurn = true;
currentCoordinates = new LinkedList<Integer>();
currentCoordinates.add(0);
currentCoordinates.add(0);
currentCoordinates.add(0);
currentCoordinates.add(0);
unmatchingMove = false;
gameOver = false;
}
/**
* undo returns the game to the state of its previous move
*/
public void undo() {
if (oddTurn) {
boardState[currentCoordinates.get(0)][currentCoordinates.get(1)] = true;
boardState[currentCoordinates.get(2)][currentCoordinates.get(3)] = false;
currentCoordinates.set(2, 0);
currentCoordinates.set(3, 0);
oddTurn = false;
unmatchingMove = false;
gameOver = false;
} else {
boardState[currentCoordinates.get(0)][currentCoordinates.get(1)] = false;
currentCoordinates.removeFirst();
currentCoordinates.removeFirst();
currentCoordinates.removeFirst();
currentCoordinates.removeFirst();
int oddr = currentCoordinates.get(0);
int oddc = currentCoordinates.get(1);
int evenr = currentCoordinates.get(2);
int evenc = currentCoordinates.get(3);
oddTurn = true;
playTurn(oddr, oddc);
playTurn(evenr, evenc);
if (!(board[oddr][oddc] == board[evenr][evenc])) {
currentCoordinates.removeFirst();
currentCoordinates.removeFirst();
currentCoordinates.removeFirst();
currentCoordinates.removeFirst();
}
boolean currentCoordinatesAllZero = true;
for (int i = 0; i < currentCoordinates.size(); i++) {
if (!(currentCoordinates.get(i) == 0)) {
currentCoordinatesAllZero = false;
}
}
if (currentCoordinatesAllZero) {
boardState = new boolean[4][4];
for (int j = 0; j <= 3; j++) {
for (int k = 0; k <= 3; k++) {
boardState[j][k] = false;
}
}
oddTurn = true;
currentCoordinates = new LinkedList<Integer>();
currentCoordinates.add(0);
currentCoordinates.add(0);
currentCoordinates.add(0);
currentCoordinates.add(0);
unmatchingMove = false;
gameOver = false;
}
}
}
// returns the value of that particular card
public int getCell(int r, int c) {
return board[r][c];
}
// returns whether that card is revealed or not
public boolean getCellState(int r, int c) {
return boardState[r][c];
}
// returns whether the turn is odd or not
public boolean isOddTurn() {
return oddTurn;
}
// returns the row of the first of two cards (odd turn)
public int getOddr() {
return currentCoordinates.get(0);
}
// returns the column of the first of two cards (odd turn)
public int getOddc() {
return currentCoordinates.get(1);
}
// returns the row of the second of two cards (even turn)
public int getEvenr() {
return currentCoordinates.get(2);
}
// returns the column of the second of two cards (even turn)
public int getEvenc() {
return currentCoordinates.get(3);
}
// returns whether the cards are both opened with different values
public boolean isUnmatchingMove() {
return unmatchingMove;
}
/* **** ****** **** BELOW USED FOR TESTING ONLY **** ****** **** */
public int[][] getBoard() {
return board;
}
public boolean[][] getBoardState() {
return boardState;
}
/**
* This main method illustrates how the model is completely independent of
* the view and controller. We can play the game from start to finish
* without ever creating a Java Swing object.
*
* This is modularity in action, and modularity is the bedrock of the
* Model-View-Controller design framework.
*
* Run this file to see the output of this method in your console.
*/
public static void main(String[] args) {
Matching t = new Matching();
// t.playTurn(1, 1);
t.printGameState();
}
}