-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameController.java
More file actions
299 lines (263 loc) · 10.2 KB
/
GameController.java
File metadata and controls
299 lines (263 loc) · 10.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
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.io.*;
/**
* The class <b>GameController</b> is the controller of the game. It has a method
* <b>selectColor</b> which is called by the view when the player selects the next
* color. It then computesthe next step of the game, and updates model and view.
*
* @author Guy-Vincent Jourdan, University of Ottawa
* @Modified by Brahim Kanouche, University of Ottawa
*/
public class GameController implements ActionListener {
/**
* Reference to the view of the board
*/
private GameView gameView;
/**
* Reference to the model of the game
*/
private GameModel gameModel;
/**
* File used to save the state of the game
*/
static private final String SAVED_FILE = "./savedGame.ser";
/**
* Constructor used for initializing the controller. It creates the game's view
* and the game's model instances
*
* @param size
* the size of the board on which the game will be played. If there
* is a saved game of different size, then a new game of size ``size''
* is used instead.
*/
public GameController(int size) {
if(!loadModel() || gameModel.getSize() != size)
gameModel = new GameModel(size);
gameView = new GameView(gameModel, this);
flood();
gameView.update();
}
/**
* resets the game
*/
public void reset(){
gameModel.reset();
flood();
gameView.update();
}
/**
* Callback used when the user clicks a button (reset or quit)
*
* @param e
* the ActionEvent
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof DotButton) {
if(!gameModel.isInitialSet()) {
DotButton clicked = (DotButton)(e.getSource());
gameModel.startUpdate();
gameModel.setInitialCaptured(clicked.getRow(), clicked.getColumn());
gameView.update();
} else {
selectColor(((DotButton)(e.getSource())).getColor());
}
} else if (e.getSource() instanceof JButton) {
JButton clicked = (JButton)(e.getSource());
if (clicked.getText().equals("Quit")) {
saveModel();
System.exit(0);
} else if (clicked.getText().equals("Reset")){
reset();
} else if (clicked.getText().equals("Undo")){
gameModel.undo();
gameView.update();
} else if (clicked.getText().equals("Redo")){
gameModel.redo();
gameView.update();
}
} else if (e.getSource() instanceof JRadioButton) {
JRadioButton clicked = (JRadioButton)(e.getSource());
if(clicked.getText().equals("Plane")) {
gameModel.setTorusMove(false);
} else if(clicked.getText().equals("Torus")) {
gameModel.setTorusMove(true);
} else if(clicked.getText().equals("Orthogonal")) {
gameModel.setDiagMove(false);
} else if(clicked.getText().equals("Diagonals")) {
gameModel.setDiagMove(true);
}
gameView.update();
}
}
/**
* <b>selectColor</b> is the method called when the user selects a new color.
* If that color is not the currently selected one, then it applies the logic
* of the game to capture possible locations. It then checks if the game
* is finished, and if so, congratulates the player, showing the number of
* moves, and gives to options: start a new game, or exit
* @param color
* the newly selected color
*/
public void selectColor(int color){
if(color != gameModel.getCurrentSelectedColor()) {
gameModel.startUpdate();
if(gameModel.getNumberOfSteps() == 0){
flood();
}
gameModel.setCurrentSelectedColor(color);
flood();
gameModel.step();
gameView.update();
if(gameModel.isFinished()) {
Object[] options = {"Play Again",
"Quit"};
int n = JOptionPane.showOptionDialog(gameView,
"Congratulations, you won in " + gameModel.getNumberOfSteps()
+" steps!\n Would you like to play again?",
"Won",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
if(n == 0){
reset();
} else{
System.exit(0);
}
}
}
}
/**
* <b>flood</b> is the method that computes which new dots should be ``captured''
* when a new color has been selected. The Model is updated accordingly
*/
private void flood() {
Stack<DotInfo> stack = new LinkedStack<DotInfo>();
Stack<DotInfo> neighborsStack;
for(int i =0; i < gameModel.getSize(); i++) {
for(int j =0; j < gameModel.getSize(); j++) {
if(gameModel.isCaptured(i,j)) {
stack.push(gameModel.get(i,j));
}
}
}
while(!stack.isEmpty()){
DotInfo dotInfo = stack.pop();
neighborsStack = listNeighbors(dotInfo.getX(),dotInfo.getY());
DotInfo neighbor;
while(!neighborsStack.isEmpty()) {
neighbor = neighborsStack.pop();
if(shouldBeCaptured (neighbor.getX(), neighbor.getY())) {
gameModel.capture(neighbor.getX(), neighbor.getY());
stack.push(neighbor);
}
}
}
}
/**
* <b>shouldBeCaptured</b> is a helper method that decides if the dot
* located at position (i,j), which is next to a captured dot, should
* itself be captured
* @param i
* row of the dot
* @param j
* column of the dot
* @return true if the dot located at position (i,j) should be captured,
* false otherwise
*/
private boolean shouldBeCaptured(int i, int j) {
if(!gameModel.isCaptured(i, j) &&
(gameModel.getColor(i,j) == gameModel.getCurrentSelectedColor())) {
return true;
} else {
return false;
}
}
/**
* <b>listNeighbors</b> is a helper method that lists all the
* neighboorings dots of a dot located at position (i,j), given
* the current settings of the game (diag moves, torus)
* itself be captured
* @param i
* row of the dot
* @param j
* column of the dot
* @return a stack of DotInfo with the neighboorings dots
*/
private Stack<DotInfo> listNeighbors(int i, int j){
Stack<DotInfo> stack = new LinkedStack<DotInfo>();
if(gameModel.isTorusMove() || i > 0) {
stack.push(gameModel.get((gameModel.getSize()+i-1)%gameModel.getSize(),j));
if(gameModel.isDiagMove() && (gameModel.isTorusMove() || j > 0)) {
stack.push(gameModel.get((gameModel.getSize()+i-1)%gameModel.getSize(),
(gameModel.getSize()+j-1)%gameModel.getSize()));
}
if(gameModel.isDiagMove() && (gameModel.isTorusMove() || j < gameModel.getSize()-1)) {
stack.push(gameModel.get((gameModel.getSize()+i-1)%gameModel.getSize(),
(j+1)%gameModel.getSize()));
}
}
if(gameModel.isTorusMove() || i < gameModel.getSize()-1) {
stack.push(gameModel.get((i+1)%gameModel.getSize(),j));
if(gameModel.isDiagMove() && (gameModel.isTorusMove() || j > 0)) {
stack.push(gameModel.get((i+1)%gameModel.getSize(),
(gameModel.getSize()+j-1)%gameModel.getSize()));
}
if(gameModel.isDiagMove() && (gameModel.isTorusMove() || j < gameModel.getSize()-1)) {
stack.push(gameModel.get((i+1)%gameModel.getSize(),
(j+1)%gameModel.getSize()));
}
}
if(gameModel.isTorusMove() || j > 0) {
stack.push(gameModel.get(i,(gameModel.getSize()+j-1)%gameModel.getSize()));
}
if(gameModel.isTorusMove() || j < gameModel.getSize()-1) {
stack.push(gameModel.get(i,(j+1)%gameModel.getSize()));
}
return stack;
}
/**
* Saves the current state of the game (the model) using serialization
* in the file SAVED_FILE
*/
private void saveModel(){
try {
FileOutputStream fos = new FileOutputStream(SAVED_FILE);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(gameModel);
out.flush();
out.close();
}
catch (IOException e) {
System.out.println(e);
}
}
/**
* Attempts to load back the saved model from
* the file SAVED_FILE. The file is deleted afterwards.
*
* @return true if the model was succesfully reloaded, false
* otherise
*/
private boolean loadModel(){
File f = new File(SAVED_FILE);
if(!f.isFile())
return false;
try {
FileInputStream fis = new FileInputStream(f);
ObjectInputStream in = new ObjectInputStream(fis);
gameModel = (GameModel)in.readObject();
in.close();
f.delete();
return true;
} catch (IOException e) {
System.out.println("Problem reading the saved model file: " + e);
} catch (ClassNotFoundException e) {
System.out.println("Problem reading the saved model: " + e);
}
return false;
}
}