-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAvisModel.java
More file actions
262 lines (238 loc) · 5.96 KB
/
AvisModel.java
File metadata and controls
262 lines (238 loc) · 5.96 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
//@author Avi Gupta
import java.util.Random;
import java.util.ArrayList;
public class AvisModel implements MineModel {
private int numRows;
private int numCols;
private int numMines;
private int numFlags;
private long initialTime;
private boolean gameStarted;
private boolean gameOver;
private boolean isPlayerDead;
private boolean isGameWon;
private AvisCell[][] cells; //indexed by row, col
private boolean firstClick;
public AvisModel() {
gameStarted = false;
}
@Override
public void newGame(int numRows, int numCols, int numMines) {
this.numRows = numRows;
this.numCols = numCols;
this.numMines = numMines;
numFlags = 0;
gameOver = false;
isPlayerDead = false;
isGameWon = false;
firstClick = true;
//initializing cell 2D array
cells = new AvisCell [numRows][numCols];
for (int row = 0; row<numRows; row++ ) {
for (int col = 0; col<numCols; col++ ) {
cells[row][col] = new AvisCell(row,col);
}
}
gameStarted = true;
initialTime = System.currentTimeMillis();
}
//this places all the mines on the board
//subject to the constraint that they can't be next to or on the first click
public void minePlace() {
Random randgen = new Random();
for (int minesPlaced = 0; minesPlaced < numMines; minesPlaced ++) {
int mineRow, mineCol;
boolean neighborvisible;
AvisCell active;
do {
mineRow = randgen.nextInt(numRows);
mineCol = randgen.nextInt(numCols);
neighborvisible = false;
active = cells[mineRow][mineCol];
ArrayList <AvisCell> neighbors = getAdjacent(mineRow, mineCol);
for (AvisCell neighbor: neighbors) {
if (neighbor.isVisible()) {
neighborvisible = true;
}
}
} while (active.isMine() || active.isVisible() || neighborvisible);
cells[mineRow][mineCol].makeMine();
ArrayList <AvisCell> neighbors = getAdjacent(mineRow, mineCol);
for (AvisCell a: neighbors) {
a.incrementNeighborMines();
}
}
}
@Override
public int getNumRows() {
return numRows;
}
@Override
public int getNumCols() {
return numCols;
}
@Override
public int getNumMines() {
return numMines;
}
@Override
public int getNumFlags() {
return numFlags;
}
@Override
public int getElapsedSeconds() {
long currentTime = System.currentTimeMillis();
int timeElapsed = (int) ((currentTime - initialTime)/1000);
return timeElapsed;
}
@Override
public Cell getCell(int row, int col) {
return cells[row][col];
}
//handles flooding
public void flood(int row, int col) {
AvisCell activeCell = cells[row][col];
ArrayList <AvisCell> neighbors = getAdjacent(row,col);
if (activeCell.getNeighborMines()==0) {
for (AvisCell a: neighbors) {
if (!a.isVisible() && !a.isMine()) {
stepOnCell(a.getRow(), a.getCol());
}
}
}
}
@Override
public void stepOnCell(int row, int col) {
AvisCell activeCell = cells[row][col];
if (activeCell.isMine()) {
isPlayerDead = true;
exposeMines(); //exposes the mines when you lose
gameOver = true;
}
if (firstClick) {
activeCell.step();
minePlace(); //places mines AFTER the first click is done so we can make
//sure the first click is flooded
firstClick = false;
}
else {
activeCell.step();
}
if (activeCell.getNeighborMines()==0 && !activeCell.isMine()) {
flood(row, col);
}
}
@Override
public void placeOrRemoveFlagOnCell(int row, int col) {
AvisCell activeCell = cells[row][col];
activeCell.changeFlag();
if (activeCell.isFlagged()==true) {
numFlags++;
}
else if (activeCell.isFlagged()==false) {
numFlags--;
}
}
@Override
public boolean isGameStarted() {
return gameStarted;
}
@Override
public boolean isGameOver() {
if (isGameWon || isPlayerDead ) {
gameOver = true;
}
return gameOver;
}
@Override
public boolean isPlayerDead() {
return isPlayerDead;
}
//current win condition: all cells that aren't mines visible
@Override
public boolean isGameWon() {
isGameWon = true;
for (int row = 0; row <numRows; row++ ) {
for (int col = 0; col < numCols; col++) {
if (cells[row][col].isVisible() ==false &&
cells[row][col].isMine()==false) {
isGameWon = false;
break;
}
}
}
if (isGameWon) {
for (AvisCell [] row: cells) {
for (AvisCell a: row) {
if (a.isMine()){
a.makeVisible();
}
}
}
}
return isGameWon;
}
//exposes all the mines at the end of the game
public void exposeMines() {
if (isPlayerDead) {
for (AvisCell[] cellRow: cells) {
for (AvisCell cell: cellRow) {
if (cell.isMine()) {
cell.makeVisible();
}
}
}
}
}
//tried to write a helper, never finished
/**public void helper(int row, int col) {
int flagCounter = 0;
AvisCell activeCell = cells[row][col];
ArrayList <AvisCell> neighbors = getAdjacent(row,col);
for (AvisCell a: neighbors) {
if (a.isFlagged()) {
flagCounter ++;
}
}
if (activeCell.getNeighborMines() == flagCounter) {
for (AvisCell b: neighbors) {
if (!b.isVisible() && !b.isFlagged()) {
stepOnCell(b.getRow(), b.getCol());
}
}
}
}**/
//returns arraylist of all adjacent cells to simplify code
public ArrayList<AvisCell> getAdjacent(int row, int col) {
boolean rowsUpper = (row + 1 < numRows);
boolean rowsLower = (row - 1 >= 0);
boolean colsUpper = (col + 1 < numCols);
boolean colsLower = (col - 1 >= 0);
ArrayList<AvisCell> neighbors = new ArrayList<AvisCell>();
if (rowsUpper) {
neighbors.add(cells[row+1][col]);
if (colsUpper) {
neighbors.add(cells[row+1][col+1]);
}
if (colsLower) {
neighbors.add(cells[row+1][col-1]);
}
}
if (rowsLower) {
neighbors.add(cells[row-1][col]);
if (colsUpper) {
neighbors.add(cells[row-1][col+1]);
}
if (colsLower) {
neighbors.add(cells[row-1][col-1]);
}
}
if (colsUpper) {
neighbors.add(cells[row][col+1]);
}
if (colsLower) {
neighbors.add(cells[row][col-1]);
}
return neighbors;
}
}