Twenty-One - Procedural design, Simple Lists, and Card Tricks
Due: Monday, September 30th @ 11:55pm
Submission Guidelines:
- Ensure your name and student number are at the top of any files submitted.
- Submit all code files as .py files
- Create a .zip file of all your files, even if there's only one (this helps us keep organized):
- Name your zip file
CUID_Assignment3.zip
, where CUID is replaced by your student number. - Submit your assignment using Brightspace.
- Any code files that will not run in python 3 may be subject to penalties up to 100% of the non-functioning code.
- Your program(s) should not use features or import modules that are outside of the scope of this course.
- Marks will be deducted for late submissions (see the course outline for details).
- Any bonus marks received may not result in a grade greater than 100%.
- If your internet connection at home is down or does not work, we will not accept this as a reason for handing in an assignment late, so do not wait until the last minute to submit your assignment!
- You are responsible for submitting all files and ENSURING that the submission is completed successfully. If you are having issues with submission, contact the instructor before the due date.
For this assignment you will create a one-player variation of the game BlackJack.
The Game
The game will be divided into five rounds. The goal of each round is to accumulate a hand of cards that gets as close to 21 without going over. At the beginng of the game you will start with a score of 100 points. After each round, the difference between your hand and 21 will be subtracted from your total score. The object of the entire game is to get the highest score possible at the end of 5 rounds.
Playing a Round
The rules of each round work similarly to playing a hand of BlackJack. You will be dealt a hand of two cards, each worth a given value (see Counting a Hand below). At this point you are given the choice to either "Stand", that is keep your hand where it is, or "Hit", and be dealt another card. You may continue to hit as many times as you like, until you wish to stand or until you go over 21. If your hand goes over 21 then your hand is "Bust", and the round ends.
Scoring a Round
At the beginning of the game you will start with 100 points. After each round the difference between the total value of your hand and the perfect score of 21 will be subtracted from your total. If you bust, however, a value of 21 will be subtracted from your total.
Counting a Hand
The deck of cards will be simulated with integers of values from 1 to 13, where 1 represents the Ace (A), 11 the Jack(J), 12 the Queen(Q), and 13 the King(K). Use the following rules when determining the value of a hand of cards.
- A hand is worth the sum of the value of each of the individual cards.
- Most cards are worth their value (ie, a 9 is worth 9).
- "Face cards" (ie, Jack, Queen, King) are worth 10.
- Aces are worth either 11 or 1. For your program consider Aces are worth 11 unless the total hand is over 21, then they are worth 1.
Below are some example hands and their sums:
- 10 5 = 15
- 2 2 3 = 7
- 7 7 7 7 = 28
- J 7 = 17
- K Q = 20
- A J = 21
- A A = 12 #11 + 1
- K A A = 12 #10 + 1 + 1
- A 3 A = 15 #11 + 3 + 1
- A 4 10 K = 25 #1 + 4 + 10 + 10
- A 3 4 A 5 = 14 #1 + 3 + 4 + 1 + 5
Scoring the Game
At the start of the game your total score will begin at 100. Each round your score will be decreased by 0 or more points. At the end of the game your final score will be a number in the range [-5,100], and it will be ranked according to the following categories:
- 95+: Ace!
- 85 to 94: King
- 75 to 84: Queen
- 50 to 74: Jack
- 25 to 49: Commoner
- -5 to 24: Noob
User Interaction
User interaction in this game will be entirely text-based. According to the following rules.
- At the start of each round, display the round number and the player's current score.
- Each time the player is dealt new card(s) display their current hand.
- Display of the player's hand should include the sum of the cards in their hand.
- Display of a card should be a value in the range 2-10, or one of the face cards: A, J, Q, K
- After being dealt new card(s), the player should be prompted to either 'hit' or 'stand' by typing one of those two words.
- If the player's hand ever busts, they should not be prompted to hit or stand.
- Invalid user inputs should be met with a repeated prompt.
- At the end of the game (after 5 rounds), the player's final score and rank should be displayed.
- Once the game is complete your program should prompt the user if they want to play again.
See below for example interactions.
Program Design Requirements
Your code must be structured in a procedural style. That is, your code should have a main() function that acts as the starting point of your program. The main function should be responsible for starting, repeating, and ending your game. In addition, your program should have (at minimum) the following other functions:
- play() - returns nothing, controls the mechanics of one complete execution of your game (not including replays)
- sumHand(list)→ int - sums and returns the value of the inputted hand according to the rules above
- displayHand(list) - returns nothing, but displays the given hand as well as the given hand's sum to the console
- dealCard() → int - returns a random card
- getRank(int)→string - returns the rank corresponding to the inputted total points
Please note, your implementation should use the signatures (function names and expected parameters and return types) shown here. You are encouraged to define any additional helper functions you may need.
You should not make use of any global variables in your solution.
Bonus
As a bonus challenge, design your game so that it simulates an authentic deck of 52 cards for each round. This means that at the start of each round you will build a list of 52 cards containing exactly 4 of each value (1-13). Each time the player is dealt a new card that card should be removed from the deck (so the player will never get a hand of 5 Aces, for example). Note: if you attempt the bonus you will likely need to modify the signature for the required dealCard() function mentioned above so that it accepts the deck of cards as argument. Please mention in the comments of that function that your solution attemped this bonus.
Examples
> python twentyone.py Round 1 Score: 100 Your hand: Q K (20) Would you like to 'hit' or 'stand': stand Round 2 Score: 99 Your hand: 7 7 (14) Would you like to 'hit' or 'stand': hit Your hand: 7 7 7 (21) Would you like to 'hit' or 'stand': stand Round 3 Score: 99 Your hand: 3 A (14) Would you like to 'hit' or 'stand': hit Your hand: 3 A 3 (17) Would you like to 'hit' or 'stand': stand Round 4 Score: 95 Your hand: 2 8 (10) Would you like to 'hit' or 'stand': hit Your hand: 2 8 J (20) Would you like to 'hit' or 'stand': stand Round 5 Score: 94 Your hand: 2 K (12) Would you like to 'hit' or 'stand': hit Your hand: 2 K K (22) Bust! Final score: 73, Your rank: Jack Would you like to play again? (y/n) n Goodbye!
> python twentyone.py Round 1 Score: 100 Your hand: K 10 (20) Would you like to 'hit' or 'stand': hit Your hand: K 10 K (30) Bust! Round 2 Score: 79 Your hand: 7 9 (16) Would you like to 'hit' or 'stand': hit Your hand: 7 9 2 (18) Would you like to 'hit' or 'stand': hit Your hand: 7 9 2 10 (28) Bust! Round 3 Score: 58 Your hand: 10 A (21) Would you like to 'hit' or 'stand': hit Your hand: 10 A 7 (18) Would you like to 'hit' or 'stand': hit Your hand: 10 A 7 5 (23) Bust! Round 4 Score: 37 Your hand: 10 9 (19) Would you like to 'hit' or 'stand': hit Your hand: 10 9 J (29) Bust! Round 5 Score: 16 Your hand: 4 A (15) Would you like to 'hit' or 'stand': hit Your hand: 4 A Q (15) Would you like to 'hit' or 'stand': hit Your hand: 4 A Q 4 (19) Would you like to 'hit' or 'stand': hit Your hand: 4 A Q 4 Q (29) Bust! Final score: -5, Your rank: Noob Would you like to play again? (y/n) y Round 1 Score: 100 Your hand: 6 10 (16) Would you like to 'hit' or 'stand': yes Would you like to 'hit' or 'stand': 16 is good, thanks. Would you like to 'hit' or 'stand': true Would you like to 'hit' or 'stand': STAND Would you like to 'hit' or 'stand': Would you like to 'hit' or 'stand': 16 Would you like to 'hit' or 'stand': stand Round 2 Score: 95 Your hand: A 4 (15) Would you like to 'hit' or 'stand': stand Round 3 Score: 89 Your hand: 7 Q (17) Would you like to 'hit' or 'stand': stand Round 4 Score: 85 Your hand: 5 A (16) Would you like to 'hit' or 'stand': stand Round 5 Score: 80 Your hand: 3 3 (6) Would you like to 'hit' or 'stand': hit Your hand: 3 3 2 (8) Would you like to 'hit' or 'stand': hit Your hand: 3 3 2 9 (17) Would you like to 'hit' or 'stand': stand Final score: 76, Your rank: Queen Would you like to play again? (y/n) maybe I'm sorry maybe is not a valid option. Please enter 'y' or 'n'. Would you like to play again? (y/n) qwert I'm sorry qwert is not a valid option. Please enter 'y' or 'n'. Would you like to play again? (y/n) no I'm sorry no is not a valid option. Please enter 'y' or 'n'. Would you like to play again? (y/n) N I'm sorry N is not a valid option. Please enter 'y' or 'n'. Would you like to play again? (y/n) n Goodbye!
Grading Scheme
- /8 marks - play() iterates through 5 rounds of the game correctly
- /5 marks - User's cards and sum are correctly displayed with each change to their hand.
- /5 marks - User is prompted to hit/stand each round correctly, and with input validation.
- /8 marks - Cards are dealt, represented, and summed correctly including special rules for A,J,Q,K
- /5 marks - Procedural design used, with required procedures implemented
- /5 marks - Documentation, comments, and code organization
- Bonus: /5 marks
- Total - [/36]