|
CMPT 120: Assignment 3
Blackjack
For the third assignment you will write a program that allows the user to play the card game Blackjack vs the computer controlled dealer.
Here's an example run.
and another.
The basic goal of Blackjack is to get a hand worth 21 or as close to 21 as possible without going over. A round of Blackjack works as follows.
- The hands are dealt. The player and dealer each get two cards. The first and third cards go to the player, the second and fourth go to the dealer.
- The player can see both of their cards, but only one of the dealer's cards
- The player can take a card (Hit) as many times as needed. However, if the player's hand goes over 21, this is called a Bust and he automatically loses a point
- If the player didn't bust, the dealer's hand is played. The dealer follows a strict rule: If the hand is worth 16 points or less, the dealer must keep hitting. If the dealer busts, the player wins a point.
- If neither bust, the player wins if their hand is worth more than the dealer's, loses if worth less, and ties (pushes) otherwise
After each round the player is asked if they want to continue. If before a round there are less than 52 cards remaining in the deck, a new deck must be shuffled for the next round.
cards module
To write this program you will need to use the cards module. Go here for information and to download it. Save the file to the same directory as your program and have import cards as the first line of your program. Read the documentation to understand the module's various functions.
User Input
For all of the user input the user will be asked to enter single letters, (Y/N) or (H/S). In all cases the user should be allowed to enter either the upper or lower case letter. If the user enters something else, just repeat the prompt until they give a correct input.
Your hand: 7C 9C
Would you like to (H)it or (S)tand?
Would you like to (H)it or (S)tand? asd
Would you like to (H)it or (S)tand? hil
Would you like to (H)it or (S)tand? H
You drew the queen of spades
Functions
Once again, using functions is going to be an important part of this program. Below are definitions for some functions that you need to write and use in your program. If you don't follow the definitions below, you will lose points even if your program works correctly.
shuffledDeck()
shuffledDeck will take no arguments and return a deck that will be used to deal the cards. This deck will contain two full decks of cards shuffled together. Therefore it will contain 104 cards. The order of the cards needs to be copmletely randomized. You will need to use the random module in order to accomplish this. The function should print the message 'Shuffling' so the player knows this is occuring You may not use the shuffle method in the random module, just the functions that give you random numbers.
handValue(hand)
Write a function named handValuethat takes one argument as input. This argument will be a hand (list) of cards. It will return the value of the hand by summing the value of each of the cards. For those who may be unfamiliar, the cards' values are as follows:
- Ace: 1 or 11
- 2 - 10: Face value, 2 thru 10 respectively
- Face Cards (J,Q,K): 10
If the hand does not contain an ace, return the sum. If the hand contains an ace, you will get two values. If the higher value is less than or equal to 21, return it, otherwise return the lower value. For example, a hand of an ace and a ten should return 21, but a hand holding an ace, a five, and a ten should return 16.
playRound(deck)
The playRound function takes one input, the deck of cards that haven't been played. The function will play one round between the dealer and the player and will return the change in the player's score that resulted from the round as an integer, 0, 1, or -1. Below are examples of the output from four calls to playRound. The player busts in the first, pushes in the second, wins the third, and loses the fourth. Note where the long and short names of the cards are used; your program should match this exactly.
The dealer has a eight of diamonds showing.
Your hand: 9H 7C
Would you like to (H)it or (S)tand? h
You drew the jack of hearts
BUST Your hand has a value of 26
You lose a point.
The dealer has a three of hearts showing.
Your hand: 8H QH
Would you like to (H)it or (S)tand? s
Your hand has a value of 18
The dealer has: 3H 6S
The dealer drew the nine of hearts
The dealer's hand has a value of 18
Push
The dealer has a eight of hearts showing.
Your hand: 5D 3S
Would you like to (H)it or (S)tand? h
You drew the five of spades
Your hand: 5D 3S 5S
Would you like to (H)it or (S)tand? h
You drew the six of hearts
Your hand: 5D 3S 5S 6H
Would you like to (H)it or (S)tand? s
Your hand has a value of 19
The dealer has: 8H 9C
The dealer's hand has a value of 17
You win a point.
The dealer has a ace of hearts showing.
Your hand: 7S KH
Would you like to (H)it or (S)tand? s
Your hand has a value of 17
The dealer has: AH 8C
The dealer's hand has a value of 19
You lose a point.
Along with these functions, you must define and use at least 2 other functions in your program. You will lose points if the functions aren't useful.
Notes
Do not try to write the entire program at once. Write a function at a time and once you have each function working, move on.
Coding style will be an important part of the grade. Functions should have docstrings, there should be a helpful comments when it seems appropriate (NOT everywhere), use good variable names, use proper spacing, and any other style issues mentioned in class.
Chris Schmidt, last updated June 12th, 2007 |