-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameController.java
236 lines (208 loc) · 9 KB
/
GameController.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
// Author: Samuel Saint-Fleur, Aden Li
// Course: ITI 1121, University of Ottawa
// Assignment: 2
// Question: GameController
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import javax.swing.*;
/**
* The class <b>GameController</b> is the controller of the game. It is a listener
* of the view, and has a method <b>play</b> which computes the next
* step of the game, and updates model and view.
*
* @author Guy-Vincent Jourdan, University of Ottawa
* @author Samuel Saint-Fleur, Aden Li
*/
public class GameController implements ActionListener {
// ADD YOUR INSTANCE VARIABLES HERE
private GameModel gameModel;
private GameView gameView;
/**
* Constructor used for initializing the controller. It creates the game's view
* and the game's model instances
*
* @param width
* the width of the board on which the game will be played
* @param height
* the height of the board on which the game will be played
* @param numberOfMines
* the number of mines hidden in the board
*/
public GameController(int width, int height, int numberOfMines) {
// ADD YOU CODE HERE
gameModel = new GameModel(width,height,numberOfMines);
gameView = new GameView(gameModel,this);
}
/**
* Callback used when the user clicks a button (reset or quit)
*
* @param e
* the ActionEvent
*/
public void actionPerformed(ActionEvent e) {
// ADD YOU CODE HERE
if(e.getActionCommand().equals("Quit"))
{
System.exit(0);
}else if(e.getActionCommand().equals("Reset"))
{
reset();
} else //if (e.getActionCommand().equals("DotB"))
{
int col, rw = 0;
col = ((DotButton) e.getSource()).getColumn();
rw = ((DotButton) e.getSource()).getRow();
this.play(col,rw);
gameView.update();
}
}
/**
* resets the game
*/
private void reset(){
// ADD YOU CODE HERE
gameModel.reset();
gameView.update();
}
/**
* <b>play</b> is the method called when the user clicks on a square.
* If that square is not already clicked, then it applies the logic
* of the game to uncover that square, and possibly end the game if
* that square was mined, or possibly uncover some other squares.
* 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 width
* the selected column
* @param heigth
* the selected line
*/
private void play(int width, int heigth){
// ADD YOU CODE HERE
if(!gameModel.hasBeenClicked(width,heigth))
{
gameModel.uncover(width,heigth);
gameModel.click(width,heigth);
gameModel.step();
if(gameModel.isMined(width,heigth))
{
gameModel.uncoverAll();
gameView.update();
int decision = 0;
decision = JOptionPane.showOptionDialog(null,"Would you like to play again?","BOOM! BIG SHAQ! YOU CLICKED A MINE!", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
switch(decision){
case JOptionPane.YES_OPTION: reset(); break;
case JOptionPane.NO_OPTION: System.exit(0); break;
}
}else if (gameModel.isFinished()) {
gameModel.uncoverAll();
gameView.update();
int decision = 0;
decision = JOptionPane.showOptionDialog(null,"Would you like to play again?","You beat the game in "+gameModel.getNumberOfSteps()+" steps!", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
switch(decision){
case JOptionPane.YES_OPTION: reset(); break;
case JOptionPane.NO_OPTION: System.exit(0); break;
}
}
else if(gameModel.isBlank(width,heigth))
{
clearZone(gameModel.get(width,heigth));
}
}
gameView.update();
}
/**
* <b>clearZone</b> is the method that computes which new dots should be ``uncovered''
* when a new square with no mine in its neighborood has been selected
* @param initialDot
* the DotInfo object corresponding to the selected DotButton that
* had zero neighbouring mines
*/
private void clearZone(DotInfo initialDot) {
// ADD YOU CODE HERE
//Up to 8 neighbours
int size = gameModel.getWidth() * gameModel.getHeigth();
GenericArrayStack<DotInfo> stack = new GenericArrayStack<DotInfo>(size);
stack.push(initialDot);
while(!stack.isEmpty()) {
DotInfo ndot = (DotInfo) stack.pop();
int curx = ndot.getX();
int cury = ndot.getY();
if (ndot.getNeighbooringMines()==0) {
if (gameModel.isCovered(curx+1,cury)==true&&!gameModel.isMined(curx+1,cury)){
gameModel.uncover(curx+1,cury);
gameModel.click(curx+1,cury);
if(gameModel.isBlank(curx+1,cury)==true){
//System.out.println(gameModel.isBlank(curx+1,cury));
//System.out.println("Point : ("+(curx+1)+","+(cury)+")");
stack.push(gameModel.get(curx+1,cury));
}
}
if (gameModel.isCovered(curx+1,cury+1)==true&&!gameModel.isMined(curx+1,cury+1)){
gameModel.uncover(curx+1,cury+1);
gameModel.click(curx+1,cury+1);
if(gameModel.isBlank(curx+1,cury+1)==true){
//System.out.println(gameModel.isBlank(curx+1,cury+1));
//System.out.println("Point : ("+(curx+1)+","+(cury+1)+")");
stack.push(gameModel.get(curx+1,cury+1));
}
}
if (gameModel.isCovered(curx+1,cury-1)==true&&!gameModel.isMined(curx+1,cury-1)){
gameModel.uncover(curx+1,cury-1);
gameModel.click(curx+1,cury-1);
if(gameModel.isBlank(curx+1,cury-1)==true){
//System.out.println(gameModel.isBlank(curx+1,cury-1));
//System.out.println("Point : ("+(curx+1)+","+(cury-1)+")");
stack.push(gameModel.get(curx+1,cury-1));
}
}
if (gameModel.isCovered(curx,cury+1)==true&&!gameModel.isMined(curx,cury+1)){
gameModel.uncover(curx,cury+1);
gameModel.click(curx,cury+1);
if(gameModel.isBlank(curx,cury+1)==true){
//System.out.println(gameModel.isBlank(curx,cury+1));
//System.out.println("Point : ("+(curx)+","+(cury+1)+")");
stack.push(gameModel.get(curx,cury+1));
}
}
if (gameModel.isCovered(curx,cury-1)==true&&!gameModel.isMined(curx,cury-1)){
gameModel.uncover(curx,cury-1);
gameModel.click(curx,cury-1);
if(gameModel.isBlank(curx,cury-1)==true){
//System.out.println(gameModel.isBlank(curx,cury-1));
//System.out.println("Point : ("+(curx)+","+(cury-1)+")");
stack.push(gameModel.get(curx,cury-1));
}
}
if (gameModel.isCovered(curx-1,cury)==true&&!gameModel.isMined(curx-1,cury)){
gameModel.uncover(curx-1,cury);
gameModel.click(curx-1,cury);
if(gameModel.isBlank(curx-1,cury)==true){
//System.out.println(gameModel.isBlank(curx-1,cury));
//System.out.println("Point : ("+(curx-1)+","+(cury)+")");
stack.push(gameModel.get(curx-1,cury));
}
}
if (gameModel.isCovered(curx-1,cury+1)==true&&!gameModel.isMined(curx-1,cury+1)){
gameModel.uncover(curx-1,cury+1);
gameModel.click(curx-1,cury+1);
if(gameModel.isBlank(curx-1,cury+1)==true){
//System.out.println(gameModel.isBlank(curx-1,cury+1));
//System.out.println("Point : ("+(curx-1)+","+(cury+1)+")");
stack.push(gameModel.get(curx-1,cury+1));
}
}
if (gameModel.isCovered(curx-1,cury-1)==true&&!gameModel.isMined(curx-1,cury-1)){
gameModel.uncover(curx-1,cury-1);
gameModel.click(curx-1,cury-1);
if(gameModel.isBlank(curx-1,cury-1)==true){
//System.out.println(gameModel.isBlank(curx-1,cury-1));
//System.out.println("Point : ("+(curx-1)+","+(cury-1)+")");
stack.push(gameModel.get(curx-1,cury-1));
}
}
}
}
}
}