10.1.2.  Exercise: CardGame GUI

[ fromfile: cardgameui1.xml id: cardgameui1 ]

Write a blackjack game, with the following actions:

These actions should be available via menubar as well as toolbar, in a main window application like Figure 10.3. The rules of the game are explained next.

When the game starts, the user and the dealer are each dealt a "hand" of cards. Each hand initially consists of two cards. The user plays her hand first by deciding to add cards to her hand with the Hit Me action zero or more times. Each Hit adds one card to her hand. The user signals that she wants no more cards with the Stay action.

Figure 10.3.  Blackjack Screenshot

Blackjack Screenshot

The object of the game is to achieve the highest point total that is not greater than 21.

For the purposes of evaluation of a hand, a "face card" (Jack Queen, and King) counts as 10 points, an Ace can count as 1 or 11 points, whichever is better. Each other card has a number and a point value equal to that number. If the hand consists of an Ace plus a Jack, then it is better to count the Ace as 11 so that the total score is 21. But if the hand consists of an 8 plus a 7, and an Ace is added to the hand, it is better to count the Ace as 1.

If a player gets a point total greater than 21, that player is "busted" (loses) and the hand ends.

If a player gets five cards in her hand with a total that is not greater than 21, then that player wins the hand.

After the user either wins, loses, or Stays, the dealer can take as many hits as necessary to obtain a point total greater than 18. When that state is reached the dealer must Stay and the hand ends. The player whose score is closer to, but not greater than, 21 wins. If the two scores are equal, the dealer wins.

When the hand is over, the user can select only Deal Hand, New Game or Quit. (i.e. Hit Me and Stay are disabled).

After the user selects Deal Hand, that choice should be disabled until the hand is finished.

Keep track of the number of games won by the dealer and by the user, starting with zero for each player, by adding one point to the total for the winner of each hand. Display these totals above the cards.

Deal more cards without resetting the deck until the deck becomes empty or the user chooses Shuffle Deck.

Try to reuse or extend the CardDeck and related classes that you developed earlier in Section 6.8.1. Add a graphical representation to your game by showing an image for each card, perhaps as shown in Section 9.5 and Section 9.6.

Provide a pull-down menu and a toolbar for each of the QActions. Make sure that Hit and Stay are enabled only after the game has started.

Show how many cards are left in the deck in a read-only QSpinBox at the top of the window.

A new game should zero the games won totals and reset the deck.

Design Suggestions

Try to keep the model classes separate from the view classes, rather than adding GUI code to the model classes. Keeping a strong separation between model and view is good programming style and makes your project easier to manage from start to finish.

Figure 10.4 shows one possible design for the solution.

Figure 10.4.  BlackJack UML Diagram

BlackJack UML Diagram