-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewGUI.java
More file actions
257 lines (229 loc) · 7.05 KB
/
ViewGUI.java
File metadata and controls
257 lines (229 loc) · 7.05 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
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JSpinner;
public class ViewGUI implements ControllerToViewGUI{
/* StartFrame:
* - Select difficulty
* - beginner 10 mines 9x9 grid
* - intermediate 40 mines 16x16 grid
* - advanced 99 mines 16x30 grid
* - custom
* - Extra Lives
*
*
* Game Frame:
* - tabs with Game (new game, exit), Help (rules)
* - grid of tiles
* - number of mines left
* - time passed
* - number of lives left (if applicable)
*
*
* End Frame:
* - message on win/loss
* - time
* - best time for begin/intermediate/expert/custom
* - total games played
* - games won
* - percentage won
* - Button: exit, play again
*/
//to communicate with the controller for the model data
private ViewGUIToController myController;
private ViewStartFrame startframe;
private ViewGameTilesFrame gameframe;
private ViewEndFrame endframe;
public ViewGUI(ViewGUIToController c)
{
myController = c;
}
//start the logic (start frame for settings)
public void go(ArrayList<String> diffs)
{
startframe = new ViewStartFrame(this, diffs);
if(startframe==null)
System.exit(NULL_EXIT_CODE);
}
//difficulty was changed, notify controller and startframe if applicable
public void setDifficulty(String difficulty)
{
if(myController==null || difficulty==null || startframe==null)
System.exit(NULL_EXIT_CODE);
if(difficulty.equals("custom"))
startframe.enableCustomSpinners(true);
else
startframe.enableCustomSpinners(false);
myController.setDifficulty(difficulty);
}
//extra lives changed, notify controller
public void setExtraLives(int extralives)
{
if(myController==null)
System.exit(NULL_EXIT_CODE);
myController.setExtraLives(extralives);
}
//a spinner was changed, get the new info from the startframe
//and notify controller
public void setCustom(JSpinner spinner)
{
if(startframe==null || myController == null)
System.exit(NULL_EXIT_CODE);
String info = startframe.getCustomInfo(spinner);
int myNum;
try{
myNum = Integer.parseInt(info.substring(3));
}catch(NumberFormatException ex)
{
System.out.println("Error!");
myNum = 0;
}
if(info.startsWith("row"))
myController.setCustomRows(myNum);
else if(info.startsWith("col"))
myController.setCustomColumns(myNum);
else //mines
myController.setCustomMines(myNum);
myController.setDifficulty("custom");
}
//exit the entire game by closing all frames
public void exitGame()
{
if(startframe!=null)
startframe.dispose();
if(gameframe!=null)
gameframe.dispose();
if(endframe!=null)
endframe.dispose();
}
//user wants to play another game - begin with a new start frame
public void playAgain()
{
if(startframe!=null)
startframe.dispose();
if(gameframe!=null)
gameframe.dispose();
if(endframe!=null)
endframe.dispose();
if(myController==null)
System.exit(NULL_EXIT_CODE);
myController.reset();
startframe = new ViewStartFrame(this, myController.getDifficulties());
}
//actually play/start the new game by notifying the controller
// and making a new game frame with the given grid
public void playGame()
{
if(gameframe!=null)
gameframe.dispose();
if(myController==null)
System.exit(NULL_EXIT_CODE);
boolean success = myController.startGame();
if(!success)
{
createPopUp("Custom number of mines should be less than rows*columns.", 400,200,false);
}
else
{
if(startframe!=null)
startframe.dispose();
gameframe = new ViewGameTilesFrame(this,myController.getGrid(),myController.getNumMines());
}
}
//get and return the rules from the controller
public String getRules()
{
if(myController!=null)
return myController.getRules();
else
return "";
}
//create a popup instance to display the given msg
//the frame will have this width and height
public void createPopUp(String msg, int width, int height, boolean timerUsage)
{
if(timerUsage && gameframe !=null)
gameframe.stopTimer();
new ViewPopupHelp(this, msg, width, height, timerUsage);
}
//popup has been closed
//if timerUsage, start the timer back up
public void endHelpPopup(boolean timerUsage)
{
if(timerUsage && gameframe!=null)
gameframe.startTimer();
}
//a tile has been pressed
//get the row,col of the tile pressed, notify controller, update
//game frame
public void tilePressed(String rowcol)
{
if(myController==null || gameframe==null)
System.exit(NULL_EXIT_CODE);
int row = Integer.parseInt(rowcol.split(",")[0]);
int col = Integer.parseInt(rowcol.split(",")[1]);
myController.tilePressed(row,col,gameframe.getCurrentTime());
int extralives = myController.getExtraLivesLeft();
if(extralives>=0) //using extralives
{
gameframe.updateExtraLives(extralives);
}
if(myController.playerLost())
{
gameframe.playerLost(myController.getLastPressed());
endframe = new ViewEndFrame(this,false,gameframe.stopTimer(),myController.getBestTime(),myController.getTotalGamesPlayed(),myController.getTotalGamesWon());
}
else if(myController.playerWon())
{
endframe = new ViewEndFrame(this,true,gameframe.stopTimer(),myController.getBestTime(),myController.getTotalGamesPlayed(),myController.getTotalGamesWon());
}
else
{
//check if last pressed is a mine
//row,col might be last pressed, but if the user is trying to
//autocomplete, may have flagged a wrong tile and autocompleted
//a mine (not row,col but actually in last pressed now)
int [] last = myController.getLastPressed();
gameframe.pressed(last[0],last[1],myController.getMineString());
}
}
//user wants to place a flag on this button
//notify controller and game frame of flag place
public void placeFlag(JButton button)
{
if(myController==null || gameframe==null)
System.exit(NULL_EXIT_CODE);
int row = Integer.parseInt(button.getActionCommand().split(",")[0]);
int col = Integer.parseInt(button.getActionCommand().split(",")[1]);
boolean flagged = gameframe.placeFlag(button);
myController.placeFlag(flagged,row,col);
}
//return the empty tile string from the controller
public String getEmptyTileString()
{
if(myController==null)
System.exit(NULL_EXIT_CODE);
return myController.getEmptyTileString();
}
//make the game frame refresh the tiles to correspond to exposed
public void refresh(boolean [][] exposed, String emptyTileText)
{
if(gameframe==null)
System.exit(NULL_EXIT_CODE);
gameframe.refresh(exposed, emptyTileText);
}
//extra lives option should be enabled for the user, notifies
//start frame
public void showExtraLives()
{
if(startframe==null)
System.exit(NULL_EXIT_CODE);
startframe.extraLives();
}
//return the extra lives left from the controller
public int getExtraLivesLeft()
{
if(myController==null)
System.exit(NULL_EXIT_CODE);
return myController.getExtraLivesLeft();
}
}