-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminesweeper.java
More file actions
248 lines (233 loc) · 8.32 KB
/
minesweeper.java
File metadata and controls
248 lines (233 loc) · 8.32 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
package minesweeper;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
/**
*
* @author Stusiak
*/
public class Minesweeper implements ActionListener {
JFrame frame;
JButton beginner;
JButton intermediate;
JButton expert;
JPanel subPanel;
public static GroupLayout layout;
public static final int TILE_WIDTH = 40;
int rows = 16;
int cols = 30;
int NUM_MINES = 10;
public int[][] grid = new int[rows][cols];
ArrayList<Integer> mineNums = new ArrayList<>();
ImageIcon MINE = new ImageIcon("mine.png");
ImageIcon TILE = new ImageIcon("minesweeper tile.jpg");
ImageIcon RED_MINE = new ImageIcon("red_mine.png");
ImageIcon FLAG = new ImageIcon("flagged tile.jpg");
ArrayList<Location> checked = new ArrayList<>();
ArrayList<Location> exposed = new ArrayList<>();
int numExposed;
JButton[][] buttons;
HashMap buttonLocations = new HashMap();
HashMap buttonByLocation = new HashMap();
JButton current;
boolean playing;
/**
* @param args the command line arguments
*/
private void clearCellsAround(int row, int col) {
Location loc = new Location(row,col);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int c = col-1+i;
int r = row-1+j;
Location newLoc = new Location(r,c);
if (0 <= r && r < rows && 0 <= c && c < cols) {
int val = grid[r][c];
boolean cond1 = !newLoc.isIn(checked);
boolean cond2 = !newLoc.isIn(exposed);
if (cond1 && cond2){
current = buttons[r][c];
checked.add(newLoc);
if (val >= 0) {
numExposed++;
current.setBackground(Color.white);
current.setForeground(Color.black);
exposed.add(newLoc);
checkWin();
}
if (val == 0) {
clearCellsAround(r,c);
}
}
}
}
}
}
public static void main(String[] args) {
Minesweeper game = new Minesweeper();
}
public Minesweeper() {
TILE = scaleIcon(TILE);
MINE = scaleIcon(MINE);
RED_MINE = scaleIcon(RED_MINE);
FLAG = scaleIcon(FLAG);
playing = true;
numExposed = 0;
buttons = new JButton[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
grid[i][j] = 0;
}
}
Random rand = new Random();
while (mineNums.size() < NUM_MINES){
int newMine = rand.nextInt(rows*cols);
if (!mineNums.contains(newMine)) {
mineNums.add(newMine);
}
}
for (int num : mineNums) {
grid[num / cols][num % cols] = -1;
}
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (grid[row][col] == -1) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int r = row - 1 + i;
int c = col - 1 + j;
if (0<= r && r < rows && 0 <= c && c < cols){
if (grid[r][c] != -1){
grid[r][c] += 1;
}
}
}
}
}
}
}
frame = new JFrame("Minesweeper");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(16, 30));
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
int[] loc = new int[]{row,col};
JButton button = new JButton();
button.setPreferredSize(new Dimension(40,40));
int val = grid[row][col];
if (val != 0){
button.setText(Integer.toString(val));
} else {
button.setText(" ");
}
buttons[row][col] = button;
frame.add(buttons[row][col]);
buttonLocations.put(button, loc);
buttonByLocation.put(loc,button);
button.addActionListener(this);
button.setBackground(Color.lightGray);
button.setForeground(Color.lightGray);
button.setBorder(BorderFactory.createLineBorder(Color.black));
}
}
frame.pack();
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (playing) {
JButton currentButton = (JButton) e.getSource();
if ("-1".equals(currentButton.getText())) {
currentButton.setText("");
currentButton.setIcon(RED_MINE);
loseGame();
}
currentButton.setBackground(Color.white);
currentButton.setForeground(Color.black);
numExposed++;
int[] location = (int[])buttonLocations.get(currentButton);
int row = location[0];
int col = location[1];
Location currentLoc = new Location(row,col);
if (!currentLoc.isIn(exposed)) {
exposed.add(new Location(row,col));
checkWin();
if (" ".equals(currentButton.getText())) {
clearCellsAround(row,col);
}
}
System.out.println("Exposed: " + numExposed);
System.out.println();
System.out.println(rows*cols - exposed.size());
}
}
private void checkWin() {
if (exposed.size() == rows*cols - NUM_MINES) {
playing = false;
for (JButton[] buttonList : buttons) {
for (JButton button : buttonList) {
if ("-1".equals(button.getText())) {
button.setText("");
button.setIcon(FLAG);
}
}
}
}
}
private void loseGame() {
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++) {
JButton button = buttons[i][j];
button.setBackground(Color.white);
if ("-1".equals(button.getText())) {
button.setText("");
button.setIcon(MINE);
} else {
button.setForeground(Color.black);
}
}
}
}
private ImageIcon scaleIcon(ImageIcon imageIcon) {
Image image = imageIcon.getImage();
Image newimg = image.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
ImageIcon scaledIcon = new ImageIcon(newimg);
return scaledIcon;
}
private static class Location {
public int x;
public int y;
public Location(int x, int y) {
this.x = x;
this.y = y;
}
public static String toString(Location newLoc){
return "(" + Integer.toString(newLoc.x) + ", " + Integer.toString(newLoc.y) + ")";
}
public boolean isEqualTo(Location newLoc){
return this.x == newLoc.x && this.y == newLoc.y;
}
public boolean isIn(ArrayList<Location> newLocList) {
boolean result = false;
for (Location item : newLocList) {
if (item.isEqualTo(this)) {
result = true;
}
}
return result;
}
}
}