-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinesweeper.java
327 lines (290 loc) · 8.72 KB
/
Minesweeper.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
318
319
320
321
322
323
324
325
326
327
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class Minesweeper extends JFrame implements MouseListener {
private int tile_count;
private int pixels;
private int size;
private int[][] answer_grid; // 0-10
private int[][] input_grid; // -1, 0, 1, 2
private LinkedList<Image> sprites;
private boolean first = true;
private int mines;
private boolean finished = false;
// private int time = 0;
// private Thread timer;
public Minesweeper(int tile_count, int pixels, int mines) {
this.tile_count = tile_count;
this.pixels = pixels;
this.mines = mines;
int size = tile_count * pixels;
answer_grid = new int[tile_count][tile_count];
input_grid = new int[tile_count][tile_count];
sprites = new LinkedList<Image>();
getSprites();
// timer = new Thread(new Runnable() {
// public void run() {
// try {
// while (true) {
// if (first || finished)
// time = 0;
// time++;System.out.println(time);
// Thread.sleep(1000);
// }
// } catch (InterruptedException e) {
// System.out.println(e.getMessage());
// }
// }
// });
// timer.start();
setSize(size + 12, size + 35);
setTitle("Minesweeper");
setIconImage(new ImageIcon("sprites/icon.png").getImage());
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
addMouseListener(this);
}
public void getSprites() {
sprites.add(0, new ImageIcon("sprites/empty_tile.png").getImage());
sprites.add(1, new ImageIcon("sprites/one.png").getImage());
sprites.add(2, new ImageIcon("sprites/two.png").getImage());
sprites.add(3, new ImageIcon("sprites/three.png").getImage());
sprites.add(4, new ImageIcon("sprites/four.png").getImage());
sprites.add(5, new ImageIcon("sprites/five.png").getImage());
sprites.add(6, new ImageIcon("sprites/six.png").getImage());
sprites.add(7, new ImageIcon("sprites/seven.png").getImage());
sprites.add(8, new ImageIcon("sprites/eight.png").getImage());
sprites.add(9, new ImageIcon("sprites/wrong_mine.png").getImage());
sprites.add(10, new ImageIcon("sprites/tile.png").getImage());
sprites.add(11, new ImageIcon("sprites/flag.png").getImage());
sprites.add(12, new ImageIcon("sprites/mine.png").getImage());
sprites.add(13, new ImageIcon("sprites/red_mine.png").getImage());
}
public void generate(int x, int y) {
int mines = this.mines;
while (mines > 0) {
int i = (int)(Math.random() * tile_count);
int j = (int)(Math.random() * tile_count);
if (!((i >= x - 2 && i <= x + 2) && (j >= y - 2 && j <= y + 2)) && answer_grid[i][j] == 0) {
answer_grid[i][j] = 12;
mines--;
}
}
for (int i = 0; i < answer_grid.length; i++) {
for (int j = 0; j < answer_grid[0].length; j++) {
incrementTiles(i, j);
}
}
}
public void incrementTiles(int x, int y) {
if (answer_grid[x][y] != 12) return;
if (y > 0 && answer_grid[x][y - 1] != 12) {
answer_grid[x][y - 1]++;
}
if (y < tile_count - 1 && answer_grid[x][y + 1] != 12) {
answer_grid[x][y + 1]++;
}
if (x > 0 && answer_grid[x - 1][y] != 12) {
answer_grid[x - 1][y]++;
}
if (x < tile_count - 1 && answer_grid[x + 1][y] != 12) {
answer_grid[x + 1][y]++;
}
if (x > 0 && y > 0 && answer_grid[x - 1][y - 1] != 12) {
answer_grid[x - 1][y - 1]++;
}
if (x < tile_count - 1 && y > 0 && answer_grid[x + 1][y - 1] != 12) {
answer_grid[x + 1][y - 1]++;
}
if (x < tile_count - 1 && y < tile_count - 1 && answer_grid[x + 1][y + 1] != 12) {
answer_grid[x + 1][y + 1]++;
}
if (y < tile_count - 1 && x > 0 && answer_grid[x - 1][y + 1] != 12) {
answer_grid[x - 1][y + 1]++;
}
}
public void onClick(int x, int y) {
if (answer_grid[x][y] == 12) {
finished = true;
input_grid[x][y] = -1;
return;
}
if (input_grid[x][y] == 1) return;
input_grid[x][y] = 1;
if (answer_grid[x][y] != 0) return;
if (y > 0 && answer_grid[x][y - 1] != 12) {
onClick(x, y - 1);
}
if (y < tile_count - 1 && answer_grid[x][y + 1] != 12) {
onClick(x, y + 1);
}
if (x > 0 && answer_grid[x - 1][y] != 12) {
onClick(x - 1, y);
}
if (x < tile_count - 1 && answer_grid[x + 1][y] != 12) {
onClick(x + 1, y);
}
if (x > 0 && y > 0 && answer_grid[x - 1][y - 1] != 12) {
onClick(x - 1, y - 1);
}
if (x < tile_count - 1 && y > 0 && answer_grid[x + 1][y - 1] != 12) {
onClick(x + 1, y - 1);
}
if (x < tile_count - 1 && y < tile_count - 1 && answer_grid[x + 1][y + 1] != 12) {
onClick(x + 1, y + 1);
}
if (y < tile_count - 1 && x > 0 && answer_grid[x - 1][y + 1] != 12) {
onClick(x - 1, y + 1);
}
}
public void tileClick(int x, int y) {
if (y > 0 && input_grid[x][y - 1] != 2) {
onClick(x, y - 1);
}
if (y < tile_count - 1 && input_grid[x][y + 1] != 2) {
onClick(x, y + 1);
}
if (x > 0 && input_grid[x - 1][y] != 2) {
onClick(x - 1, y);
}
if (x < tile_count - 1 && input_grid[x + 1][y] != 2) {
onClick(x + 1, y);
}
if (x > 0 && y > 0 && input_grid[x - 1][y - 1] != 2) {
onClick(x - 1, y - 1);
}
if (x < tile_count - 1 && y > 0 && input_grid[x + 1][y - 1] != 2) {
onClick(x + 1, y - 1);
}
if (x < tile_count - 1 && y < tile_count - 1 && input_grid[x + 1][y + 1] != 2) {
onClick(x + 1, y + 1);
}
if (y < tile_count - 1 && x > 0 && input_grid[x - 1][y + 1] != 2) {
onClick(x - 1, y + 1);
}
}
public int adjacentFlag(int x, int y) {
int flags = 0;
if (y > 0 && input_grid[x][y - 1] == 2) {
flags++;
}
if (y < tile_count - 1 && input_grid[x][y + 1] == 2) {
flags++;
}
if (x > 0 && input_grid[x - 1][y] == 2) {
flags++;
}
if (x < tile_count - 1 && input_grid[x + 1][y] == 2) {
flags++;
}
if (x > 0 && y > 0 && input_grid[x - 1][y - 1] == 2) {
flags++;
}
if (x < tile_count - 1 && y > 0 && input_grid[x + 1][y - 1] == 2) {
flags++;
}
if (x < tile_count - 1 && y < tile_count - 1 && input_grid[x + 1][y + 1] == 2) {
flags++;
}
if (y < tile_count - 1 && x > 0 && input_grid[x - 1][y + 1] == 2) {
flags++;
}
return flags;
}
public void rightClick(int x, int y) {
input_grid[x][y] = input_grid[x][y] == 2 ? 0 : 2;
}
public void restart() {
answer_grid = new int[tile_count][tile_count];
input_grid = new int[tile_count][tile_count];
first = true;
finished = false;
repaint();
}
@Override
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < answer_grid.length; i++) {
for (int j = 0; j < answer_grid[0].length; j++) {
if (finished) {
if (input_grid[i][j] == -1)
g.drawImage(sprites.get(13), i * pixels + 7, j * pixels + 30, pixels, pixels, null);
else if (input_grid[i][j] == 2)
if (answer_grid[i][j] == 12)
g.drawImage(sprites.get(11), i * pixels + 7, j * pixels + 30, pixels, pixels, null);
else
g.drawImage(sprites.get(9), i * pixels + 7, j * pixels + 30, pixels, pixels, null);
else
g.drawImage(sprites.get(answer_grid[i][j]), i * pixels + 7, j * pixels + 30, pixels, pixels, null);
// g.drawString("Time: " + time, size / 2, size / 2);
}
else if (input_grid[i][j] == 1)
g.drawImage(sprites.get(answer_grid[i][j]), i * pixels + 7, j * pixels + 30, pixels, pixels, null);
else if (input_grid[i][j] == 2)
g.drawImage(sprites.get(11), i * pixels + 7, j * pixels + 30, pixels, pixels, null);
else
g.drawImage(sprites.get(10), i * pixels + 7, j * pixels + 30, pixels, pixels, null);
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
int x = (e.getX() - 7) / pixels;
int y = (e.getY() - 30) / pixels;
// if (e.getButton() == MouseEvent.BUTTON2) {
// restart();
// return;
// }
if (finished) {
restart();
return;
}
if (e.getButton() == MouseEvent.BUTTON3 && (input_grid[x][y] == 0 || input_grid[x][y] == 2)) {
rightClick(x, y);
repaint();
return;
}
if (first) {
generate(x, y);
first = false;
}
if (input_grid[x][y] == 1) {
if (adjacentFlag(x, y) != answer_grid[x][y]) return;
tileClick(x, y);
repaint();
}
else if (input_grid[x][y] == 2);
else {
onClick(x, y);
repaint();
}
checkFinished();
}
public void checkFinished() {
int tiles = tile_count * tile_count - mines;
int clicked = 0;
for (int i = 0; i < input_grid.length; i++) {
for (int j = 0; j < input_grid[0].length; j++) {
if (input_grid[i][j] == 1) clicked++;
}
}
if (clicked == tiles) finished = true;
}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
public static void main(String[] args) {
// 20 30 50
new Minesweeper(20, 30, 50);
}
}