PLAYER – METHOD SUMMARY
Player objects have a name, score and hand of cards. They also contain the following methods for accessing and
modifying this information:
get_name(name)
Returns the Player's name
hit_me(Card)
Adds the argument Card to the Player's hand. Cards should be drawn from a Deck object before being added
to the Player using hit_me()
get_hand()
Returns a list containing all Cards in the Player's hand. Each Card contains methods allowing you to get the
value, rank or suit of the Card. You will have to loop through each item in the list and print them out to get a
summary of what is in the Player's hand.
get_hand_value()
Returns the combined value of all Cards in the Player's hand. Aces will be increased to a value of 11 if the
resulting value would be less than or equal to 21. Otherwise, Aces will have a value of 1. All other face cards
including Jacks, Queens and Kings will contribute 10 to the total value. Number cards will contribute their
number value.
reset_hand()
Removes all Cards from the Player's hand.
is_bust()
Returns True if the hand value exceeds 21. Otherwise returns False.
get_score()
Returns the number of games the Player has won in a row.
win()
Adds 1 to the number of games the Player has won in a row.
lose()
Resets the number of games the Player has won in a row back to zero.
DECK – METHOD SUMMARY
A Deck object contains 52 unique instances of the Card class. The Deck contains the following methods for accessing
and modifying Cards and information:
reset()
Restores the Deck to have 52 cards sorted in numerical order. The Deck should be reset between games.
shuffle()
Randomizes the order of the Cards in the Deck. Whenever the Deck is reset, it should also be shuffled.
draw_card()
Removes and returns the top Card from the Deck. If the deck has been shuffled previously, this Card will be
random. If the size of the Deck is 0, this will return None.
size()
Returns the number of Cards left in the Deck. Each time the draw_card() method is called, the size will
decrease by 1.
CARD – METHOD SUMMARY
A Card object contains a rank, suit and value. This class is immutable and should only be accessed from a Deck
object. The Card class contains the following methods for accessing information:
get_value()
Return the number value of the Card from 1-10. Aces will return 1 even though they may be worth 11.
get_suit()
Returns the Card's suit. This could be either "Spades", "Hearts", "Diamonds" or "Clubs".
get_rank()
Returns the Card's rank as a String. This could return "Ace", "Jack", "Queen", "King" or a number from 2 – 10.
PSP Assignment 2 - 201801 Page 13 of 24
STAGES
It is recommended that you develop this part of the assignment in the suggested stages. Each stage is worth a portion
of the marks. Many problems in later stages are due to errors in early stages. Make sure you have finished and
thoroughly tested each stage before continuing.
The following stages of development are recommended:
Stage 1
You will need both the blackjack.py and game.py files for this assignment. These have been provided for you.
Find these in Moodle. Ensure that they are saved in the same folder as each other.
Test to ensure that this is working correctly by opening and running game.py. If this is working correctly, you should
see the following output:
"Welcome to Blackjack"
Stage 2
Add code underneath the constructor for the dealer object. Take input for the player's name by prompting the user,
"Enter your name: ". While the name contains any space characters, print, "ERROR Must only be 1 word" and take
input for the name using the same prompt. Modify the player constructor to take the input name as an argument.
Stage 3
Implement the output_player function to print the argument Player's name and each card in their hand followed by the
combined hand value. You will need to call the player methods player.get_name(), player.get_hand() and
player.get_hand_value() to get this information. You may need to use a loop to get each card before printing them.
You will need to print it in the form.
"'s hand: , , \n
Total: "
For example, if the Player's name is "Bob" and their hand contains the Ace of Spades, 5 of Hearts and 10 of
Diamonds, the output_player function would print:
"Bob's hand: Ace of Spades, 5 of Hearts, 10 of Diamonds
Total: 16"
Test this function is working by passing both the player and dealer into the function as arguments. Remove this test
code when you are sure it is working.
Stage 4
Implement the play_game function to shuffle the deck. It should then draw 2 cards from the Deck for both the player
and dealer. Call the hit_me method to add a card to a Player's hand. When both players have 2 cards, call the
output_player function with both players as an argument. Test this is working by calling the play_game function with
the player, dealer and deck as arguments. Run your code.
It should print something like this:
Welcome to Blackjack
Enter your name: Michael
Dealer's hand: Ace of Spades, 6 of Spades
Total: 17
Michael's hand: 2 of Diamonds, 2 of Spades
Total: 4
PSP Assignment 2 - 201801 Page 14 of 24
Stage 5
In the main program, create a variable called play set to the string "yes". While this is "yes", call the play_game
function. Inside the loop take input for the play variable by asking if the user, "Would you like to play again (yes/no): ".
Add an error handling loop that ensures the user can only enter "yes" or "no". If the user enters anything else, it should
output, "ERROR: Only enter 'yes' or 'no'" and then ask for input again.
Running your code should produce output that looks like this:
Welcome to Blackjack
Enter your name: Bob Brown
ERROR Must only be 1 word
Enter your name: Michael
Dealer's hand: 2 of Hearts, 5 of Spades
Total: 7
Michael's hand: 3 of Diamonds, 7 of Clubs
Total: 10
Would you like to play again (yes/no): y
ERROR: Only enter 'yes' or 'no'
Would you like to play again (yes/no): yes
Dealer's hand: 2 of Hearts, 5 of Spades, Ace of Spades, 7 of Diamonds
Total: 15
Michael's hand: 3 of Diamonds, 7 of Clubs, 9 of Diamonds, 6 of Hearts
Total: 25
Would you like to play again (yes/no): no
Notice that the player hand is now 4 cards long. The Player hands should be reset between games. The Deck should
also be reset and shuffled.
Stage 6
Implement the player_turn function so that it asks the user "hit or stand? " Add an error handling loop that ensures the
user can only enter "hit" or "stand". If the user enters anything else, it should output, "ERROR: Only enter 'hit' or
'stand'" and then ask for input again.
You should call the player's is_bust method to find out if their hand value exceeds 21. While the player has not bust
and the player's choice is "hit", the player should draw a card from the deck. It should then call the output_player
function again to see the updated hand. If that card draw did not cause the player to bust, Ask the user to "hit or
stand? " again, followed by the same error handling loop as before.
Call the player_turn function from the play_game function. If, after the player_turn, the player has bust, it should print,
" busts!" It should also call the player lose method, so it sets their win streak score to zero. Otherwise, it
is the dealer's turn. Running your code should output something like this:
Welcome to Blackjack
Enter your name: Tiffany
Dealer's hand: Queen of Clubs, 3 of Spades
Total: 13
Tiffany's hand: 7 of Clubs, 2 of Spades
Total: 9
hit or stand? hit
Tiffany's hand: 7 of Clubs, 2 of Spades, 9 of Spades
Total: 18
hit or stand? hit
Tiffany's hand: 7 of Clubs, 2 of Spades, 9 of Spades, 8 of Hearts
Total: 26
Tiffany busts!
Would you like to play again (yes/no): no