-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameModel.java
executable file
·258 lines (218 loc) · 6.18 KB
/
GameModel.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
import java.util.Random ;
import java.lang.Exception ;
import java.io.Serializable ;
/**
* 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
*/
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;
/**
* 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() ;
sizeOfGame = size ;
reset() ;
}
/**
* 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)) ;
}
}
setCurrentSelectedColor(-1) ;
numberOfSteps = -1 ;
numberCaptured = 0 ;
}
/**
* 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();
}
/**
* 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 ;
}
/**
* Getter method for the current number of captured dots
*
* @return the current number of captured dots
*/
public int getNumberCaptured() {
return numberCaptured ;
}
// public void numberCapturedPlusOne() {
// numberCaptured++ ;
// }
/**
* 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];
}
/**
* 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() ;
}
/**
* The method <b>clone</b> creates a deep copy of the the current model
*
* @return deep copy of the current model
*/
protected GameModel clone() throws CloneNotSupportedException {
return (GameModel) super.clone() ;
}
}