CHAPTER 7 ■ WRITING A USER INTERFACE
int getMinesFound() {
return minesFound;
}
int getMinesRemaining() {
return minesRemaining;
}
private gameState cascade(int x, int y) {
if (x < 0 || y < 0 || x >= rows || y >= columns) {
return gameState.CONTINUE;
}
Mine thisMine = mineField[x][y];
if (thisMine.hasMine()) {
return gameState.CONTINUE;
}
if (!thisMine.isCleared()) {
thisMine.clear();
emptiesRemaining--;
if (emptiesRemaining == 0) {
return gameState.WIN;
}
}
if (countAdjacentMines(x, y) > 0) {
return gameState.CONTINUE;
} else{
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (i < 0 || j < 0 || i >= rows || j >= columns) {
continue;
} else if (!mineField[i][j].isCleared()) {
cascade(i, j);
}
}
}
}
return gameState.CONTINUE;
}
int countAdjacentMines(int x, int y) {
int count = 0;
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (i == x && j == y) {
continue;
} else if (i < 0 || j < 0 || i >= rows || j >= columns) {
continue;
} else if (mineField[i][j].hasMine()) {
count++;
}
}
}