|
CPSC 115L: Introduction to Computing
|
Fall2010
|
Project 2: Blackjack Card game
CPSC 115-01: due Monday, November 22
CPSC 115-02: due Tuesday, November 23
For this project, you are encouraged to work with a partner,
although you may work on your own if you prefer. If submitted as a
pair, both you and your partner will receive the same grade.
Objectives
The main objectives of this assignment are
- to give you practice designing and writing a Python program of moderate complexity, and
- to give you practice using object-oriented programming concepts.
Introduction
For this assignment we will write a program that plays the game of
blackjack (or 21) (see
here for instructions) with a single deck of cards. The user of
the program will play against the program, which will play the role of
the dealer.
For each hand, the player and the dealer will each be dealt two cards.
The player's objective is to beat the dealer. If the player's cards total
more than the dealer's without exceeding 21, the player wins. The player
plays first. If the player's cards exceed 21, the player busts and loses.
The dealer plays after the player is finished. If the dealer busts and the
player does not, the player wins. If neither the player nor the dealer bust,
the winner is the one who has the highest total without exceeding 21.
In blackjack, the cards' suit have no bearing, just their rank. Cards
2-10 are counted at their face value (their rank). All face cards (Jack,
Queen, King) are valued at 10. Aces can count for either 1 or 11. So
an Ace and a face card would total 21--that's a blackjack.
In blackjack, the player can choose to hit or stay
for each additional card beyond the first two that were dealt.
The dealer, however, must take hit whenever the total is below
17 and must stay whenever the total is 17 or above.
Program Specifications
The following are the minimum requirements for this
assignment. You should free to include additional features besides
these -- e.g., keeping score -- to make your game more enjoyable.
- Your Python program should play a single hand of blackjack against the dealer. (Option:
Your program could loop through multiple hands until the user decides to stop.)
- Your program should be written in a file named blackjack.py.
- Your program should import and use
the cards.py module, which include
the Card, Deck, and Hand
classes. Download cards.py and save it in the
same directory as your blackjack.py file. Open and read the
definitions of the Card, Deck, and Hand
classes. You will need to use them in your program.
- You may not modify the Card, Deck, or
Hand classes in any way.
- To import the Card, Deck, and Hand
classes, place the following statement at the beginning of your
blackjack.py program:
from cards import Card, Deck, Hand
- Define the following classes and methods in blackjack.py:
- Class BlackjackHand. This class is a subclass of Hand. Like its
parent class, Hand, this class will have two attributes, a Python
list named cards and a string named label. The label
can be used to store the player's or dealer's name. The cards list will
store the individual Cards that make up the player's hand.
- The __init__(self, label) method should initialize both the cards list and
the label. It should have one parameter (besides self) for the
label.
- The __cmp__(self, other) method should be used to compare two blackjack
hands. It should return a positive number if self's total is greater than other's,
a negative number if it is less than other's and zero if they are equal.
- The getLabel() method should return the hand's label -- e.g., the player's name.
It takes no parameters.
- The total(self) method should return the total of that blackjack hand. Cards
2-10 are valued at their face value. Face cards are valued at 10 and Aces are valued at
1 or 11. Note that the value of an Ace may change as the hand proceeds. An Ace
shouldn't be valued at 11 if that would cause the hand to bust.
- The isaBust(self) is a boolean method that returns True if the hand exceeds
21 and False if not.
- The isaBlackjack(self) is a boolean method that returns True if the hand equals
21 and False if not.
Note that your classes and methods should have exactly the specifications described
here, including name.
Implementation
Implement your blackjack game in a Python script named blackjack.py.
Save snapshots of several runs that demonstrate the following points:
- The program correctly plays a hand of blackjack, including the dealer's hand.
- The program correctly totals the player's and dealer's hands.
- The program correctly identifies a blackjack.
- The program correctly identifies when the player or dealer busts.
- The program correctly determines the winner of the hand.
Sample Output
You are free to design your own user interface. But here are a couple of sample runs
of the script:
Dealer hand: ( 12 ): 2 of Clubs, Jack of Hearts
Ralph hand: ( 14 ): 9 of Diamonds, 5 of Diamonds
Ralph, type 1 for a hit and 0 to stay > 1
Ralph hand: ( 18 ): 9 of Diamonds, 5 of Diamonds, 4 of Hearts
Ralph, type 1 for a hit and 0 to stay > 0
Ralph total is 18 and Dealer total is 12 .
Dealer's turn. Type any key to continue >
Dealer takes a hit
Dealer hand: ( 22 ): 2 of Clubs, Jack of Hearts, 10 of Diamonds
That is a bust
Game over. Ralph has 18 Dealer has 22 .
Dealer busts and pays.
Dealer hand: ( 21 ): King of Diamonds, Ace of Spades
That's blackjack!
Ralph hand: ( 15 ): 8 of Spades, 7 of Clubs
Ralph, type 1 for a hit and 0 to stay > 1
Ralph hand: ( 20 ): 8 of Spades, 7 of Clubs, 5 of Hearts
Ralph, type 1 for a hit and 0 to stay > 1
Ralph hand: ( 30 ): 8 of Spades, 7 of Clubs, 5 of Hearts, Jack of Diamonds
That is a bust.
Ralph total is 30 and Dealer total is 21 .
Ralph went bust and loses the hand.
Dealer's turn. Type any key to continue >
Dealer stays with 21
Game over. Ralph has 30 Dealer has 21 .
The dealer wins this hand.
Documentation
Your blackjack.py script should contain a comment block at the
beginning with your name, date, file name. It should include a brief
description of the program, including the problem statement. Each
method in the BlackjackHand class should include
a docstring that describes its purpose.
What to hand in
Submit the following in paper:
- Your blackjack.py script.
- Printed copies of your output.
Plagiarism and academic dishonesty
Please remember our course policy on plagiarism and academic dishonesty:
You are encouraged to consult with one another when you work on homework
assignments, but in the end everyone must do one's own work to hand in. In
particular, discussion of homework assignments should be limited to
brainstorming and verbally going through strategies, but it must not involve
one student sharing written solutions with another student. In the end
everyone must write up solutions independently. If you have discussed
with classmates or used any outside source, you must clearly indicate so on
your solutions and provide all references. Turning in another person's work
under your name is plagiarism and qualifies as academic dishonesty. Academic
dishonesty is a serious intellectual violation, and the consequences can be
severe. For more details, read the
Student Handbook 2010–2011, pp. 21–29.