-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameModel.java
More file actions
375 lines (322 loc) · 9.2 KB
/
GameModel.java
File metadata and controls
375 lines (322 loc) · 9.2 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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import java.util.Random;
import java.io.*;
/**
* The class <b>GameModel</b> holds the model, the state of the systems.
* It stores the followiung information:
* - the state of all the ``dots'' on the board (color, captured or not)
* - the size of the board
* - the number of steps since the last reset
* - the current color of selection
*
* The model provides all of this informations to the other classes trough
* appropriate Getters.
* The controller can also update the model through Setters.
* Finally, the model is also in charge of initializing the game
*
* @author Guy-Vincent Jourdan, University of Ottawa
* @Modified by Brahim Kanouche, University of Ottawa
*/
public class GameModel implements Cloneable,Serializable {
/**
* predefined values to capture the color of a DotInfo
*/
public static final int COLOR_0 = 0;
public static final int COLOR_1 = 1;
public static final int COLOR_2 = 2;
public static final int COLOR_3 = 3;
public static final int COLOR_4 = 4;
public static final int COLOR_5 = 5;
public static final int NUMBER_OF_COLORS = 6;
/**
* The current selection color
*/
private int currentSelectedColor;
/**
* The size of the game.
*/
private int sizeOfGame;
/**
* A 2 dimentionnal array of sizeOfGame*sizeOfGame recording the state of each dot
*/
private DotInfo[][] model;
/**
* The number of steps played since the last reset
*/
private int numberOfSteps;
/**
* The number of captered dots
*/
private int numberCaptured;
/**
* Type of moves: Torus or plan
*/
private boolean torusMove;
/**
* Type of moves: diagonal or not
*/
private boolean diagMove;
/**
* True iff is the initial dot has been selected
*/
private boolean initialSet;
/**
* The undo stack
*/
private LinkedStack<GameModel> undoStack;
/**
* The redo stack
*/
private LinkedStack<GameModel> redoStack;
/**
* Random generator
*/
private Random generator;
/**
* Constructor to initialize the model to a given size of board.
*
* @param size
* the size of the board
*/
public GameModel(int size) {
generator = new Random();
torusMove = false;
diagMove = false;
sizeOfGame = size;
reset();
}
public boolean isTorusMove(){
return torusMove;
}
public void setTorusMove(boolean torus){
if(torusMove != torus) {
startUpdate();
torusMove = torus;
}
}
public boolean isDiagMove(){
return diagMove;
}
public void setDiagMove(boolean diag){
if(diagMove != diag) {
startUpdate();
diagMove = diag;
}
}
/**
* Resets the model to (re)start a game. The previous game (if there is one)
* is cleared up .
*/
public void reset(){
model = new DotInfo[sizeOfGame][sizeOfGame];
for(int i = 0; i < sizeOfGame; i++){
for(int j = 0; j < sizeOfGame; j++){
model[i][j] = new DotInfo(i,j,generator.nextInt(NUMBER_OF_COLORS));
}
}
// initially, the top left point is captured
//setInitialCaptured(0,0);
numberOfSteps = 0;
numberCaptured = 0;
initialSet = false;
undoStack = new LinkedStack<GameModel>();
redoStack = new LinkedStack<GameModel>();
}
/**
* Captures the initial dot
* @param x
* the x coordinate of the dot
* @param y
* the y coordinate of the dot
*/
public void setInitialCaptured(int x, int y){
for(int i = 0; i < sizeOfGame; i++){
for(int j = 0; j < sizeOfGame; j++){
model[i][j].setCaptured(false);
}
}
currentSelectedColor = model[x][y].getColor();
model[x][y].setCaptured(true);
numberOfSteps = 0;
numberCaptured = 1;
initialSet = true;
}
/**
* Getter method for the size of the game
*
* @return the value of the attribute sizeOfGame
*/
public int getSize(){
return sizeOfGame;
}
/**
* returns the color of a given dot in the game
*
* @param i
* the x coordinate of the dot
* @param j
* the y coordinate of the dot
* @return the status of the dot at location (i,j)
*/
public int getColor(int i, int j){
if(isCaptured(i, j)) {
return currentSelectedColor;
} else {
return model[i][j].getColor();
}
}
/**
* returns true is the dot is captured, false otherwise
*
* @param i
* the x coordinate of the dot
* @param j
* the y coordinate of the dot
* @return the status of the dot at location (i,j)
*/
public boolean isCaptured(int i, int j){
return model[i][j].isCaptured();
}
/**
* Getter method for initialSet
* @return the value of the attribute initialSet
*/
public boolean isInitialSet(){
return initialSet;
}
/**
* Sets the status of the dot at coordinate (i,j) to captured
*
* @param i
* the x coordinate of the dot
* @param j
* the y coordinate of the dot
*/
public void capture(int i, int j){
model[i][j].setCaptured(true);
numberCaptured++;
}
/**
* Getter method for the current number of steps
*
* @return the current number of steps
*/
public int getNumberOfSteps(){
return numberOfSteps;
}
/**
* Setter method for currentSelectedColor
*
* @param val
* the new value for currentSelectedColor
*/
public void setCurrentSelectedColor(int val) {
currentSelectedColor = val;
}
/**
* Getter method for currentSelectedColor
*
* @return currentSelectedColor
*/
public int getCurrentSelectedColor() {
return currentSelectedColor ;
}
/**
* Getter method for the model's dotInfo reference
* at location (i,j)
*
* @param i
* the x coordinate of the dot
* @param j
* the y coordinate of the dot
*
* @return model[i][j]
*/
public DotInfo get(int i, int j) {
return model[i][j];
}
public void startUpdate(){
try{
undoStack.push((GameModel)this.clone());
} catch(CloneNotSupportedException e){
System.out.println("Game model cannot be cloned. No Undo/Redo.");
}
if(!redoStack.isEmpty()){
redoStack = new LinkedStack<GameModel>();
}
}
/**
* The metod <b>step</b> updates the number of steps. It must be called
* once the model has been updated after the payer selected a new color.
*/
public void step(){
numberOfSteps++;
}
/**
* The metod <b>isFinished</b> returns true iff the game is finished, that
* is, all the dats are captured.
*
* @return true if the game is finished, false otherwise
*/
public boolean isFinished(){
return numberCaptured == sizeOfGame*sizeOfGame;
}
/**
* Builds a String representation of the model
*
* @return String representation of the model
*/
public String toString(){
StringBuffer b = new StringBuffer();
for(int i = 0; i < sizeOfGame; i++){
for(int j = 0; j < sizeOfGame; j++){
b.append(getColor(i, j) + " ");
}
b.append("\n");
}
return b.toString();
}
public boolean canUndo(){
return !undoStack.isEmpty();
}
public boolean canRedo(){
return !redoStack.isEmpty();
}
public void undo(){
try{
redoStack.push((GameModel)this.clone());
restoreGameModel(undoStack.pop());
} catch(CloneNotSupportedException e){
System.out.println("Game model cannot be cloned. No Undo/Redo.");
}
}
public void redo(){
try{
undoStack.push((GameModel)this.clone());
restoreGameModel(redoStack.pop());
} catch(CloneNotSupportedException e){
System.out.println("Game model cannot be cloned. No Undo/Redo.");
}
}
private void restoreGameModel(GameModel g){
numberOfSteps = g.numberOfSteps;
sizeOfGame = g.sizeOfGame;
numberCaptured = g.numberCaptured;
model=g.model;
initialSet = g.initialSet;
currentSelectedColor = g.currentSelectedColor;
torusMove = g.torusMove;
diagMove = g.diagMove;
}
protected Object clone()
throws CloneNotSupportedException {
GameModel cloned = (GameModel)super.clone();
cloned.model = model.clone();
for(int i = 0 ; i < sizeOfGame; i++) {
cloned.model[i]= model[i].clone();
for(int j = 0 ; j < sizeOfGame; j++) {
cloned.model[i][j]= (DotInfo)model[i][j].clone();
}
}
return cloned;
}
}