200 likes | 298 Views
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
E N D
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 • 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
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
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
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
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
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
Design – Key Nouns • Here is a first approximation of the classes needed (re-ordered a bit): • Card • Deck • Player • Dealer • Game • Hand
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()
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
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
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
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
The Hand Class • Instance data • ArrayList<Card> cardsheld • boolean broke • Methods • void addCard(Card c) • int handValue()
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
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
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
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”
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