-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
258 lines (235 loc) · 7.28 KB
/
GUI.java
File metadata and controls
258 lines (235 loc) · 7.28 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
258
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
/**
* Minesweeper game implementation with a more modern, modular design.
*/
public class Minesweeper extends JFrame {
private final int rows;
private final int cols;
private final int totalBombs;
private int safeCellsRemaining;
private final CellButton[][] buttons;
private final boolean[][] bombLocations;
private final int[][] neighborBombCount;
/**
* Constructor to initialize the game with default size and bombs.
*/
public Minesweeper() {
this(10, 10, 25);
}
/**
* Constructor to initialize the game with specified size and bombs.
*/
public Minesweeper(int rows, int cols, int bombs) {
super("Minesweeper");
this.rows = rows;
this.cols = cols;
this.totalBombs = bombs;
this.safeCellsRemaining = rows * cols - bombs;
// Initialize grid data
bombLocations = new boolean[rows][cols];
neighborBombCount = new int[rows][cols];
// Initialize button grid
buttons = new CellButton[rows][cols];
// Setup game UI
setupFrame();
generateBombs();
calculateNeighborCounts();
createButtons();
setVisible(true);
}
/**
* Sets up the main window properties.
*/
private void setupFrame() {
setSize(800, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(rows, cols));
setResizable(false);
}
/**
* Randomly assign bombs to the grid.
*/
private void generateBombs() {
Random rand = new Random();
int bombsPlaced = 0;
while (bombsPlaced < totalBombs) {
int r = rand.nextInt(rows);
int c = rand.nextInt(cols);
if (!bombLocations[r][c]) {
bombLocations[r][c] = true;
bombsPlaced++;
}
}
}
/**
* Calculates number of neighboring bombs for each cell.
*/
private void calculateNeighborCounts() {
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
neighborBombCount[r][c] = countBombsAround(r, c);
}
}
}
/**
* Counts bombs around a specific cell.
*/
private int countBombsAround(int row, int col) {
int count = 0;
for (int dr = -1; dr <= 1; dr++) {
for (int dc = -1; dc <= 1; dc++) {
int newRow = row + dr;
int newCol = col + dc;
if (isWithinBounds(newRow, newCol) && bombLocations[newRow][newCol]) {
if (!(dr == 0 && dc == 0)) {
count++;
}
}
}
}
return count;
}
/**
* Creates the grid buttons and adds listeners.
*/
private void createButtons() {
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
CellButton btn = new CellButton(r, c);
btn.addActionListener(new CellClickListener());
buttons[r][c] = btn;
add(btn);
}
}
}
/**
* Helper to check if position is inside grid bounds.
*/
private boolean isWithinBounds(int r, int c) {
return r >= 0 && r < rows && c >= 0 && c < cols;
}
/**
* Handles cell button clicks.
*/
private class CellClickListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
CellButton btn = (CellButton) e.getSource();
int r = btn.row;
int c = btn.col;
if (bombLocations[r][c]) {
revealAllBombs();
int option = JOptionPane.showConfirmDialog(
Minesweeper.this,
"Boom! You hit a mine. Play again?",
"Game Over",
JOptionPane.YES_NO_OPTION
);
if (option == JOptionPane.YES_OPTION) {
resetGame();
} else {
System.exit(0);
}
} else {
revealCell(r, c);
if (--safeCellsRemaining == 0) {
JOptionPane.showMessageDialog(
Minesweeper.this,
"Congratulations! You won! Play again?",
"Victory",
JOptionPane.INFORMATION_MESSAGE
);
int option = JOptionPane.showConfirmDialog(
Minesweeper.this,
"Play again?",
"Victory",
JOptionPane.YES_NO_OPTION
);
if (option == JOptionPane.YES_OPTION) {
resetGame();
} else {
System.exit(0);
}
}
}
}
}
/**
* Reveals all bombs when game is lost.
*/
private void revealAllBombs() {
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (bombLocations[r][c]) {
buttons[r][c].setText("💣");
}
buttons[r][c].setEnabled(false);
}
}
}
/**
* Reveals a cell and triggers recursive reveal if no neighboring bombs.
*/
private void revealCell(int r, int c) {
if (!isWithinBounds(r, c) || !buttons[r][c].isEnabled()) {
return;
}
buttons[r][c].setEnabled(false);
int count = neighborBombCount[r][c];
if (count > 0) {
buttons[r][c].setText(String.valueOf(count));
} else {
// If no neighboring bombs, reveal neighbors recursively
for (int dr = -1; dr <= 1; dr++) {
for (int dc = -1; dc <= 1; dc++) {
int nr = r + dr;
int nc = c + dc;
if (isWithinBounds(nr, nc) && (dr != 0 || dc != 0)) {
revealCell(nr, nc);
}
}
}
}
}
/**
* Resets the game to initial state.
*/
private void resetGame() {
// Reset data
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
bombLocations[r][c] = false;
neighborBombCount[r][c] = 0;
buttons[r][c].reset();
}
}
safeCellsRemaining = rows * cols - totalBombs;
generateBombs();
calculateNeighborCounts();
}
/**
* Custom JButton class to store position info.
*/
private class CellButton extends JButton {
final int row;
final int col;
public CellButton(int row, int col) {
this.row = row;
this.col = col;
setFont(new Font("Arial", Font.BOLD, 14));
}
public void reset() {
setText("");
setEnabled(true);
}
}
/**
* Main method to start the game.
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(Minesweeper::new);
}
}