This site applies only to CMPT 120 D1 (Burnaby) in Summer 2011. See the other instructors' pages for other sections.
Suggested Plan
This is a suggested list of steps that you can follow to get your program working. Remember to test your code frequently, at least after each of these steps.
The hints don't say much about breaking your code into functions. Deciding how to do that is part of the assignment.
-
Import
cards
. Create a deck of cards usingcards.deck()
. Usecards.hand_display()
to print out the contents of the deck to check it. Add code to shuffle and test again. - Get the number of players in the game.
- Create a loop for the turns (you want to loop three times). In it, call a function that plays a full round (or will eventually, at least).
-
For each round, create an empty list for the hands. For each player, create their hand (a list of card objects) and append it to the list of hands. Use
hand_display
to print out the contents of each hand to test. Do the same for the dealer. (Don't worry about hiding the dealer's hole card yet.) - Create a function to count to total number of points in a hand (list of cards). Use it in the loop to calculate the total for each initial hand.
- Add code to let a player hit or stand. Check for the player busting. Re-test your point totalling function to make sure it handles hands with aces that should count as 1 properly. (See hints below.)
- Add code to play the dealer's hand: keep hitting until the total is ≥ 17.
- Add code to determine and print the result for each player (win/loss/push). You will add the betting/money code later: for now, just indicate the result of the hand.
-
Create your own version of
hand_display
that displays**
instead of the first card. Use that instead ofhand_display
when initially displaying the dealer's hand. This will take card of hiding the hole card. - Work back through you program and add code for the player's banks (starting at $1000 each). This should probably be a list of integers: one for each player.
- Add code that asks for each player's bet before each turn.
- In your win/loss/push code, add logic to update each player's bank.
- Make sure you have done the error checking on all of the input, as seen in the sample runs.
Functions and Pseudocode
The following are outlines of logic for this assignment.
Blackjack Game
Code corresponding to this logic would likely form the main part of your program. It should be broken up into appropriate functions, but at a high level, your program should do this:
- Create and shuffle the deck.
- Ask how many players there are and put $1000 in each player's “bank”.
- For each round you're going to play:
- Ask each player for their bet.
- Give two cards to each player and the dealer (display all but the dealer's hole card).
- Let each player take their turn:
- Ask the player whether they want to hit or stand, until they either stand or bust.
- Each card the player is given should be added to their hand, and their new total calculated appropriately.
- Play the dealer's hand. (Hit on 16, stand on 17.)
- Settle the bets: give/take the bet for each player.
- Game over.
Counting a Hand's Value
This is the general idea for determining the number of points in a hand:
- For each card in the hand:
- Add the card's value to the total.
- Count aces as 11, but keep track of the number of aces you've seen.
- If the total is over 21, but there were aces in the hand, subtract 10. Repeat as necssary.
Notes
-
The
random
module has a functionshuffle
that will shuffle the deck for you. -
Lists have a
pop()
method that removes an element from the end of the list and returns it. This would be handy for removing a card from the deck and adding it to a player's hand.c = deck.pop()
hand.append(c)