1 / 20

Design Example

Design Example. Requirements. Make a program that simulates the game of blackjack For now, we ignore money/betting…. just simulate game play But… this requires a description of the rules of play. Requirements. Blackjack is played with a deck of cards

erich-rice
Download Presentation

Design Example

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design Example

  2. Requirements • Make a program that simulates the game of blackjack • For now, we ignore money/betting…. just simulate game play • But… this requires a description of the rules of play

  3. Requirements • Blackjack is played with a deck of cards • There is a player and a dealer, and they each hold a “hand” consisting of some cards • The value of a hand is the total of the card values • Faces count as 10 • Aces count as 1 or 11 • The goal is to achieve the closest hand value to 21 – without going over

  4. Requirements • Game starts by giving the player 2 cards and giving the dealer 1 card • The player can choose to take another card • If the new hand value exceeds 21 then the game is over and the dealer wins • If the new hand value is less than 21, then repeat • When the player stops taking cards, it is the dealer’s turn • The dealer takes cards until his/her hand value exceeds 16, then the game is over • If the dealer exceeds 21, the player wins • If the player and dealer are both below 21, then the winner is the hand that is closest to 21

  5. Requirements • Play the game at the command line • Running the program gives initial hands, then prompts the player for a move • When the player loses or stops taking cards, then the dealer draws cards until the game is over • At the end of a successful run of the program, we should be told if it was a player win, a dealer win, or a tie

  6. Design – Find the Nouns • The game is played with a deck of cards • There is a player and a dealer, and they each hold a “hand” consisting of some cards • The value of a hand is the total of the card values • Faces count as 10 • Aces count as 1 or 11 • The goal is to achieve the closest hand value to 21 – without going over

  7. Design – Find the Nouns • Game starts by giving the player 2 cards and giving the dealer 1 card • The player can choose to take another card • If the new hand value exceeds 21 then the game is over and the dealer wins • If the new hand value is less than 21, then repeat • When the player stops taking cards, it is the dealer’s turn • The dealer takes cards until his/her hand value exceeds 16, then the game is over • If the dealer exceeds 21, the player wins • If the player and dealer are both below 21, then the winner is the hand that is closest to 21

  8. Design – Find the Nouns • A lot of repeat occurrences • Here are the nouns (abridged): • game • deck • card • player • dealer • hand • total • value • face • ace • winner Same thing… looks like an integer would be ok. Cards with particular values… not a new type. Refers to either the player or dealer

  9. Design – Key Nouns • Here is a first approximation of the classes needed (re-ordered a bit): • Card • Deck • Player • Dealer • Game • Hand

  10. The Card Class • Instance data: • char rank (admissible values 2-10, J,Q,K,A) • char suit (admissible values h,s,c,d) • To make the class re-usable for other games, we don’t want to include blackjack specific information • e.g. getValue() • We would like • String toString()

  11. The Deck Class • Basically a wrapper for an array of cards • Important instance data: • Card[] cards • int currentposition • Important method: • Card getcard(); • void shuffle(); • Constructor can initialize the 52 card deck, and optionally shuffle

  12. The Player Class • The Player has an ArrayList of cards: • ArrayList<Card> cardsheld • Useful methods: • void addCard(Card c) • void stand() • int handValue() • The player can add cards or stop adding cards, and check the value of his/her hand

  13. The Dealer Class • The Dealer also has an ArrayList of cards: • ArrayList<Card> cardsheld • Useful methods: • void playHand() (adds cards until 16) • int handValue() • The dealer always plays the same

  14. Note: Duplicate Methods/Logic • Both the player and the dealer have an ArrayList of cards, with methods for adding cards and getting hand value • This suggests at a better representation… the collection of cards should be a separate object

  15. The Hand Class • Instance data • ArrayList<Card> cardsheld • boolean broke • Methods • void addCard(Card c) • int handValue()

  16. Player and Dealer Revisited • Now… the player and dealer each have instance data: • Hand currenthand • The addCard() method is part of Hand, so the distinction between Player and Dealer is now the methods for playing the hand • This seems right

  17. The Game Class • A Game object should do the following: • Construct a Deck for playing the game • Define a method play(Dealer d, Player p) • Take user input for player’s hand • Output a winner and loser • Keep track of games played • Keep track of game “traces” • The Game class can also define a main method

  18. Class Summary • Game object • contains Deck object • contains Card objects • Dealer object • contains Hand object • Player object • contains (another) Hand object • Game.play(d,p); • contains game logic, returns outcome

  19. Game.play(d,p) Pseudocode Game.play(dealer,player) shuffle(); player.addCard(); player.addCard(); dealer.addCard(); player.playHand(); \\ requires user input if(player.getValue()>21) return “dealer wins” dealer.playHand(); if(player.getValue()<dealer.getValue<21) return “dealer wins” return “player wins”

  20. What Next? • Implementation! • Note that everything is pretty straightforward now… but there are a couple of choices to make: • How to get user input • What should the main method do? • But the hard part is basically done – and we haven’t done any Java yet

More Related