-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMinesweeper.java
More file actions
283 lines (250 loc) · 9.96 KB
/
Minesweeper.java
File metadata and controls
283 lines (250 loc) · 9.96 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
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
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;
import java.util.random.RandomGenerator;
import java.util.stream.IntStream;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/**
* A Java implementation of the classic Minesweeper game of Microsoft Windows.
* Modernized for Java 21+ with records, sealed types, switch expressions,
* pattern matching, and contemporary idioms.
*
* @author Ilkka Kokkarinen
*/
public class Minesweeper extends JPanel {
// --- Coordinate record and neighbour enumeration ---
/** A position on the grid, with utility methods for bounds and neighbours. */
record Pos(int x, int y) {
boolean inBounds(int w, int h) {
return x >= 0 && x < w && y >= 0 && y < h;
}
private static final int[][] OFFSETS = {
{-1, -1}, {-1, 0}, {-1, 1},
{ 0, -1}, { 0, 1},
{ 1, -1}, { 1, 0}, { 1, 1}
};
/** The (up to) eight neighbours of this position within the given bounds. */
java.util.List<Pos> neighbours(int w, int h) {
return java.util.Arrays.stream(OFFSETS)
.map(off -> new Pos(x + off[0], y + off[1]))
.filter(p -> p.inBounds(w, h))
.toList();
}
}
// --- Tile state as a sealed hierarchy ---
/** The visual/logical state of a single tile. */
sealed interface TileState {
record Closed() implements TileState { }
record Marked() implements TileState { }
record Opened() implements TileState { }
}
private static final TileState CLOSED = new TileState.Closed();
private static final TileState MARKED = new TileState.Marked();
private static final TileState OPENED = new TileState.Opened();
// --- Layout constants ---
private static final int TILE_SIZE = 30;
private static final int X_OFFSET = 10;
private static final int Y_OFFSET = 10;
private static final int NUM_OFFSET = TILE_SIZE / 2 - 4;
private static final Font TILE_FONT = new Font("Code2000", Font.BOLD, 12);
private static final BasicStroke TILE_STROKE = new BasicStroke(2.0f);
// --- Instance state ---
private final int gridWidth;
private final int gridHeight;
private final double prob;
private final RandomGenerator rng = RandomGenerator.getDefault();
private boolean gameOn;
private boolean firstClick;
private boolean[][] mines;
private TileState[][] state;
private int[][] value; // precomputed neighbour mine counts
/**
* Construct a new Minesweeper panel of given size.
*
* @param w the width of the game field, in tiles
* @param h the height of the game field, in tiles
* @param prob the probability of a tile containing a mine (0.0–1.0)
*/
public Minesweeper(int w, int h, double prob) {
this.gridWidth = w;
this.gridHeight = h;
this.prob = prob;
setBackground(Color.GRAY);
setPreferredSize(new Dimension(
2 * X_OFFSET + w * TILE_SIZE,
2 * Y_OFFSET + h * TILE_SIZE
));
addMouseListener(new MineListener());
startNewGame();
}
// --- Game lifecycle ---
private void startNewGame() {
gameOn = true;
firstClick = true;
mines = new boolean[gridWidth][gridHeight];
state = new TileState[gridWidth][gridHeight];
value = new int[gridWidth][gridHeight];
for (var row : state) { java.util.Arrays.fill(row, CLOSED); }
repaint();
}
/**
* Plant the mines after the first click, keeping a Manhattan-distance
* safe zone of radius 2 around the clicked position.
*/
private void seedMines(Pos start) {
for (int x = 0; x < gridWidth; x++) {
for (int y = 0; y < gridHeight; y++) {
boolean safeZone = Math.abs(x - start.x()) + Math.abs(y - start.y()) <= 2;
mines[x][y] = !safeZone && rng.nextDouble() < prob;
}
}
for (int x = 0; x < gridWidth; x++) {
for (int y = 0; y < gridHeight; y++) {
var pos = new Pos(x, y);
value[x][y] = (int) pos.neighbours(gridWidth, gridHeight).stream()
.filter(p -> mines[p.x()][p.y()])
.count();
}
}
}
private int countMarkedNeighbours(Pos pos) {
return (int) pos.neighbours(gridWidth, gridHeight).stream()
.filter(p -> state[p.x()][p.y()] instanceof TileState.Marked)
.count();
}
// --- Tile opening logic ---
/**
* Open the tile at the given position. Returns {@code true} if a mine
* was hit. When {@code cascadeOnly} is true, only the neighbours are
* cascade-opened (used for chord clicks on satisfied numbers).
*/
private boolean openTile(Pos pos, boolean cascadeOnly) {
if (!pos.inBounds(gridWidth, gridHeight)) { return false; }
if (!cascadeOnly) {
// Already open or flagged — nothing to do.
if (!(state[pos.x()][pos.y()] instanceof TileState.Closed)) { return false; }
if (firstClick) {
firstClick = false;
seedMines(pos);
}
state[pos.x()][pos.y()] = OPENED;
if (mines[pos.x()][pos.y()]) { return true; }
}
// Cascade: if the count matches the number of marked neighbours,
// recursively open all unmarked neighbours.
if (value[pos.x()][pos.y()] == countMarkedNeighbours(pos)) {
boolean hitMine = false;
for (var neighbour : pos.neighbours(gridWidth, gridHeight)) {
hitMine |= openTile(neighbour, false);
}
return hitMine;
}
return false;
}
private void revealAll() {
gameOn = false;
for (int x = 0; x < gridWidth; x++) {
for (int y = 0; y < gridHeight; y++) {
state[x][y] = OPENED;
}
}
repaint();
}
// --- Mouse handling ---
private class MineListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent me) {
if (!gameOn) { startNewGame(); return; }
var pos = new Pos(
(me.getX() - X_OFFSET) / TILE_SIZE,
(me.getY() - Y_OFFSET) / TILE_SIZE
);
if (!pos.inBounds(gridWidth, gridHeight)) { return; }
boolean hitMine = switch (me.getButton()) {
case MouseEvent.BUTTON1 -> openTile(pos, false);
default -> {
// Right click (or middle): toggle flag.
if (state[pos.x()][pos.y()] instanceof TileState.Closed) {
state[pos.x()][pos.y()] = MARKED;
} else if (state[pos.x()][pos.y()] instanceof TileState.Marked) {
state[pos.x()][pos.y()] = CLOSED;
}
yield false;
}
};
// Chord-open: any open tile whose count matches its marked
// neighbours can have its remaining neighbours auto-opened.
if (!hitMine) {
for (int x = 0; x < gridWidth; x++) {
for (int y = 0; y < gridHeight; y++) {
if (state[x][y] instanceof TileState.Opened) {
var p = new Pos(x, y);
if (value[x][y] == countMarkedNeighbours(p)) {
hitMine |= openTile(p, true);
}
}
}
}
}
if (hitMine) { revealAll(); } else { repaint(); }
}
}
// --- Rendering ---
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
var g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setFont(TILE_FONT);
g2.setStroke(TILE_STROKE);
for (int x = 0; x < gridWidth; x++) {
for (int y = 0; y < gridHeight; y++) {
int bx = X_OFFSET + TILE_SIZE * x;
int by = Y_OFFSET + TILE_SIZE * (y + 1);
switch (state[x][y]) {
case TileState.Opened _ -> {
if (mines[x][y]) {
g2.drawString("M", bx + NUM_OFFSET, by - NUM_OFFSET);
} else if (value[x][y] > 0) {
g2.drawString(String.valueOf(value[x][y]),
bx + NUM_OFFSET, by - NUM_OFFSET);
}
}
case TileState.Closed _, TileState.Marked _ -> {
var rect = new RoundRectangle2D.Double(
bx + 2, by - TILE_SIZE + 2,
TILE_SIZE - 4, TILE_SIZE - 4, 5, 5
);
g2.setPaint(state[x][y] instanceof TileState.Marked
? Color.RED : Color.GREEN);
g2.fill(rect);
g2.setPaint(Color.BLACK);
g2.draw(rect);
}
}
}
}
}
// --- Entry point ---
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
var frame = new JFrame("Minesweeper");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.add(new Minesweeper(35, 20, 0.23));
frame.pack();
frame.setVisible(true);
});
}
}