-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMines.java
More file actions
189 lines (171 loc) · 5.31 KB
/
Copy pathMines.java
File metadata and controls
189 lines (171 loc) · 5.31 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
package mines;
import java.util.Random;
public class Mines {
private int height, width;
private Dot[][] dots;
public Mines(int height, int width, int numMines) {
this.height = height;
this.width = width;
dots = new Dot[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
dots[i][j] = new Dot();
}
}
for (int i = 0; i < numMines; i++) {
if (!addMine(new Random().nextInt(height), new Random().nextInt(width)))
i--;
}
}
public boolean addMine(int i, int j) {
if (!dots[i][j].mine) {
dots[i][j].mine = true;
for (int m = 0; m < height; m++)
for (int n = 0; n < width; n++)// save for each spot the number of mines around
dots[m][n].minesAround = cuntMines(m, n);
return true;
}
return false;
}
public boolean open(int i, int j) {
if (!dots[i][j].opened)// if not opened
if (dots[i][j].mine == true) {// if this is a mine
return false;// return false to indicate that it is a mine
} else {// if not a mine
dots[i][j].opened = true;// set as opened
if (dots[i][j].minesAround == 0) {// if there are no mines around
// try to open sounding aria
if (i > 0) {
open(i - 1, j);
}
if (i < height - 1) {
open(i + 1, j);
}
if (j > 0) {
open(i, j - 1);
}
if (j < width - 1) {
open(i, j + 1);
}
if (i > 0 && j > 0) {
open(i - 1, j - 1);
}
if (i < height - 1 && j > 0) {
open(i + 1, j - 1);
}
if (i > 0 && j < width - 1) {
open(i - 1, j + 1);
}
if (i < height - 1 && j < width - 1) {
open(i + 1, j + 1);
}
}
return true;
}
else
return true;// if the spot is already open no need to try again
// but it is not a false move
}
// put's a flag if there is not and removes it if there is
public void toggleFlag(int x, int y) {
if (dots[x][y].flag == true) {
dots[x][y].flag = false;
} else
dots[x][y].flag = true;
}
// checks if all the dot that are not mines are opened or closed
// if 1 dot is closed retures false
public boolean isDone() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (!dots[i][j].mine)
if (!dots[i][j].opened)
return false;
}
}
return true;
}
public String get(int i, int j) {
return dots[i][j].toString();
}
public void setShowAll(boolean showAll) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
dots[i][j].show = showAll;
}
}
}
public String toString() {
String str = "";
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
str = new String(str + dots[i][j].toString());
}
str = new String(str + "\n");
}
return str;
}
// count the mines arround evry dot
private int cuntMines(int i, int j) {
int count = 0;
if (i > 0) {
if (dots[i - 1][j].mine)
count++;
}
if (i < height - 1) {
if (dots[i + 1][j].mine)
count++;
}
if (j > 0) {
if (dots[i][j - 1].mine)
count++;
}
if (j < width - 1) {
if (dots[i][j + 1].mine)
count++;
}
if (i > 0 && j > 0) {
if (dots[i - 1][j - 1].mine)
count++;
}
if (i < height - 1 && j > 0) {
if (dots[i + 1][j - 1].mine)
count++;
}
if (i > 0 && j < width - 1) {
if (dots[i - 1][j + 1].mine)
count++;
}
if (i < height - 1 && j < width - 1) {
if (dots[i + 1][j + 1].mine)
count++;
}
return count;
}
private class Dot {
boolean mine, opened, flag, show;
int minesAround;
private Dot() {
mine = false;
opened = false;
flag = false;
show = false;
minesAround = 0;
}
public String toString() {
if (!this.opened && !this.show) {
if (!this.flag) {
return ".";
} else
return "F";
} else {
if (this.mine) {
return "X";
} else if (this.minesAround == 0) {
return " ";
} else
return "" + minesAround;
}
}
}
}