Assignment 1
Assignment 1
1 Introduction
Like the labs, assignments are marked automatically online. This means that your program must
produce exactly the expected output for it to be considered correct. For each task, submit the
appropriate Python file to the Moodle marker. Successfully completing all six tasks will earn
you 100%.
Again, please be aware of the policy surrounding plagiarism (see the course outline for a full
description). Code will be scrutinised, and anyone found to have cheated (including the person
from whom the code was copied) will immediately receive 0, and may be subject to disciplinary
sanctions.
2 Background
As mathematicians, we all know that the odds in any gambling game are stacked against
us. No casino would ever offer players a game where they are able to come out ahead in
expectation. Gambling should thus be considered a form of entertainment, where players
are under no illusion that they will most likely lose money in the long run. In my opin-
ion, there are better things to spend your money on. Like this 53-inch Teddy Bear (http:
//costcocouple.com/53-inch-plush-teddy-bear/). But that’s just me.
Your friend1 Kanye, however, is convinced otherwise. He wants to visit his local casino and
win money by playing Blackjack. Before visiting the casino, he’d like to get some practice in,
and has asked you to write a program that will allow him to play hands of Blackjack on his
computer. Normally you’d decline such a request, but he’s promised to take you to the next MTV
Video Music Awards and so, reluctantly, you agree.
1
More of an acquaintance, really.
1
3 The Plan
Kanye is ... well ... let’s just say he can’t count to twenty with his shoes on. You therefore decide
to implement a simplified version2 of Blackjack, the rules of which are described below.
The Rules
Setup
The game uses a single deck of standard playing cards, and is contested between two people:
the dealer and the player. At the start of each round, the player receives two cards, while the
dealer receives a single card. The object of the game is to beat the dealer by having a hand of
cards with a value greater than the dealer’s, but not more than 21.
The Cards
Figure 1: Standard deck of cards. The first row are clubs, the second spades, the third hearts
and the fourth diamonds.
The game is played with a standard deck of cards. There are 52 cards in total, divided into
4 suits: clubs, spades, hearts and diamonds. Each suit contains thirteen cards: ace, king, queen,
jack, and the cards 2 — 10.
Scoring
The value of a person’s hand is calculated as the sum of the individual cards: aces count as either
1 or 11, tens and face cards (kings, queens and jacks) are worth 10 points, while the remaining
cards are scored according to their face value.
2
The rules of this version mirror standard Blackjack, but the player only has two actions available to him: hit or
stand.
2
Scoring is slightly more involved when a hand contains an ace. In this case there are two
possible values, since an ace can be treated as either 1 or 11. If both possible values do not
exceed 21, then the hand is said to contain a usable ace. If the larger of the values is more than
21, then only the lower value is considered. For example, a hand consisting of an ace and a six
has a value of either 7 or 17, while a hand consisting of an ace, a six and a ten has a value of 17
only.
A special case occurs when the first two cards of either contestant add up to 21. In such a
case, the person is said to have Blackjack and immediately win the round. A hand with more
than two cards that add up to 21 is not Blackjack.
Play
At the beginning of the round, the dealer receives one card while the player receives two. First,
we examine the player’s cards. If they add up to 21, the player has Blackjack and immediately
wins the round. Otherwise the player is given two options: to request another card (hit) or
to stick with his current hand (stand). The player keeps making decisions until he chooses to
stand, or the value of his hand exceeds 21. If his hand exceeds 21, he is said to have busted, and
the dealer wins the round.
If the player completes his turn without busting, the dealer then takes his turn. The dealer
first draws a card. If this results in the dealer achieving Blackjack, then he immediately wins the
round. Otherwise, the dealer keeps drawing cards until the value of his hand is greater than 16,
at which point he stops drawing cards. If the dealer has a usable ace, then he stops if his higher
score is greater than 16.
If the dealer busts, or has a value less than that of the player, then the player wins. If the
dealer has a higher value, the player loses. And if both have the same value, they tie (push).
Like any good programmer, you’re not going to write this as one big system, so you break it
down into a series of manageable tasks. For each of the tasks, you plan to represent a card as a
string in the form <rank><suit>. <rank> is either 2-10, J (jack), Q (queen), K (king), or A (ace),
while <suit> is either c (clubs), d (diamonds), h (hearts), or s (spades).
For example, the “ten of hearts” is represented by 10h, while the “ace of clubs” is Ac.
3
5 The First Task (15 marks)
Write a program that takes in a single card and outputs its value according to the rules of
Blackjack. For aces, the program should output both possible values.
5.1 Input
5.2 Output
Sample Input #1
2d
Sample Output #1
2
Sample Input #2
As
Sample Output #2
1 or 11
Sample Input #3
Qh
Sample Output #3
10
4
6 The Second Task (15 marks)
Now write a program that calculates the value of two cards. If the hand’s value is 21, then the
program should output Blackjack! Otherwise, the value of the hand should be displayed. If
the hand contains a usable ace, both possible values must be output.
6.1 Input
6.2 Output
Sample Input #1
Ah
Kd
Sample Output #1
Blackjack!
Sample Input #2
As
Ac
Sample Output #2
2 or 12
Sample Input #3
Ac
4d
Sample Output #3
5 or 15
5
7 The Third Task (20 marks)
Because the program will be used for practising, a useful feature would be to suggest an action
to the player. Write a program that accepts the dealer’s single card, and the player’s two initial
cards, and outputs a suggested action according to the tables below.
7.1 Input
Input consists of three cards, each on their own line. The first card belongs to the dealer, while
the remaining two belong to the player.
7.2 Output
6
7.3 Example Input-Output Pairs
7
8 The Fourth Task (20 marks)
Naturally, either the player or the dealer may be holding any number of cards, not just two. This
is known as their hand — the collection of cards the player or dealer is currently in possession of.
Write a program that takes in a hand consisting of any number of cards and outputs the hand’s
score, as per The Second Task. If the hand consists of two cards and equals 21, Blackjack!
should be printed. Otherwise, if the hand’s value exceeds 21, the word Bust! should be dis-
played.
8.1 Input
Input consists of of a series of cards, one per line. Input is terminated by a single line containing
the word end.
8.2 Output
Output either the value(s) of the hand, Blackjack! or Bust! where appropriate.
8
9 The Fifth Task (20 marks)
At this point, we need to determine who has won a particular round, given the cards held by the
player and the dealer. Write a program that accepts as input the player’s hand (consisting of any
number of cards) and the dealer’s hand (also consisting of any number of cards), and outputs
the result. The outcome of a round is determined by the following cases in priority order (i.e.
the first condition that holds determines the outcome):
9.1 Input
The player’s hand consists of a series of cards, one per line. The player’s hand is terminated by
a single line containing the word end. Next is the dealer’s hand, consisting of a series of cards,
one per line. The dealer’s hand is terminated by a single line containing the word end.
9.2 Output
If the player wins, output Player wins!. If the dealer wins, output Dealer wins!. Otherwise,
output Push!
9
9.3 Example Input-Output Pairs
10
10 The Sixth Task (10 marks)
Having completed the previous tasks, creating a Blackjack game is simply a case of putting it all
together. Download the source code on Moodle and complete the require functions to create a
basic, but fully-working Blackjack game. You may add as many additional functions as required,
but do not modify or delete the existing code in any way. There are five functions that need to
be implemented to complete the program:
def output_score(hand)
This function takes in a list of strings (of any size) representing the cards in a hand and returns
the valuation of the hand, as per The Fourth Task. The value returned should be a string.
def is_blackjack(hand)
This function takes in a list of strings (of any size) representing the cards in a hand and returns
whether the hand is a Blackjack hand. The value returned should be a Boolean.
def is_bust(hand)
This function takes in a hand of any size and returns whether it is a bust hand. The value
returned should be a Boolean.
This function takes in a hand of any size and the dealer’s card (as a string), and returns what
action should be taken, as per The Third Task. The value returned should be a string.
def get_high_score(hand)
This function takes in a hand of any size and returns its numeric score. If the hand contains a
usable ace, then this function returns the larger of its two possible values. The value returned
should be an integer.
This function takes in the player’s hand and dealer’s hand (of any size) and returns the winner,
as per The Fifth Task. The value returned should be an string.
11
10.1 Input
Input consists of the user responses to the on-screen prompts. For example, when the program
starts, the following is displayed
Input would then be either the character N or Q. There is no need to worry about handling
input — the existing code takes care of it for you.
10.2 Output
Output is handled by the existing program. Ensure your functions do not print anything to the
screen, and do not modify existing output in any way.
Run the program and interact with it. A sample of what it might look like is reproduced below.
User input is indicated by red text.
12
(N)ew round or (Q)uit? N
Dealer shows 7h -> 7
Player shows 2c 9d -> 11
(H)it, (S)tand, or (A)dvice? H
Player shows 2c 9d 5s -> 16
(H)it, (S)tand, or (A)dvice? S
Dealer shows 7h Jh -> 17
Dealer wins!
******************************************
(N)ew round or (Q)uit? N
Dealer shows 4c -> 4
Player shows 10h Qc -> 20
(H)it, (S)tand, or (A)dvice? S
Dealer shows 4c 6h -> 10
Dealer shows 4c 6h Kd -> 20
Push!
******************************************
(N)ew round or (Q)uit? N
Dealer shows 3s -> 3
Player shows 8c Ah -> 9 or 19
(H)it, (S)tand, or (A)dvice? A
Advice: Stand
(H)it, (S)tand, or (A)dvice? S
Dealer shows 3s Qs -> 13
Dealer shows 3s Qs Kh -> Bust!
Player wins!
******************************************
(N)ew round or (Q)uit? Q
13