End and Start the New Game
Learn how to check if the clicked terrain button has a hole and ask the user for a new game.
End the game when a hole is clicked
Next, end the game if the user clicks on a terrain button that has a hole.
- Add code to the beginning of
clickedTerrain()
to check whether the clicked terrain has a hole. Check the terrain atrow
andcol
using TerrainButton’shasHole()
method. - If the terrain has a hole, create a string called
message
, stating that the user stepped in a hole and the game is over. Then, ask the user if they want to play again. - Call
promptForNewGame()
, passing themessage
as a parameter value. - Put the rest of the code in
promptForNewGame()
in anelse
block. It should only check the chosen terrain button and that button’s neighbors, if the terrain does not have a hole.
Note: Don’t click on the “Run” button just after adding the above changes. The code will have a syntax error because promptForNewGame()
doesn’t exist yet.
...
private void clickedTerrain(int row, int col) {
if (_______________________) {
_________ message = __________________;
________(message);
}
else {
check(row, col);
checkNeighbors(row, col);
}
}
...
Get hands-on with 1200+ tech skills courses.