End and Start a New Game

Is the game over?

Next, check whether the game is over every time the user plays a card on a foundation stack that has more than one card.

  1. In released(), after adding movingCard to a foundation that already had one or more cards, call isGameOver().

Note: Don’t execute after following the above steps. The code will have a syntax error because isGameOver() doesn’t exist yet.

    ...
 
  private void released(int x, int y) {
 
        ...
 
      // otherwise, valid suit and rank?
      else {
        Card topCard = foundation[i].getLast();
        if (movingCard.getSuit()==topCard.getSuit()
            && movingCard.getValue()==topCard.getValue()+1) {
          validMove = true;
          foundation[i].add(movingCard);
          movingCard = null;
          isGameOver();
        }
      }
        ...

The game is over when each of the four foundation stacks contains thirteen cards.

  1. Add a new private method called isGameOver(). It should take no parameters and return nothing.
  2. Create a boolean called gameOver, initialized to true.
  3. Loop through as many as 4 foundation stacks, only while gameOver is true.
    • If the size of that foundation is not 13, set gameOver to false using CardStack’s size() method.
    ...
 
  _______________ isGameOver(_________) {
    __________ gameOver = ___________;
    for(_______; _____ && ______; ______) {
      if (______________) {
        gameOver = __________;
      }
    }
  }
    ...

Get hands-on with 1200+ tech skills courses.