-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathField.java
More file actions
340 lines (300 loc) · 9.5 KB
/
Field.java
File metadata and controls
340 lines (300 loc) · 9.5 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import java.util.Random;
/**
* @author Michael
* @author Nik
* @author Shane
* @author Connor
*
* Class for aggregation and manipulation of Tile objects that
* constitute a Field.
*/
public class Field
{
private int _yRows;
private int _xColumns;
private Tile[][] _field;
public static final int DEFAULT_BOMB_RATE = 10;
public static final int DEFAULT_FIELD_SIZE = 10;
public static final int MAX_FIELD_WIDTH = 18;
// Return values for revealed tiles
public static final int TILE_REVEALED_NORMAL = 0;
public static final int TILE_REVEALED_BOMB = 1;
public static final int TILE_PREVIOUSLY_REVEALED = 2;
/**
* Creates a Field with the default size and bomb odds
*/
public Field()
{
this(DEFAULT_FIELD_SIZE);
}
/**
* Creates a Field with the given square size and default bomb odds
*/
public Field(int size)
{
this(size, size);
}
/**
* Creates a Field with given rows and columns, and default bomb odds
*/
public Field(int yRows, int xColumns)
{
this(yRows, xColumns, DEFAULT_BOMB_RATE);
}
/**
* Creates a field with given dimensions and bomb odds
*/
public Field(int yRows, int xColumns, int minePercentage)
{
_yRows = yRows;
_xColumns = xColumns <= MAX_FIELD_WIDTH ? xColumns : MAX_FIELD_WIDTH;
_field = new Tile[_yRows][_xColumns];
Random random = new Random();
// Fill the field with tiles
for (int i = 0; i < _yRows; i++)
{
for (int j = 0; j < _xColumns; j++)
{
_field[i][j] = new Tile(i, j, random.nextInt(100) < minePercentage);
}
}
}
/**
* Returns the tile at given coordinates
*
* @return the requested tile
*/
public Tile getTileAt(int yRow, int xColumn)
{
return _field[yRow][xColumn];
}
/**
* Returns the neighbors of a given tile
* @param t a tile to find the neighbors of
* @return an array of the given tile's neighbors
*/
public Tile[] getNeighbors(Tile t)
{
Tile[] neighbors = null;
// If the Tile t is at the corner of the field, it only has 3 neighbors.
if (t.getX() == 0 && t.getY() == 0
|| t.getX() == _xColumns - 1 && t.getY() == 0
|| t.getX() == _xColumns - 1 && t.getY() == _yRows - 1
|| t.getX() == 0 && t.getY() == _yRows - 1)
{
neighbors = new Tile[3];
// If we are at the top left corner Tile...
if (t.getX() == 0 && t.getY() == 0)
{
neighbors[0] = getTileAt(0, 1);
neighbors[1] = getTileAt(1, 1);
neighbors[2] = getTileAt(1, 0);
}
// If we are at the top right corner...
else if (t.getX() == _xColumns - 1 && t.getY() == 0)
{
neighbors[0] = getTileAt(0, t.getX() - 1);
neighbors[1] = getTileAt(1, t.getX() - 1);
neighbors[2] = getTileAt(1, t.getX() - 0);
}
// If we are at the bottom right corner.
else if (t.getX() == _xColumns - 1 && t.getY() == _yRows - 1)
{
neighbors[0] = getTileAt(t.getY(), t.getX() - 1);
neighbors[1] = getTileAt(t.getY() - 1, t.getX() - 1);
neighbors[2] = getTileAt(t.getY() - 1, t.getX());
}
// If we are at the bottom left corner.
else if (t.getX() == 0 && t.getY() == _yRows - 1)
{
neighbors[0] = getTileAt(t.getY() - 0, 1);
neighbors[1] = getTileAt(t.getY() - 1, 1);
neighbors[2] = getTileAt(t.getY() - 1, 0);
}
}
// If the Tile t is at the edge of the field, it has 5 neighbors.
else if (t.getY() == 0
|| t.getX() == _xColumns - 1
|| t.getY() == _yRows - 1
|| t.getX() == 0)
{
neighbors = new Tile[5];
if (t.getY() == 0)
{
neighbors[0] = getTileAt(0, t.getX()+1); //_field[_xColumns + 1][0];
neighbors[1] = getTileAt(1, t.getX()+1); //_field[1][1];
neighbors[2] = getTileAt(1, t.getX()); //_field[0][1];
neighbors[3] = getTileAt(1, t.getX()-1); //_field[1][0];
neighbors[4] = getTileAt(0, t.getX()-1); //_field[1][1];
}
if (t.getX() == _xColumns - 1)
{
neighbors[0] = getTileAt(t.getY()-1,t.getX()-1);
neighbors[1] = getTileAt(t.getY()-1, t.getX());
neighbors[2] = getTileAt(t.getY()+1, t.getX());
neighbors[3] = getTileAt(t.getY()+1, t.getX()-1);
neighbors[4] = getTileAt(t.getY(), t.getX()-1);
}
if (t.getY() == _yRows - 1)
{
neighbors[0] = getTileAt(t.getY()-1, t.getX()-1);
neighbors[1] = getTileAt(t.getY()-1, t.getX());
neighbors[2] = getTileAt(t.getY()-1,t.getX()+1);
neighbors[3] = getTileAt(t.getY(),t.getX()+1);
neighbors[4] = getTileAt(t.getY(),t.getX()-1);
}
if (t.getX() == 0)
{
neighbors[0] = getTileAt(t.getY()-1, 0);
neighbors[1] = getTileAt(t.getY()-1, 1);
neighbors[2] = getTileAt(t.getY(), 1);
neighbors[3] = getTileAt(t.getY()+1, 1);
neighbors[4] = getTileAt(t.getY()+1,0);
} // if
} // else if
// Otherwise the Tile t should have 8 neighbors.
else
{
neighbors = new Tile[8];
neighbors[0] = getTileAt(t.getY() - 1, t.getX() - 1);
neighbors[1] = getTileAt(t.getY() - 1, t.getX());
neighbors[2] = getTileAt(t.getY() - 1, t.getX() + 1);
neighbors[3] = getTileAt(t.getY(), t.getX() + 1);
neighbors[4] = getTileAt(t.getY() + 1, t.getX() + 1);
neighbors[5] = getTileAt(t.getY() + 1, t.getX());
neighbors[6] = getTileAt(t.getY() + 1, t.getX() - 1);
neighbors[7] = getTileAt(t.getY(), t.getX() - 1);
} // else
return neighbors;
}
/**
* Returns the number of "rows" in the field
*
* @return the number of "rows"
*/
public int getYRows()
{
return _yRows;
}
/**
* Returns the number of "columns" in the field
*
* @return the number of "columns"
*/
public int getXColumns()
{
return _xColumns;
}
/**
* Returns true if all non-bomb tiles have been revealed, false otherwise
*
* @return true if all non-bomb tiles have been revealed, false otherwise
*/
public boolean isWinner()
{
boolean win = true;
/*
* Check all tiles
* If a bomb has been revealed or a non-bomb has not been revealed, the
* game has not been won
*/
for (int i = 0; i < _yRows; i++)
{
for (int j = 0; j < _xColumns; j++)
{
if ( (_field[i][j].isBomb() == true
&& _field[i][j].isRevealed() == true)
|| (_field[i][j].isBomb() == false
&& _field[i][j].isRevealed() == false) )
{
win = false;
}
}
}
return win;
}
/**
* Used to reveal a selected tile. Returns an int indicating the outcome
* of the reveal attempt
*
* @return an int representing the outcome of the reveal
*/
public int revealTile(Tile t)
{
int returnStatus = TILE_REVEALED_NORMAL;
if (!t.isRevealed())
{
// If the tile was not revealed before, it is now
t.reveal();
if (t.isBomb())
{
returnStatus = TILE_REVEALED_BOMB;
}
else
{
// If the tile is not bomb-adjacent, reveal its neighbors
if (t.getVolatileNeighbors() == 0)
{
for (Tile neighbor : getNeighbors(t))
{
revealTile(neighbor);
}
}
}
}
else
{
returnStatus = TILE_PREVIOUSLY_REVEALED;
}
return returnStatus;
}
/**
* Returns a String representing the field in the form
* -------------
* | t | t | t |
* -------------
* | t | t | t |
* -------------
* | t | t | t |
* -------------
*
* @return a String representing the field
*/
public String toString()
{
// The string to be returned
StringBuilder outputString = new StringBuilder();
// For each row of values, append a row of separation dashes, then the
// row of values, with values pipe-separated
for (int i = 0; i < _yRows; i++)
{
// Row of sep dashes
for (int j = 0; j < _xColumns; j++)
{
outputString.append("----");
}
outputString.append("-\n");
// Open the row of tiles
outputString.append("|");
// Row of pipe-separated tiles
for (int j = 0; j < _xColumns; j++)
{
outputString.append(getTileAt(i, j).toString());
}
outputString.append(" " + (i + 1) + "\n");
}
// Bottom border of dashes
for (int j = 0; j < _xColumns; j++)
{
outputString.append("----");
}
outputString.append("-\n");
// Column numbers
for (int j = 0; j < _xColumns; j++)
{
outputString.append(String.format(" %2d ", j + 1));
}
outputString.append(" \n");
return outputString.toString();
}
}