End and Start a New Game
Learn how to ask the user to play again after the game is over.
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.
- In
released()
, after addingmovingCard
to a foundation that already had one or more cards, callisGameOver()
.
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.
- Add a new private method called
isGameOver()
. It should take no parameters and return nothing. - Create a boolean called
gameOver
, initialized totrue
. - Loop through as many as 4 foundation stacks, only while
gameOver
istrue
.- If the size of that foundation is not
13
, setgameOver
tofalse
using CardStack’ssize()
method.
- If the size of that foundation is not
...
_______________ isGameOver(_________) {
__________ gameOver = ___________;
for(_______; _____ && ______; ______) {
if (______________) {
gameOver = __________;
}
}
}
...
Get hands-on with 1200+ tech skills courses.