-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
346 lines (285 loc) · 9.2 KB
/
Grid.java
File metadata and controls
346 lines (285 loc) · 9.2 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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//YOUTUBE LINK ---> https://youtu.be/S95Ojx8VqSM
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Grid extends JFrame {
private boolean[][] bombGrid;
private int[][] countGrid;
private int numColumns;
private int numRows;
private int numBombs;
private static final int NUMCOLUMNS = 10;
private static final int NUMROWS = 10;
private static final int NUMBOMBS = 25;
private JButton button[][];
private JFrame gridFrame;
private int win;
public Grid() {
this.numBombs = NUMBOMBS;
this.numRows = NUMROWS;
this.numColumns = NUMCOLUMNS;
createBombGrid();
createCountGrid();
gui();
}
public Grid(int rows, int columns) {
this.numBombs = NUMBOMBS;
this.numRows = rows;
this.numColumns = columns;
createBombGrid();
createCountGrid();
gui();
}
public Grid(int rows, int columns, int numBombs) {
numRows = rows;
numColumns = columns;
this.numBombs = numBombs;
createBombGrid();
createCountGrid();
gui();
}
public int getNumRows() {
return numRows;
}
public int getNumColumns() {
return numColumns;
}
public int getNumBombs() {
return numBombs;
}
public boolean[][] getBombGrid() {
boolean copyBombGrid[][] = new boolean[numRows][numColumns];
for (int r = 0; r < copyBombGrid.length; r++) {
for (int c = 0; c < copyBombGrid[r].length; c++) {
copyBombGrid[r][c] = bombGrid[r][c];
}
}
return copyBombGrid;
}
public int[][] getCountGrid() {
int copycountGrid[][] = new int[numRows][numColumns];
for (int r = 0; r < copycountGrid.length; r++) {
for (int c = 0; c < copycountGrid[r].length; c++) {
copycountGrid[r][c] = countGrid[r][c];
}
}
return copycountGrid;
}
public boolean isBombAtLocation(int row, int column) {
return bombGrid[row][column];
}
public int getCountAtLocation(int r, int c) {
int count = 0;
// checks the starting point
if (c >= 0 && r >= 0 && c < numColumns && r < numRows && isBombAtLocation(r, c)) {
count++;
}
// checks right
if (c + 1 >= 0 && r >= 0 && c + 1 < numColumns && r < numRows && isBombAtLocation((r), c + 1)) {
count++;
}
// checks left
if (c - 1 >= 0 && r >= 0 && c - 1 < numColumns && r < numRows && isBombAtLocation((r), c - 1)) {
count++;
}
// checks down left
if (c - 1 >= 0 && r + 1 >= 0 && c - 1 < numColumns && r + 1 < numRows && isBombAtLocation((r + 1), c - 1)) {
count++;
}
// checks down right
if (c + 1 >= 0 && r + 1 >= 0 && c + 1 < numColumns && r + 1 < numRows && isBombAtLocation((r + 1), c + 1)) {
count++;
}
// checks down
if (c >= 0 && r + 1 >= 0 && c < numColumns && r + 1 < numRows && isBombAtLocation(r + 1, c)) {
count++;
}
// checks up
if ((c >= 0 && r - 1 >= 0 && c < numColumns && r - 1 < numRows) && (isBombAtLocation((r - 1), c))) {
count++;
}
// checks top left
if (c - 1 >= 0 && r - 1 >= 0 && c - 1 < numColumns && r - 1 < numRows && isBombAtLocation((r - 1), c - 1)) {
count++;
}
// checks top right
if (c + 1 >= 0 && r - 1 >= 0 && c + 1 < numColumns && r - 1 < numRows && isBombAtLocation((r - 1), c + 1)) {
count++;
}
return count;
}
private void createBombGrid() {
int numOfBombs = 0, row = 0, column = 0;
Random randomBombs = new Random();
bombGrid = new boolean[numRows][numColumns];
boolean numOfBombsInGrid;
// goes through both the rows and columns and sets everything to false
for (int r = 0; r < bombGrid.length; r++) {
for (int c = 0; c < bombGrid[r].length; c++) {
bombGrid[r][c] = false;
}
}
// generates random bombs
while (numOfBombs < numBombs) {
row = randomBombs.nextInt(numRows);
column = randomBombs.nextInt(numColumns);
numOfBombsInGrid = bombGrid[row][column];
if (numOfBombsInGrid == false) {
bombGrid[row][column] = true;
numOfBombs= numOfBombs + 1;
}
}
}
private void createCountGrid() {
countGrid = new int[numRows][numColumns];
for (int r = 0; r < numRows; r++) {
for (int c = 0; c < numColumns; c++) {
countGrid[r][c] = getCountAtLocation(r, c);
}
}
}
private void gui() {
gridFrame = new JFrame();
// number of boxes needed to click in order to win
// the size of the grid depends on the rows * columns
setLayout(new GridLayout(numRows, numColumns));
// each botton has a coordinate
button = new JButton[numRows][numColumns];
int r = 0, c = 0;
for (r = 0; r < numRows; r++) {
for (c = 0; c < numColumns; c++) {
button[r][c] = new JButton();
button[r][c].setForeground(Color.blue);
button[r][c].addActionListener(new ButtonListener(r, c));
add(button[r][c]);
}
}
setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(true);
this.setTitle("MineSweeper by Ana Leonardo");
}
public void winner() {
for (int r = 0; r < getNumRows(); r++) {
for (int c = 0; c < getNumColumns(); c++) {
button[r][c] = new JButton("");
button[r][c].setForeground(Color.blue);
button[r][c].setEnabled(true);
}
}
String userNumBombs = "Would you like to choose the number of Bombs?";
String wonGame = "You WON! Would you like to try again?";
String lost = "You Lost! Would you like to try again?";
String userEdit = "Would you like to make your own rows and columns?";
int rowS = 0, columnS = 0, b;
int userAnswer = JOptionPane.showConfirmDialog(gridFrame, wonGame);
if(userAnswer == JOptionPane.YES_OPTION) {
userAnswer = JOptionPane.showConfirmDialog(gridFrame, userEdit);
if(userAnswer == JOptionPane.YES_OPTION) {
String userRows = JOptionPane.showInputDialog("How many rows do you want?");
String userColumns = JOptionPane.showInputDialog("How many Columns do you want?");
rowS = Integer.parseInt(userRows);
columnS = Integer.parseInt(userColumns);
int userBombs = JOptionPane.showConfirmDialog(gridFrame, userNumBombs);
if(userAnswer == JOptionPane.YES_OPTION) {
String userB = JOptionPane.showInputDialog("How many rows do you want?");
b = Integer.parseInt(userB);
Grid c = new Grid(rowS,columnS,b);
}
if(userAnswer == JOptionPane.NO_OPTION) {
Grid c = new Grid(rowS,columnS);
}
if(userAnswer == JOptionPane.NO_OPTION) {
Grid c = new Grid();
}
}
else {
System.exit(0);
}
}
}
public void reset() {
Grid newG;
for (int r = 0; r < getNumRows(); r++) {
for (int c = 0; c < getNumColumns(); c++) {
button[r][c] = new JButton("");
button[r][c].setForeground(Color.blue);
button[r][c].setEnabled(true);
}
}
String lost = "You Lost! Would you like to try again?";
String userEdit = "Would you like to make your own rows and columns?";
String userNumBombs = "Would you like to choose the number of Bombs?";
int userAnswer = JOptionPane.showConfirmDialog(gridFrame, lost);
int rowS = 0, columnS = 0, b;
if (userAnswer == JOptionPane.YES_OPTION) {
userAnswer = JOptionPane.showConfirmDialog(gridFrame, userEdit);
if (userAnswer == JOptionPane.YES_OPTION) {
String userRows = JOptionPane.showInputDialog("How many rows do you want?");
String userColumns = JOptionPane.showInputDialog("How many Columns do you want?");
rowS = Integer.parseInt(userRows);
columnS = Integer.parseInt(userColumns);
int userBombs = JOptionPane.showConfirmDialog(gridFrame, userNumBombs);
if (userBombs == JOptionPane.YES_OPTION) {
String userB = JOptionPane.showInputDialog("How many Bombs do you want?");
b = Integer.parseInt(userB);
newG = new Grid(rowS, columnS, b);
}
if (userBombs == JOptionPane.NO_OPTION) {
newG= new Grid(rowS, columnS);
}
}
else {
//(userAnswer == JOptionPane.NO_OPTION) {
newG = new Grid();//NUMROWS, NUMCOLUMNS);
}
}
else {
System.exit(0);
}
}
private class ButtonListener implements ActionListener {
int row, col;
public ButtonListener(int row, int col) {
this.row = row;
this.col = col;
}
@Override
public void actionPerformed(ActionEvent e) {
// if bombs exists in that location
if (isBombAtLocation(row, col)) {
// print out everything when you lose
if (isBombAtLocation(row, col)) {
for (int r = 0; r < getNumRows(); r++) {
for (int c = 0; c < getNumColumns(); c++) {
if (!isBombAtLocation(r, c)) {
button[r][c].setText(String.valueOf(getCountAtLocation(r, c)));
} else {
button[r][c].setIcon(new ImageIcon("C:\\Users\\Dell Inspiron\\Downloads\\swing.jpg"));
}
}
}
reset();
}
else {
button[row][col].setText(String.valueOf(getCountAtLocation(row, col)));
}
}
// prints how many bombs are near
else {
button[row][col].setText(String.valueOf(getCountAtLocation(row, col)));
win++;
if (win == (numRows * numColumns) - numBombs) {
winner();
}
}
}
}
}