-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewGameTilesFrame.java
317 lines (284 loc) · 8.9 KB
/
ViewGameTilesFrame.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
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
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
//represent a frame for the actual game of minesweeper
public class ViewGameTilesFrame extends JFrame{
private final int fontSize = 18;
private final int topHeight = 100; //height for the top of the game
//where extralives, time passed, mines left are put
private ViewGUI view;
private JButton[][] buttons; //buttons for the tile grid
private int numrows; //number of rows in the grid
private int numcols; //number of cols in the grid
private int width; //width of the grid
private int cheight; //height of the grid
private JLabel minesLeft; //mines left unflagged and unhit in the grid
private JLabel time; //time passed so far in the game
private Timer timer; //has action listener to increment time every second
//can be stopped and started through this class
private JLabel extralives; //number of extra lives user has left
//given the view, actual grid of tile strings, number of mines in the grid
public ViewGameTilesFrame(ViewGUI myView,String [][] grid, int mines)
{
super("Minesweeper");
view = myView;
if(grid!=null)
{
numrows = grid.length;
numcols = grid[0].length;
}
else
{
numrows = 0;
numcols = 0;
}
width = numcols*60; //arbitrary width of 60 for each tile
cheight = numrows*50; //arbitrary height of 50 for each tile
if(width<500)
width = 500; //minimum to fit all of top panel
setSize(width,cheight+topHeight);
setLayout(new BorderLayout(10,10));
setLocationRelativeTo(null);
//menu bar with "Game", "Help"
JMenuBar menubar = new JMenuBar();
setJMenuBar(createMenu(menubar));
//holds the extra lives (if applicable), mines left, time passed
add(topPanel(mines),BorderLayout.PAGE_START);
if(grid!=null)
add(centerPanel(grid),BorderLayout.CENTER);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
setVisible(true);
timer.start();
}
//create and return the menu bar for the game
private JMenuBar createMenu(JMenuBar menubar)
{
JMenu gameSettings = new JMenu("Game");
gameSettings.setMnemonic(KeyEvent.VK_G);
gameSettings.setFont(new Font("Arial",Font.PLAIN,fontSize));
gameSettings.add(createMenuItem("New Game With Same Settings",KeyEvent.VK_N));
gameSettings.add(createMenuItem("Play Different Game", KeyEvent.VK_D));
gameSettings.add(createMenuItem("Exit",KeyEvent.VK_E));
JMenu help = new JMenu("Help");
help.setMnemonic(KeyEvent.VK_H);
help.setFont(new Font("Arial",Font.PLAIN,fontSize));
help.add(createMenuItem("Display Rules",KeyEvent.VK_R));
if(menubar!=null)
{
menubar.add(gameSettings);
menubar.add(help);
}
return menubar;
}
//create and return a single menu item with the given text and keyevent
private JMenuItem createMenuItem(String text, int keyevent)
{
JMenuItem item = new JMenuItem(text);
item.setActionCommand(text);
item.setFont(new Font("Arial",Font.PLAIN,fontSize));
item.setAccelerator(KeyStroke.getKeyStroke(keyevent,ActionEvent.ALT_MASK));
if(view!=null)
item.addActionListener(new ViewMenuListener(view));
return item;
}
//assuming player has lost, set the background color of the last
//pressed button (which should be a mine)
public void playerLost(int[] lastpressed)
{
if(buttons!=null && lastpressed!=null)
buttons[lastpressed[0]][lastpressed[1]].setBackground(Color.RED);
}
public void updateExtraLives(int lives)
{
if(lives>=0&&extralives!=null) //using extralives
{
String text = extralives.getText();
extralives.setText(text.substring(0, text.length()-1)+lives);
}
}
//make sure all tiles shown to user (true in exposed[][]) are displayed
//correctly
public void refresh(boolean[][] exposed, String emptyTileText)
{
if(exposed!=null && emptyTileText!=null && buttons!=null)
{
for(int i = 0;i<numrows;i++)
{
for(int j= 0;j<numcols;j++)
{
if(exposed[i][j]==true && buttons[i][j].getBackground()!=Color.GRAY &&
buttons[i][j].getText().equals(" "))
{
String buttontext = buttons[i][j].getActionCommand().split(",")[2];
buttons[i][j].setText(buttontext);
if(buttontext.equals(emptyTileText))
buttons[i][j].setBackground(Color.GRAY);
}
}
}
}
repaint();
}
//create and return the top panel with the number of mines left,
//time passed, lives left (if applicable)
private JPanel topPanel(int mines)
{
JPanel top = new JPanel();
top.setSize(width,topHeight);
top.setLayout(new FlowLayout(FlowLayout.CENTER,70,0));
minesLeft = new JLabel("Mines Left: "+mines);
minesLeft.setFont(new Font("Arial",Font.BOLD,fontSize));
top.add(minesLeft);
if(view!=null)
{
int lives = view.getExtraLivesLeft();
if(lives>0)
{
extralives = new JLabel("Lives Left: "+lives);
extralives.setFont(new Font("Arial",Font.BOLD,fontSize));
top.add(extralives);
}
}
time = new JLabel("0");
time.setFont(new Font("Arial",Font.BOLD,fontSize));
timer = new Timer(1000, new TimerActionListener(this));
top.add(time);
return top;
}
//create and return the center panel with the grid tiles as buttons
private JPanel centerPanel(String [][] grid)
{
JPanel center = new JPanel();
if(grid!=null)
{
buttons = new JButton[numrows][numcols];
for(int i =0;i<numrows;i++)
{
for(int j = 0;j<numcols;j++)
{
JButton mybutton = createButton(i,j,grid[i][j]);
buttons[i][j] = mybutton;
center.add(mybutton);
}
}
center.setLayout(new GridLayout(numrows,numcols));
}
return center;
}
//create and return a single button with the given information
private JButton createButton(int row, int col,String buttontext)
{
JButton thisbutton;
thisbutton = new JButton(" ");
thisbutton.setActionCommand(row+","+col+","+buttontext);
if(view!=null)
thisbutton.addMouseListener(new ViewMouseListener(view));
thisbutton.setFont(new Font("Arial",Font.BOLD,fontSize));
return thisbutton;
}
//place a flag at the given button if it is not exposed and not flagged
//and return true, else if the button is already flagged,
//unflag the button and return false, else return false
//(not flagged, already exposed)
//also update minesLeft
public boolean placeFlag(JButton button)
{
if(view!=null && minesLeft!=null)
{
String minesremain = minesLeft.getText();
int mines = Integer.parseInt(minesremain.substring(12));
if(button.getText().equals("F"))
{
mines++;
button.setText(" ");
button.setBackground(null);
minesLeft.setText( minesremain.substring(0,12)+mines);
return false; //already a flag
}
else if(button.getText().equals(view.getEmptyTileString()))
{//cannot flag something already clicked
mines--;
button.setText("F"); //flag
button.setBackground(Color.WHITE);
minesLeft.setText( minesremain.substring(0,12)+ mines);
return true; //change to a flag
}
}
return false; //not flagged
}
//stop the timer and return the current time
public long stopTimer()
{
if(timer!=null&&time!=null)
{
timer.stop();
return Long.parseLong(time.getText());
}
return 0;
}
//start the timer and return the current time
public long startTimer()
{
if(timer!=null&&time!=null)
{
timer.start();
return Long.parseLong(time.getText());
}
return 0;
}
//return the current time
public long getCurrentTime()
{
if(time!=null)
return Long.parseLong(time.getText());
return 0;
}
//increment the time
public void incrementTime()
{
if(view!=null&&time!=null)
{
long curtime = Long.parseLong(time.getText());
if(++curtime <=0)
{
System.out.println("You took way too long!");
view.exitGame();
}
else
{
time.setText(curtime+"");
}
}
}
//tile at row,col was pressed - if it is a mine, make the button
//background red
public void pressed(int row,int col,String mine)
{
if(buttons!=null)
{
JButton button = buttons[row][col];
String text = button.getText().substring(button.getText().length()-mine.length());
if(text.equals(mine))
button.setBackground(Color.RED);
}
}
}