Upcoming SlideShare
×

# Ch11 Search & Sort

586

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
586
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
8
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Ch11 Search & Sort

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

Clipping is a handy way to collect important slides you want to go back to later.