Java 7 for Absolute Beginners

(nextflipdebug5) #1
CHAPTER 7 ■ WRITING A USER INTERFACE

Mine.flagState.MINE) {
return gameState.CONTINUE;
}
if (thisMine.isCleared())
return gameState.CONTINUE;
if (thisMine.hasMine()) {
return gameState.LOSE;
} else {
return cascade(i, j);
}
} else {
Mine.flagState state =
thisMine.setFlagState();
if(state == Mine.flagState.MINE) {
minesFound++;
minesRemaining--;
} else if(state == Mine.flagState.SUSPECT) {
minesFound--;
minesRemaining++;
}
}
}
}
}
return gameState.CONTINUE;
}


private void populate() {
populate(0);
}


private void populate(int mineCount) {
int currentCount = mineCount;
double mineChance = (double) mines / (double) (rows * columns);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
Mine thisMine = mineField[i][j];
if (!thisMine.hasMine()) {
if (Math.random() < mineChance) {
thisMine.setMine();
currentCount++;
if (currentCount == mines) {
return;
}
}
}
}
}
if (currentCount < mines) {
populate(currentCount);
}
}

Free download pdf