-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMineSweeperPanel.java
267 lines (214 loc) · 9.02 KB
/
MineSweeperPanel.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
/*****************************************************************
Handles updates of gameplay and layout of MineSweeper.
@author Xue Hua
@version Winter 2019
*****************************************************************/
package MineSweeper;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
public class MineSweeperPanel extends JPanel {
/* Array of JButtons that represent the board */
private JButton[][] board;
/* Quit Button */
private JButton butQuit;
/* Current cell State */
private Cell iCell;
/* Current number of wins */
private JLabel winLbl;
/* Current number of losses */
private JLabel lossLbl;
/* Current number of played games */
private JLabel playedLbl;
/* Model for gameplay */
private MineSweeperGame game;
/* Size of board */
private int size;
/*****************************************************************
Constructor creates a board of specified size with specified
mines.
@param inputSize size of board
@param inputMine number of mines
*****************************************************************/
public MineSweeperPanel(int inputSize, int inputMine) {
size = inputSize;
// Instantiate JPanels
JPanel bottom = new JPanel();
JPanel center = new JPanel();
// Create game listeners
ButtonListener listener = new ButtonListener();
// Instantiate game
game = new MineSweeperGame(inputSize, inputMine);
// Instantiate labels, buttons and places them
lossLbl = new JLabel("Losses : " + game.loss);
winLbl = new JLabel("Wins : " + game.won);
playedLbl = new JLabel("Games : " + game.games);
butQuit = new JButton("Quit");
butQuit.addActionListener(listener);
bottom.add(butQuit);
bottom.add(playedLbl);
bottom.add(winLbl);
bottom.add(lossLbl);
// Create the board
center.setLayout(new GridLayout(size, size));
board = new JButton[size][size];
// Span the minefield
for (int row = 0; row < size; row++)
for (int col = 0; col < size; col++) {
// Span each minefield with a blank JButton or Icon
board[row][col] = new JButton("", new ImageIcon(
"src\\images\\facingdown.png"));
board[row][col].addActionListener(listener);
final int finalRow = row;
final int finalCol = col;
// Add MouseListener to each button for right click/flagging
board[row][col].addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
flagMine(finalRow, finalCol);
}
}
});
// JButton customization
board[row][col].setContentAreaFilled(false);
board[row][col].setPreferredSize(new Dimension(20, 20));
board[row][col].setFont(new Font("Arial", Font.BOLD, 5));
// Add each button to JPanel
center.add(board[row][col]);
}
displayBoard();
bottom.setLayout(new GridLayout(3, 2));
// Add all to contentPane
add(new JLabel("MineSweeper"), BorderLayout.CENTER);
add(center, BorderLayout.CENTER);
add(bottom, BorderLayout.SOUTH);
}
/*****************************************************************
Helper method that updates the minefield
@param none
@return none
*****************************************************************/
private void displayBoard() {
// Updates win, loss and games played JLabels
updateGameLbls();
//
for (int r = 0; r < size; r++)
for (int c = 0; c < size; c++){
// Fetches the Cell state of the current cell
iCell = game.getCell(r, c);
if (!iCell.isFlagged() && !iCell.isMine()) {
// Set faced down if not flagged
board[r][c].setIcon(iconSet(9));
} else
board[r][c].setEnabled(true);
if (iCell.isMine() && !iCell.isFlagged()) {
// Set mine if not flagged
//board[r][c].setText("!");
board[r][c].setIcon(iconSet(10));
}
if (iCell.isExposed()) {
// Set disabled is Game exposes cell
board[r][c].setEnabled(false);
// Expose how close neighbouring mines are or none
if (game.getMineCount(r, c) > 0) {
//board[r][c].setText("" + game.getMineCount(r, c));
board[r][c].setDisabledIcon(iconSet(game.getMineCount(r, c)));
} else
board[r][c].setDisabledIcon(iconSet(0));
} else
// Default state is keeping Cell as enabled
board[r][c].setEnabled(true);
}
}
/*****************************************************************
Helper method that updates the game labels
@param none
@return none
*****************************************************************/
private void updateGameLbls(){
playedLbl.setText("Played: " + game.games);
winLbl.setText("Won: " + game.won);
lossLbl.setText("Lost: " + game.loss);
}
/*****************************************************************
Class that implements actionListener for gameplay
@param none
@return none
*****************************************************************/
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Quit button
if (butQuit == e.getSource())
System.exit(0);
// Trigger Game when Cell is selected
for (int r = 0; r < size; r++)
for (int c = 0; c < size; c++)
if (board[r][c] == e.getSource()) {
game.select(r, c);
}
// Updates with each trigger
displayBoard();
// Player loses
if (game.getGameStatus() == GameStatus.Lost) {
displayBoard();
JOptionPane.showMessageDialog(null,
"You Lose \n The game will reset");
//exposeMines = false;
game.reset();
displayBoard();
}
// Player wins
if (game.getGameStatus() == GameStatus.Won) {
JOptionPane.showMessageDialog(null,
"You Win: all mines have been found!\n The game will reset");
game.reset();
displayBoard();
}
}
}
/*****************************************************************
Helper method to flag or un-flag mines upon right click on cell
@param row number of rows
col number of columns
@return none
*****************************************************************/
private void flagMine(int row, int col) {
// Check if cell is valid to flag
if (board[row][col].isEnabled())
// Check if cell was previously flagged
if (game.getCell(row, col).isFlagged()) {
// Un-flag if previously flagged
game.getCell(row, col).setFlagged(false);
// Sets it back to a appropriate icon
if (game.getCell(row, col).isMine())
board[row][col].setIcon(iconSet(10));
else
board[row][col].setIcon(iconSet(9));
}else {
// Flag if not already flagged
game.getCell(row, col).setFlagged(true);
game.flag(row, col);
// Set to flag icon. 11 is 'flag'
board[row][col].setIcon(iconSet(11));
}
}
/*****************************************************************
Helper method to set icons throughout the game.
@param i index of icons array to represent corresponding icon
@return icon ImageIcon type to set icon
*****************************************************************/
private ImageIcon iconSet(int i) {
// Array of icons
String[] icons = {"0.png", "1.png", "2.png", "3.png", "4.png", "5.png", "6.png",
"7.png", "8.png", "facingdown.png", "bomb.png", "flagged.png"};
ImageIcon icon = new ImageIcon("src\\MineSweeper\\" + icons[i]);
Image img = icon.getImage();
Image newImg = img.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
icon = new ImageIcon(newImg);
return icon;
}
}