This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainFrm.java
149 lines (128 loc) · 4.26 KB
/
MainFrm.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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
public class MainFrm extends JFrame implements MouseListener {
private final ImageIcon bombIcon = new ImageIcon("mine.png");
private final ImageIcon flagIcon = new ImageIcon("flag.png");
private Minesweeper map;
private JButton button[][];
private boolean flag[][];
private boolean gameStatus = true;
private int openedMines = 0;
public MainFrm() {
setTitle("Minesweeper");
setSize(500, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
map = new Minesweeper();
button = new JButton[map.size()][map.size()];
flag = new boolean[map.size()][map.size()];
createFrm();
setVisible(true);
}
private void createFrm() {
JPanel panel = new JPanel(new GridLayout(map.size(), map.size()));
for (int i=0; i<map.size(); i++)
for (int j=0; j<map.size(); j++) {
button[i][j] = new JButton();
button[i][j].addMouseListener(this);
panel.add(button[i][j]);
}
add(panel);
}
private int[] getButtonPressed(Object src) {
for (int i=0; i<map.size(); i++)
for (int j=0; j<map.size(); j++)
if (button[i][j] == src) {
int retval[] = {i, j};
return retval;
}
return null;
}
public void mouseReleased(MouseEvent e) {
if (gameStatus) {
int idx[] = getButtonPressed(e.getSource());
int x = idx[0];
int y = idx[1];
if (e.getModifiers() == InputEvent.BUTTON3_MASK) { // right click
if (button[x][y].isEnabled()) {
if (!flag[x][y])
button[x][y].setIcon(flagIcon);
else
button[x][y].setIcon(null);
flag[x][y] = !flag[x][y];
}
}
else if (e.getModifiers() == InputEvent.BUTTON1_MASK) { // left click
if (map.value(x, y) == 0) { // mines
button[x][y].setEnabled(false);
openedMines++;
showMines(x, y);
}
else if (map.value(x, y) < 0) { // bombs
showAllBomb();
gameStatus = false;
}
else if (map.value(x, y) > 0) { // mines
button[x][y].setText("" + map.value(x, y));
button[x][y].setEnabled(false);
openedMines++;
}
checkWinner();
}
}
}
private void checkWinner() {
if (gameStatus) {
if (openedMines >= map.mines()) {
gameStatus = false;
showAllBomb();
JOptionPane.showMessageDialog(this, "You win!");
}
}
else {
JOptionPane.showMessageDialog(this, "Game over!", "Game over!", JOptionPane.ERROR_MESSAGE);
}
}
private void showAllBomb() {
for (int i=0; i<map.size(); i++)
for (int j=0; j<map.size(); j++)
if (map.value(i, j) < 0)
button[i][j].setIcon(bombIcon);
}
private void showMines(int x, int y) {
LinkedList<Integer> Q = new LinkedList<Integer>();
Q.add(x);
Q.add(y);
while (Q.size() > 0) {
x = Q.remove();
y = Q.remove();
for (int i=-1; i<=1; i++) {
for (int j=-1; j<=1; j++) {
if (x+i >= 0 && x+i < map.size() && y+j >= 0 && y+j < map.size()) {
if ((map.value(x+i, y+j) >= 0) && (button[x+i][y+j].isEnabled())) {
button[x+i][y+j].setEnabled(false);
if (map.value(x+i, y+j) > 0) {
button[x+i][y+j].setText("" + map.value(x+i, y+j));
}
else if (map.value(x+i, y+j) == 0) {
Q.add(x+i);
Q.add(y+j);
}
openedMines++;
}
}
}
}
}
}
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public static void main(String[] args) {
MainFrm frm = new MainFrm();
}
}