This site applies only to CMPT 120 D1 (Burnaby) in Summer 2011. See the other instructors' pages for other sections.
For this assignment, you will have to use the Python Module Reference (SFU mirror of the Python Module Reference). As part of this course, you are expected to be able to use documentation such as this.
Part I: Warmup
-
In a file
guess.py
, implement the guessing game described in question 4 of assignment 1. Use a function from therandom
module to generate the random number. -
Name your program for this part
lists1.py
.Write a function that takes one floating point number x as its argument and returns . The
math
module has a square root function.Using a loop, read 5 floating point values from the user into a list. Apply the function you wrote to each value in the list and print the results in reverse order.
Enter 5 values:
2
3
4
5
6
1082.44948974 627.236067977 322.0 136.732050808 41.4142135624Enter 5 values:
1.2
-2
10
0
2.41
71.5400224696 0.0 5003.16227766 -38.5857864376 9.73544511501There is a built-in function to reverse a list. You may not use it in this question.
-
Copy your program from the previous part into
lists2.py
.Lists in Python are objects: they have methods that can be used to operate on the list. In particular, they have a
reverse
method that reverses the order of the list. Modify the program so it uses thereverse
method to reverse the list before being printed. The output should be identical to the previous question.
Part II: Programming
For this assignment, your job is to create a program that plays (a simplified version of) blackjack. Name your program blackjack.py
.
The Rules
For simplicity, we won't be implementing the full set of blackjack rules, but will get the core of the game working:
-
A regular deck of 52 cards is used. The cards are every combination of 13 “ranks” (two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace) and 4 “suits” (spades, clubs, diamonds, hearts). The deck is shuffled (put into a random order) before the game begins.
-
In our game, each player starts with $1000. At the start of the game, ask how many people wish to play.
-
Before each turn, each player makes a bet. They can risk any amount of their money on each turn.
-
Two cards are dealt (given) to each player and placed face-up on the table. The dealer also receives two cards, but one is kept face-down (the “hole card”). The rest of the cards form the “shoe”.
-
The object of the game is to get cards that have value as close as possible to 21 without going over (“busting”). The cards in a players hand have point values as follows.
Card values in blackjack Card Rank Value 2–10 the card's face value (2–10) J, Q, K 10 A 1 or 11, whichever is better -
Each player gets a turn to take additional cards from the shoe. A player can take another card (“hit”) as many times as they like. When they stop taking new cards (“stand”), their turn is over. The cards in the player's hand will not change after their turn is over.
-
Once the players have taken their turns, the dealer reveals his/her hole card. The dealer also has the chance to hit and stand, but they have a more rigid rule to follow. The dealer must hit (take another card) if their hand contains 16 points or less, and stand (stop taking cards) if they have 17 points or more.
-
Once all of the cards have been played, each player either wins or loses depending on what is in their hand and the dealer's hand:
Blackjack hand outcomes Player's outcomes Busts (> 21 points) Doesn't bust (≤ 21 points) Dealer's
outcomesBusts player loses player wins points < player player loses player wins points = player player loses push points > player player loses player loses If the player wins, they get their bet back plus an equal amount from the dealer. If they lose, they lose the amount of money they bet.
A “push” is a tie. In that case, the player takes his/her money back and doesn't win or lose anything. [In true blackjack, there is an additional set of outcomes that occur when either the player or dealer has “blackjack”: 21 points with two cards. We won't worry about those rules here.]
-
After the turn is played and the bets are settled, they play continues with another round (go back to step 3).
-
In a true game of blackjack, players can join or leave the game between hands. We won't worry about that. For this game, all of the players will play three hands, and then the game will stop.
Implementing
To help you along, a cards
module has been provided. You'll have to download the cards.pyc
file. Save this file in the same directory as your blackjack.py
file and you should be able to import cards
.
You must use the Card
object to represent the cards in the deck and players' hands. The deck and hands should be lists of Card
objects. There are several functions in the cards
module that will help you complete the assignment; they are described in the module documentation.
Several hints have been provided to help you find your way through this assignment. The hints don't say much about breaking your code into functions: deciding how to do that is part of the assignment.
Output
Here is a short sample of what the program should look like when it runs.
How many players are there? 2 Player #1: You have $1000. How much do you want to bet? 100 Player #2: You have $1000. How much do you want to bet? 200 Hands: Dealer: ** 4H Player #1: 2D JH Player #2: 9D TS Player #1 Cards: 2D JH, total: 12 Hit (h) or stand (s)? h Cards: 2D JH 8H, total: 20 Hit (h) or stand (s)? s Player #2 Cards: 9D TS, total: 19 Hit (h) or stand (s)? s Dealer draws 4S. Dealer's hand: QC 4H 4S, total: 18 Player #1 wins $100. Player #2 wins $200. Press enter to continue. Player #1: You have $1100. How much do you want to bet? 100 Player #2: You have $1200. How much do you want to bet? 200 Hands: Dealer: ** 8D Player #1: JC 6C Player #2: 6H KC Player #1 Cards: JC 6C, total: 16 Hit (h) or stand (s)? s Player #2 Cards: 6H KC, total: 16 Hit (h) or stand (s)? h Cards: 6H KC KD, total: 26 Bust! Dealer draws 5C. Dealer's hand: 4D 8D 5C, total: 17 Player #1 loses $100. Player #2 loses $200. Press enter to continue. Player #1: You have $1000. How much do you want to bet? 500 Player #2: You have $1000. How much do you want to bet? 200 Hands: Dealer: ** 6S Player #1: 7S JS Player #2: 5D 5S Player #1 Cards: 7S JS, total: 17 Hit (h) or stand (s)? s Player #2 Cards: 5D 5S, total: 10 Hit (h) or stand (s)? h Cards: 5D 5S 9H, total: 19 Hit (h) or stand (s)? s Dealer draws QH. Dealer's hand: 3C 6S QH, total: 19 Player #1 loses $500. Player #2 pushes. Press enter to continue. Player 1: $500 Player 2: $1000
There is another page of sample runs that you can look at as well.
When you play the game, it should look like the given examples (i.e. all the messages, prompts, etc. should be the same).
Notes
Some things to keep in mind:
- Remember that every time a player takes a card from the deck, it is removed from the deck.
- An ace can count for either 11 or 1 in a hand. When calculating the value of a hand, make sure you use the best one.
-
The user should be able to enter their choice in lower- or uppercase. So, entering
s
andS
should do the same thing. Hint: convert their input to uppercase immediately with the string'supper
method. - You won't want to play the full game all of the time while testing. Press control-C to stop the program.
Break up the program into subtasks placed into functions as appropriate. You should do that whenever you think it's useful.
Your code should be easy to read and understand. Style will be marked more closely on this assignment.
When you're done, submit your work in the Course Management System.