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.

  1. Add code to the beginning of clickedTerrain() to check whether the clicked terrain has a hole. Check the terrain at row and col using TerrainButton’s hasHole() method.
  2. 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.
  3. Call promptForNewGame(), passing the message as a parameter value.
  4. Put the rest of the code inpromptForNewGame() in an else 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.