ch11 search & sort
Post on 22-Jan-2015
568 Views
Preview:
DESCRIPTION
TRANSCRIPT
Chapter 5
Black Jack
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-2
Chapter Objectives
• Provide a case study example from problem statement through implementation
• Demonstrate how a set can be used to solve a problem
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-3
Black Jack
• Black Jack is a card game typically involving multiple players and a dealer
• Each card in a hand is awarded points based upon its face value
• Face cards are worth 10 points each
• Numeric cards are worth their face value
• Aces are worth either 1 or 11 points
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-4
Black Jack
• The goal of the game is to be closer to 21 than the dealer without going over 21
• Black Jack is usually played with a shoe of cards (a collection of seven decks)
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-5
Black Jack
• For this case study, we define black jack as a one-player v.s. the dealer interactive card game
• We will also limit the game to a single deck of cards rather than a shoe
• The player begins the game by clicking a Deal button
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-6
Black Jack
• The player and the dealer are then dealt two cards
• The player can see their own cards and one card of the dealer
• The player then has the choice to hit (take another card) or stay (accept this hand as final for this game)
• If the player busts (goes over 21) then the game is over
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-7
Black Jack
• Once the player elects to stay, the dealer then chooses to hit or stay
• The dealer must hit on 16 or less and must stay otherwise
• An Ace is considered to be 1 point rather than 11 points if otherwise it would cause the player or dealer to bust
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-8
Black Jack - Initial Design
• Our black jack game is made up of the components of the game, the function of the game, and the user interface
• The components include cards, deck, and players hands
• The function includes controlling the order of play, whether a player hits or stays, and the value of a players hand
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-9
Black Jack - Initial Design
• In this case study, there is a clear distinction between the low-level components of the game and the game itself
• These low-level components (card, deck, hand) are also very well defined in terms of state and behavior
• Thus a bottom-up approach to this design problem makes sense
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-10
Black Jack - Initial Design
• Bottom-up simply means that we will design the low-level components first and then work our way up to the driver
• Other approaches include top-down and re-use based design
• Keep in mind, these are simply frameworks not rigid models
• Thus we will often find ourselves mixing these approaches even on the same system
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-11
Black Jack - the Card Class
• A Card object must represent: – the suit of the card (heart, diamond, club, or spade),
– the value of the card (1 to 11),
– the face of the card (ace, king, queen, six, two, etc.),
– the image of the card
• A Card object must also provide a constructor, operations to: – retrieve the suit, value, face, or image of the card,
– an operation to change the value of an ace from 11 to 1
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-12
Black Jack - the Deck Class
• A deck is an unordered collection of unique cards
• Thus a set is a perfect collection to represent a deck
• The Deck class must include – the collection of Cards,
– a method to retrieve a random card from the deck
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-13
Black Jack - the Deck Class
• We will also choose to instantiate a new deck at the beginning of each new game
• This eliminates the possibility of ever having an “empty” deck during a game
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-14
Black Jack - the Hand Class
• A hand is a collection of unique cards that have been dealt to a player
• A Hand object must keep track of: – the cards in the hand,
– the count of the cards in the hand,
– the value of the hand
• A Hand object must also provide methods to: – add a card to the hand,
– remove a card from the hand,
– return the value of the hand,
– provide a string representation of the hand
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-15
Black Jack - the Hand Class
• A Hand object must also provide a method to determine if an ace is in the hand and needs to be reduced in value from 11 to 1
• As with a Deck, a set seems a reasonable collection to represent a Hand since each of the cards in the Hand are unique and order does not matter
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-16
Black Jack - the BlackJack Class
• Separating the function of the game from the user interface, we will have a BlackJack class to control the game and a BlackJackGUI class to provide the interface
• The BlackJack class must represent – the hands of both players
– the deck
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-17
Black Jack - the BlackJack Class
• This class must provide methods to: • deal the initial cards, • hit a particular player, • return the value of a player’s hand, • determine is a player has busted,• determine the winner of the game
• The BlackJack class will use the Deck class to store the deck and the Hand class to represent each player
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-18
Black Jack - the BlackJackGUI Class
• The BlackJackGUI class will provide the user interface for our game
• This class will provide: • a deal button, • buttons for the player to hit or stay, • a display of each player’s hand• a display of the winner of the game
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-19
FIGURE 5.2 User interface design for BlackJack
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-20
BlackJack - the BlackJackDemo Class
• The BlackJackDemo class will serve as the driver for our system
• This class simply creates an instance of BlackJackGUI and calls its display method
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-21
FIGURE 5.1 Blackjack class diagram
top related