-
Notifications
You must be signed in to change notification settings - Fork 2
/
Scoreboard.java
283 lines (252 loc) · 8.44 KB
/
Scoreboard.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
package kablewie;
import java.awt.Component;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
/**
* @file -Scoreboard.java
* @author -Rabidra Thapa, Thomas Fisher
* @date -07/12/2015
* @see -Board.java
* @see -Game.java
*
* A Scoreboard panel that displays many details of the current game
*/
public class Scoreboard extends JPanel {
/* Initialisation of variables */
private int m_time;
private Game m_game;
private Board m_board;
private int m_numberOfTilesRevealed;
private int m_spaceFromBorder = 10;
private int m_spaceFromLbl = 5;
private JLabel m_lblName;
private JLabel m_lblTime;
private JLabel m_lblDiffused;
private JLabel m_lblRevealed;
private JLabel m_lblGameState;
private JLabel m_lblHidden;
private JLabel m_lblMines;
private Timer m_timer;
private static final long MS_IN_SECOND = 1000L;
public final int SECS_MINS = 60;
private int m_numberOfTilesNotRevealed;
/**
* Constructor for the Scoreboard class.
* @param g The game object that the spring board is in.
* @see Game.java
*/
public Scoreboard(Game g) {
SpringLayout layout = new SpringLayout();
setLayout(layout);
m_game = g;
m_board = g.getBoard();
m_lblName = new JLabel();
m_lblTime = new JLabel("Time - " + getTime());
m_lblDiffused = new JLabel("Number Diffused - 0");
m_lblRevealed = new JLabel("Tiles Revealed - " + getNumberOfRevealed());
m_lblGameState = new JLabel("Good luck!");
m_lblHidden = new JLabel("Hidden Tiles - " + getNumberOfNotRevealed());
m_lblGameState = new JLabel();
m_lblMines = new JLabel("Number of Mines - "
+ m_board.getNumberOfMines());
addComponent(m_lblName);
addComponent(m_lblTime);
addComponent(m_lblDiffused);
addComponent(m_lblGameState);
addComponent(m_lblRevealed);
addComponent(m_lblHidden);
addComponent(m_lblMines);
/* Layout */
layout.putConstraint(SpringLayout.WEST, m_lblName,
m_spaceFromBorder,
SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, m_lblName,
0,
SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.EAST, m_lblTime,
-10,
SpringLayout.EAST, this);
layout.putConstraint(SpringLayout.NORTH, m_lblTime,
0,
SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, m_lblRevealed,
m_spaceFromBorder,
SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, m_lblRevealed,
m_spaceFromLbl,
SpringLayout.SOUTH, m_lblName);
layout.putConstraint(SpringLayout.EAST, m_lblDiffused,
-10,
SpringLayout.EAST, this);
layout.putConstraint(SpringLayout.NORTH, m_lblDiffused,
m_spaceFromLbl,
SpringLayout.SOUTH, m_lblTime);
layout.putConstraint(SpringLayout.EAST, m_lblHidden,
-10,
SpringLayout.EAST, this);
layout.putConstraint(SpringLayout.NORTH, m_lblHidden,
5,
SpringLayout.SOUTH, m_lblDiffused);
layout.putConstraint(SpringLayout.NORTH, m_lblMines,
5,
SpringLayout.SOUTH, m_lblRevealed);
layout.putConstraint(SpringLayout.WEST, m_lblMines,
m_spaceFromBorder,
SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.WEST, m_lblGameState,
m_spaceFromBorder,
SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, m_lblGameState,
5,
SpringLayout.SOUTH, m_lblHidden);
}
/**
* A method that sets the m_lblGameState label to
* the string given in the parameter
* @param message A string passed through the parameter.
*/
public void setGameState(String message) {
m_lblGameState.setText(message);
}
/**
* A method that sets the m_lblTime label to "Time - "
* concatenated with the value the gameTime() method returns.
* @see getTime()
*/
public void setTime() {
m_lblTime.setText("Time - " + getTime());
}
/**
* A method that sets the m_lblRevealed label to "Tiles Revealed - "
* concatenated with the value the getNumberOfRevealed() method returns.
* @see getNumberOfRevealed()
*/
public void setTilesRevealed() {
m_lblRevealed.setText("Tiles Revealed - " + getNumberOfRevealed());
}
/**
* A method that sets the m_lblHidden label to "Hidden Tiles - "
* concatenated with the value the getNumberOfNotRevealed() method returns.
* @see getNumberOfNotRevealed()
*/
public void setTilesNotRevealed() {
m_lblHidden.setText("Hidden Tiles - " + getNumberOfNotRevealed());
}
/**
* A method that sets the m_lblDiffused label to "Number Diffused - "
* concatenated with the value the m_board.getMinesDiffused() method
* returns.
* @see m_board.getMinesDiffused()
*/
public void setMinesDiffused() {
m_lblDiffused.setText("Number Diffused - "
+ m_board.getNumberDiffused());
}
/**
* A method that sets the m_lblName label to "Name - "
* concatenated with the string given in the parameter
* @param name A string passed through the parameter.
*/
public void setPlayerName(String name) {
m_lblName.setText("Name - " + name);
}
/**
* A method that gets the game time in HH:MM:SS format.
* @return game time in HH:MM:SS format.
*/
public String getTime(){
return m_time /(SECS_MINS * SECS_MINS) + ":" +
(m_time/SECS_MINS) + ":" + (m_time % SECS_MINS);
}
/**
* A method that gets the number of tiles that are revealed.
* @return number of tiles that are revealed.
*/
public int getNumberOfRevealed() {
return m_numberOfTilesRevealed;
}
/**
* A method that gets the number of tiles that are not revealed.
* @return number of tiles that are revealed.
*/
public int getNumberOfNotRevealed() {
int boardArea = m_board.getBoardSize() * m_board.getBoardSize();
m_numberOfTilesNotRevealed = boardArea - m_numberOfTilesRevealed;
return m_numberOfTilesNotRevealed;
}
/**
* A method that adds the given parameter component
* to the panel where the method is called
* @param x the component that is added.
*/
private void addComponent(Component x){
this.add(x);
}
/**
* A method that resets some of the values on the scoreboard
* and calls setGameOver() on the board and calls updateTime()
* @see setGameOver()
* @see updateTime()
*/
public void reset() {
m_board.setGameOver(false);
m_time = 0;
updateTime();
m_numberOfTilesRevealed = 0;
m_numberOfTilesNotRevealed =0;
}
/**
* Updates the values that the labels display
* and checks if the player has won
* @see setTime()
* @see setTilesRevealed()
* @see setMinesDiffused()
* @see setTilesNotRevealed()
*/
public void update() {
setTime();
setTilesRevealed();
setMinesDiffused();
setTilesNotRevealed();
int numberOfTiles = m_board.getBoardSize() * m_board.getBoardSize();
int numberOfNonMineTiles = numberOfTiles - m_board.getNumberOfMines();
if (m_numberOfTilesRevealed == numberOfNonMineTiles) {
m_board.setGameOver(true);
m_game.endGame('w');
}
}
/**
* Increments the value that tracks number of revealed
* tiles and updates the labels
* @see update()
*/
public void incrementTilesRevealed() {
m_numberOfTilesRevealed++;
update();
}
/**
* Creates a timer and sets a task to execute at a fixed period of time
*/
public void updateTime(){
//Creates a timer and sets a task to iterate m_time at every second
m_timer = new Timer();
m_timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
m_time += 1;
m_lblTime.setText("Time - " + getTime());
}
}, MS_IN_SECOND, MS_IN_SECOND);
}
/**
* Stops timer
*/
public void stopTimer() {
m_timer.cancel();
}
}