-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinesweeper.java
More file actions
203 lines (167 loc) · 4.85 KB
/
Minesweeper.java
File metadata and controls
203 lines (167 loc) · 4.85 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
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
@SuppressWarnings("serial")
public class Minesweeper extends JFrame {
private static final int HEIGHT = 400;
private static final int WIDTH = 400;
private static final int D = 9;
public static JButton[][] button = new JButton[(D+2)][(D+2)];
private static Minefield field;
private static int[][] realGrid;
private static ImageIcon[] icon = new ImageIcon[10];
private static boolean firstRun = true;
public Minesweeper(){
loadImages();
Container pane = getContentPane();
pane.add(new JMenuBar());
for (int x = 1; x <= D; x++){
for (int y = 1; y <= D; y++){
button[x][y] = new JButton(icon[0]);
button[x][y].addMouseListener(new ButtonHandler(x, y));
pane.add(button[x][y]);
}
}
for (int x = 0; x < (D+2); x++){
button[x][0] = new JButton(" ");
button[x][(D+1)] = new JButton(" ");
}
for (int y = 0; y < (D+2); y++){
button[0][y] = new JButton(" ");
button[(D+1)][y] = new JButton(" ");
}
setVisible(true);
setSize(HEIGHT, WIDTH);
setLayout(new GridLayout(D, D));
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void newGrid(int x, int y){
if (firstRun){
field = new Minefield(x, y);
realGrid = field.getMinefield();
firstRun = false;
}
int numberOfMines = realGrid[x][y];
if (numberOfMines == -1){
setVisible(false);
dispose();
}
if (numberOfMines > 0){
button[x][y].setIcon(getIconFromNumber(numberOfMines));
}
if (numberOfMines == 0){
button[x][y].setIcon(null);
button[x][y].setEnabled(false);
pointIsZero(x, y);
}
}
private static void pointIsZero(int x, int y){
int[] xPosition = Minefield.getPositionX(x);
int[] yPosition = Minefield.getPositionY(y);
for (int counter = 0; counter < 8; counter++){
int currentX = xPosition[counter];
int currentY = yPosition[counter];
if (button[currentX][currentY].getIcon() != null){
if (button[currentX][currentY].getIcon() == icon[0]){
int neighboringMines = realGrid[currentX][currentY];
if (neighboringMines > 0){
button[currentX][currentY].setIcon(getIconFromNumber(neighboringMines));
}
if (neighboringMines == 0){
button[currentX][currentY].setIcon(null);
button[currentX][currentY].setEnabled(false);
pointIsZero(currentX, currentY);
}
}
}
}
}
public static void markBox(int x, int y){
if (button[x][y].getIcon().equals(icon[0])){
button[x][y].setIcon(icon[9]);
}
else {
button[x][y].setIcon(icon[9]);
}
}
public static ImageIcon getIconFromNumber(int number){
switch (number) {
case 1: return icon[1];
case 2: return icon[2];
case 3: return icon[3];
case 4: return icon[4];
case 5: return icon[5];
case 6: return icon[6];
case 7: return icon[7];
case 8: return icon[8];
default: return null;
}
}
private static void loadImages(){
try {
icon[0] = new ImageIcon(ImageIO.read(new File("images/question.png")));
icon[1] = new ImageIcon(ImageIO.read(new File("images/1.png")));
icon[2] = new ImageIcon(ImageIO.read(new File("images/2.png")));
icon[3] = new ImageIcon(ImageIO.read(new File("images/3.png")));
icon[4] = new ImageIcon(ImageIO.read(new File("images/4.png")));
icon[5] = new ImageIcon(ImageIO.read(new File("images/5.png")));
icon[6] = new ImageIcon(ImageIO.read(new File("images/6.png")));
icon[7] = new ImageIcon(ImageIO.read(new File("images/7.png")));
icon[8] = new ImageIcon(ImageIO.read(new File("images/8.png")));
icon[9] = new ImageIcon(ImageIO.read(new File("images/bomb.png")));
}
catch (IOException e){
System.exit(0);
}
catch (IllegalArgumentException e) {
System.exit(0);
}
}
public ImageIcon getIcon(int iconNumber){
return icon[iconNumber];
}
class ButtonHandler implements MouseListener {
private int x;
private int y;
public ButtonHandler(int x, int y){
this.x = x;
this.y = y;
}
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3){
if (button[x][y].isEnabled()){
markBox(this.x, this.y);
}
}
if (e.getButton() == MouseEvent.BUTTON1){
if (button[x][y].isEnabled()){
newGrid(this.x, this.y);
}
}
}
@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
}
}
}