-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinesweeper_GUI.java
323 lines (261 loc) · 8.81 KB
/
Minesweeper_GUI.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
import java.awt.Graphics;
import java.awt.GridLayout;
//import java.awt.event.ActionEvent;
//import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
//import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Minesweeper_GUI extends JPanel implements MouseListener {
/**
*
*/
private static final long serialVersionUID = 1L;
//USE JDIALOG FOR WIN / LOSE CONDITIONS
int numOfBombs;
int placeBombs;
int tileCount = 0;
int turn = 0;
Cell[][] cells = new Cell[10][10];
Cell last_clicked = null;
MouseEvent last_clicked_event = null;
JFrame frame;
Minesweeper_GUI() {
this.frame = new JFrame();
frame.add(this);
//Set up window
frame.setSize(600,600);
frame.setTitle("Minesweeper");
GridLayout playingField = new GridLayout(10,10);
//end set up window
//set up game
numOfBombs = 10;
placeBombs = numOfBombs;
//System.out.println(placeBombs);
this.setLayout(playingField);
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 10; j++) {
//JButton button = new JButton("");
//window.add(new JButton(""));
Cell b = new Cell(this, i, j);
cells[i][j] = b;
//button.addActionListener(b);
//b.addMouseListener(b);
this.add(cells[i][j]);
}
}
//Implement a feature that generates the bomb locations after the first click
//cells[0][0].setBombCount(numOfBombs);//static variables
//cells[0][0].setTileCount(100);
while(placeBombs > 0) {//Places bombs
//System.out.println("Infinite Loop");
int x = (int) ((double)(Math.random() * 10));
int y = (int) ((double)(Math.random() * 10));
//System.out.println(x + " " + y);
if(cells[x][y].getBomb() == false) {
cells[x][y].setBomb(true);
placeBombs -= 1;
}
}
for(int i = 0; i < 10; i++) {//Calculates bombsNear
for(int j = 0; j < 10; j++) {
int count = 0;
if (cells[i][j].getBomb()) {
cells[i][j].setBombsNear(-1);
continue;
}
if(i > 0 && (cells[i-1][j].getBomb()== true)) {//To the left
count += 1;
}
if(i < 9 && (cells[i + 1][j].getBomb() == true)) {//To the right
count += 1;
}
if(j > 0 && (cells[i][j - 1].getBomb()== true)) {//To the Top
count += 1;
}
if(j < 9 && (cells[i][j + 1].getBomb() == true)) {//To the Bottom
count += 1;
}
if(i > 0 && j > 0 && (cells[i-1][j-1].getBomb()== true)) {//To the Top left
count += 1;
}
if(i < 9 && j > 0 && (cells[i + 1][j - 1].getBomb() == true)) {//To the Top right
count += 1;
}
if(i > 0 && j < 9 && (cells[i-1][j + 1].getBomb()== true)) {//To the Bottom left
count += 1;
}
if(i < 9 && j < 9 && (cells[i + 1][j + 1].getBomb() == true)) {//To the Bottom right
count += 1;
}
cells[i][j].setBombsNear(count);
cells[i][j].repaint();
}
}
//cells[0][0].CopyArray(cells);//Transfer array over to cell
//end set up game
//Check for win conditions / Fix win condition
//Do a recursive call to reveal adjacent blank squares
//Upon losing expose all mines
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
void floodfill_tile(int x, int y) {
if (0 <= x && x < cells.length && 0 <= y && y < cells[0].length && cells[x][y].getClicked() == false) {
cells[x][y].setClicked(true);// = true;
tileCount += 1;
//System.out.println(cells[x][y].bombsNear);
String text =" ";
if (cells[x][y].getBombsNear() > 0) text = cells[x][y].getBombsNear() + "";
cells[x][y].setText(text);
if (cells[x][y].getBombsNear() > 0) return;
floodfill_tile(x+1, y);
floodfill_tile(x-1, y);
floodfill_tile(x, y+1);
floodfill_tile(x, y-1);
}
}
boolean gameover = false;
@Override
protected void paintComponent(Graphics g) {
//FIX SEAL
if (!gameover) {
if(tileCount == 100) {
gameover = true;
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame , "Congratulations! You won the game!" ,"Game Done", JOptionPane.INFORMATION_MESSAGE);
}
if (last_clicked != null) {
this.last_clicked_event.getButton();
if(last_clicked_event.getButton() == 3 && !last_clicked.getClicked()) {
if(last_clicked.getText() != (last_clicked.getBombsNear() + "")) {
if(last_clicked.getText() == "S") {
last_clicked.setText("");
tileCount -= 1;
} else {
last_clicked.setText("S");
tileCount += 1;
}
}
}
if(last_clicked_event.getButton() == 1 && !last_clicked.getClicked()) {
if(last_clicked.getText() != "S") {
last_clicked.setClicked(true);// = true;
if (last_clicked.getBombsNear() >= 0) {
if (last_clicked.getBombsNear() == 0) {
last_clicked.setClicked(false);// = false;
floodfill_tile(last_clicked.getROW(), last_clicked.getCOLUMN());
} else {
if(last_clicked.getText() != last_clicked.getBombsNear() + "") {
last_clicked.setText("" + last_clicked.getBombsNear());
tileCount += 1;
} else {
}
}
}else if (last_clicked.getBomb()) {
if(turn == 0) {//If clicked bomb on first turn
int xPos =last_clicked.getROW();
int yPos =last_clicked.getCOLUMN();
cells[xPos][yPos].setBomb(false);
placeBombs = 1;
while(placeBombs > 0) {//Places bombs
//System.out.println("Infinite Loop");
int x = (int) ((double)(Math.random() * 10));
int y = (int) ((double)(Math.random() * 10));
//System.out.println(x + " " + y);
if(cells[x][y].getBomb() == false && ((x != xPos)||(y != yPos))) {
cells[x][y].setBomb(true);
placeBombs -= 1;
}
}
for(int i = 0; i < 10; i++) {//Calculates bombsNear
for(int j = 0; j < 10; j++) {
int count = 0;
if (cells[i][j].getBomb()) {
cells[i][j].setBombsNear(-1);
continue;
}
if(i > 0 && (cells[i-1][j].getBomb()== true)) {//To the left
count += 1;
}
if(i < 9 && (cells[i + 1][j].getBomb() == true)) {//To the right
count += 1;
}
if(j > 0 && (cells[i][j - 1].getBomb()== true)) {//To the Top
count += 1;
}
if(j < 9 && (cells[i][j + 1].getBomb() == true)) {//To the Bottom
count += 1;
}
if(i > 0 && j > 0 && (cells[i-1][j-1].getBomb()== true)) {//To the Top left
count += 1;
}
if(i < 9 && j > 0 && (cells[i + 1][j - 1].getBomb() == true)) {//To the Top right
count += 1;
}
if(i > 0 && j < 9 && (cells[i-1][j + 1].getBomb()== true)) {//To the Bottom left
count += 1;
}
if(i < 9 && j < 9 && (cells[i + 1][j + 1].getBomb() == true)) {//To the Bottom right
count += 1;
}
cells[i][j].setBombsNear(count);
}
}
last_clicked.setClicked(true);
if(last_clicked.getText() != last_clicked.getBombsNear() + "") {
last_clicked.setText("" + last_clicked.getBombsNear());
tileCount += 1;
} else {
}
} else {
last_clicked.setText("X");
for(int i = 0; i < cells.length; i++) {
for(int j = 0; j < cells[0].length; j++) {
if(cells[i][j].getText() == "S") {
cells[i][j].setText("");
}
if(cells[i][j].getBomb()) {
cells[i][j].setText("X");
}
}
}
gameover = true;
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame , "BOOM! You lost!" ,"Game Done", JOptionPane.INFORMATION_MESSAGE);
}
}
}
turn += 1;
}
}
last_clicked = null;
last_clicked_event = null;
//super.paintComponent(g);
repaint();
}
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
//System.out.println(e);
}
}