-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathField.java
235 lines (210 loc) · 6.74 KB
/
Field.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
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
class Field {
private Space[][] field;
public Field(int size, int bombs){
if (size*size < bombs){
throw new IllegalArgumentException("Bomb count cannot be greater than total size of grid!");
}
field = new Space[size][size];
populateBombs(bombs);
populateSpaces();
}
// Creates new Bomb Spaces at random places on the field
private void populateBombs(int bombs){
for (int i = 0; i < bombs; i++){
int x = (int) (Math.random() * field.length);
int y = (int) (Math.random() * field.length);
// If the Space already exists, run again.
if (field[y][x] != null){
i--;
continue;
}
field[y][x] = new Space(true, x, y);
}
}
// Fill every space with how many bombs are adjacent to it
private void populateSpaces(){
for(int i = 0; i < field.length; i++){
for(int j = 0; j < field[i].length; j++){
//Ignore if it's a bomb (not null)
if (field[i][j] != null){
continue;
}
int count = 0;
// Checking for all the possible adjacent positions
Space[] adj = getAdjacentSpaces(i,j);
for(int k = 0; k < adj.length; k++){
if (adj[k] != null){
if (adj[k].isBomb()){
count++;
}
}
}
// Set count to this Space
field[i][j] = new Space(count, j, i);
}
}
System.out.println();
}
// Commands
// Reveals a space
public boolean reveal(int x, int y){
if(isRevealed(x,y)){
return false;
}
field[y][x].reveal();
// If this space is 0, flood spaces around it
if (field[y][x].getAdj() == 0){
flood(x, y);
}
return true;
}
// Flags a space
public boolean flag(int x, int y){
if(field[y][x].isFlagged()){
return false;
}
field[y][x].flag();
return true;
}
// Unflags a space
public boolean unFlag(int x, int y){
if(!field[y][x].isFlagged()){
return false;
}
field[y][x].unFlag();
return true;
}
// Reveal all spaces (for when game ends)
public void revealAll(){
for(int i = 0; i < field.length; i++){
for (int j = 0; j < field[0].length; j++){
field[i][j].reveal();
}
}
}
// Checks if all bombs are flagged
public boolean isGameWon(){
for(Space[] row : field){
for(Space s : row){
if(s.isBomb() && !s.isFlagged()){
return false;
}
}
}
return true;
}
// Checks if a bomb has been revealed
public boolean isBombDetonated(){
for(Space[] row : field){
for(Space s : row){
if(s.isBomb() && s.isRevealed()){
return true;
}
}
}
return false;
}
// Returns if a given Space has been revealed
public boolean isRevealed(int x, int y){
return field[y][x].isRevealed();
}
// Returns if a given Space has been flagged
public boolean isFlagged(int x, int y){
return field[y][x].isFlagged();
}
// Internal supplemental methods
private void flood(int x, int y){
// Get adjacent spaces
Space[] adj = getAdjacentSpaces(y, x);
// If the space flood is called on is zero, reveal all around it.
for(Space s : adj){
if (s == null || s.isRevealed()){
continue;
}
reveal(s.getX(), s.getY());
if(s.getAdj() == 0){
flood(s.getX(), s.getY());
}
}
}
// Check whether position is valid or not
private boolean isValidPos(int i, int j){
if (i < 0 || j < 0 || i > field.length - 1 || j > field.length - 1) {
return false;
}
return true;
}
// Get all spaces adjacent to provided coordinates
private Space[] getAdjacentSpaces(int i, int j){
Space[] spaces = new Space[8];
if (isValidPos(i - 1, j - 1)) {
spaces[0] = field[i - 1][j - 1];
}
if (isValidPos(i - 1, j)) {
spaces[1] = field[i - 1][j];
}
if (isValidPos(i - 1, j + 1)) {
spaces[2] = field[i - 1][j + 1];
}
if (isValidPos(i, j - 1)) {
spaces[3] = field[i][j - 1];
}
if (isValidPos(i, j + 1)) {
spaces[4] = field[i][j + 1];
}
if (isValidPos(i + 1, j - 1)) {
spaces[5] = field[i + 1][j - 1];
}
if (isValidPos(i + 1, j)) {
spaces[6] = field[i + 1][j];
}
if (isValidPos(i + 1, j + 1)) {
spaces[7] = field[i + 1][j + 1];
}
return spaces;
}
// Display the field as revealed
public void display() {
int counter = 0;
// Spacing for top numbers
System.out.print("\u001B[4m ");
for (int i = 0; i < field[0].length; i++) {
counter++;
System.out.print(" " + counter + " ");
}
System.out.println();
for (int i = 0; i < field.length; i++) {
System.out.print(i + 1 + " ");
for (Space s : field[i]) {
// new space padding
System.out.print("| ");
// otherwise print the piece (becomes the owner's symbol)
if (s.isRevealed() ) {
System.out.print(s);
}else if (s.isFlagged()) {
System.out.print("$");
} else {
System.out.print("#");
}
// ending padding
System.out.print(" ");
}
// EOL padding
System.out.println("|");
}
System.out.print("\u001B[0m");
}
// toString override
@Override
public String toString(){
String base = new String();
for(int i = 0; i < field.length; i++){
for(int j = 0; j < field[i].length; j++){
Space current = field[i][j];
base = base + current.toString();
}
base = base + "\n";
}
return base;
}
}