-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryGameModel
More file actions
226 lines (200 loc) · 6.48 KB
/
Copy pathMemoryGameModel
File metadata and controls
226 lines (200 loc) · 6.48 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
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
import java.util.ArrayList;
import java.util.Collections;
public class MemoryGameModel {
public enum TileState { FACE_DOWN, FACE_UP, MATCHED }
public class Tile {
private String value;
private TileState state;
/*
* purpose: constructs a new tile with a value and set it face down
* input: value - a String variable that represents what is shown on the card
* output: none
* assumptions: value is not null
*/
public Tile(String value) {
this.value = value;
this.state = TileState.FACE_DOWN;
}
/*
* purpose: returns the value shown on the tile
* input: none
* output: value - a String variable that represents what is shown on the card
* assumptions: none
*/
public String getValue() {
return value;
}
/*
* purpose: returns the state of the tile
* input: none
* output: state - the TileState of the tile (face down, face up, or matched)
* assumptions: none
*/
public TileState getState() {
return state;
}
/*
* purpose: updates the state of the tile
* input: s - the TileState that the tile will take on
* output: none but the state of the tile is updated
* assumptions: s is a valid TileState value
*/
public void setState(TileState s) {
state = s;
}
}
private Tile[][] board;
private int rows;
private int cols;
private Tile firstTile = null;
private Tile secondTile = null;
/*
* purpose: initializes the MemoryGameModel with the number of rows and columns that the game has
* input: rows - an int variable that represents the number of rows
* cols - an int variable that represents the number of columns
* output: none
* assumptions: rows * cols is even
*/
public MemoryGameModel(int rows, int cols) {
this.rows = rows;
this.cols = cols;
setupBoard();
}
/*
* purpose: create and shuffle all tile pairs and place them on the board
* input: none
* output: none but the board field is initialized with shuffled tile pairs
* assumptions: rows * cols is even
*/
private void setupBoard() {
board = new Tile[rows][cols];
ArrayList<String> values = new ArrayList<>();
int pairs = (rows * cols) / 2;
for (int i = 1; i <= pairs; i++) {
values.add("" + i);
values.add("" + i);
}
//shuffles the tiles
Collections.shuffle(values);
int index = 0;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
board[r][c] = new Tile(values.get(index++));
}
}
// ensure flips are cleared
resetFlips();
}
/*
* purpose: reset and re-initialize the board for a new game (re-shuffle pairs)
* input: none
* output: none but the board is re-created and tile states reset
* assumptions: rows and columns are unchanged and rows*cols is even
*/
public void reset() {
setupBoard();
}
/*
* purpose: return the number of rows on the board
* input: none
* output: rows - an int variable that represents the number of rows on the board
* assumptions: none
*/
public int getRows() {
return rows;
}
/*
* purpose: return the number of columns on the board
* input: none
* output: cols - an int variable that represents the number of columns on the board
* assumptions: none
*/
public int getCols() {
return cols;
}
/*
* purpose: returns the tile at a given position
* input: r - an int variable that represents the row
* c - an int variable that represents the column
* output: board[r][c] - a Tile at board[r][c]
* assumptions: r and c lead to a tile on the board
*/
public Tile getTile(int r, int c) {
return board[r][c];
}
/*
* purpose: flip a tile face-up and track whether it is the first or second flipped tile
* input: r - row index
* c - column index
* output: boolean - true if this flip completes a pair (second tile), false otherwise
* assumptions: r and c are valid indices and the tile is not already matched or face-up
*/
public boolean flipTile(int r, int c) {
Tile t = board[r][c];
if (t.getState() != TileState.FACE_DOWN) return false;
t.setState(TileState.FACE_UP);
if (firstTile == null) {
firstTile = t;
return false;
} else {
secondTile = t;
return true;
}
}
/*
* purpose: determine whether the two currently flipped tiles match
* input: none
* output: boolean - true if the two flipped tiles have the same value
* assumptions: firstTile and secondTile have been set
*/
public boolean checkMatch() {
if (firstTile == null || secondTile == null) return false;
return firstTile.getValue().equals(secondTile.getValue());
}
/*
* purpose: mark the two currently flipped tiles as matched
* input: none
* output: none
* assumptions: firstTile and secondTile are not null
*/
public void markMatched() {
firstTile.setState(TileState.MATCHED);
secondTile.setState(TileState.MATCHED);
resetFlips();
}
/*
* purpose: flip the two currently flipped tiles back face-down
* input: none
* output: none
* assumptions: firstTile and secondTile are not null
*/
public void flipBack() {
firstTile.setState(TileState.FACE_DOWN);
secondTile.setState(TileState.FACE_DOWN);
resetFlips();
}
/*
* purpose: clear the stored first and second tile
* input: none
* output: none
* assumptions: none
*/
private void resetFlips() {
firstTile = null;
secondTile = null;
}
/*
* purpose: check whether every tile has been matched
* input: none
* output: boolean - true if all tiles are matched, false otherwise
* assumptions: board is fully initialized
*/
public boolean allMatched() {
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (board[r][c].getState() != TileState.MATCHED) return false;
}
}
return true;
}
}