0% found this document useful (0 votes)
37 views

Assignment 1

The document describes an assignment to write a simplified Blackjack game for practice. It is broken into multiple tasks: 1. Write a program that takes a single card as input and outputs its value. 2. Write a program that takes two cards as input and outputs their total value or indicates if it is a Blackjack. 3. Write a program that takes the dealer's card and a player's two cards, and outputs a suggested action of "Hit" or "Stand" according to strategy tables provided.

Uploaded by

Touch
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Assignment 1

The document describes an assignment to write a simplified Blackjack game for practice. It is broken into multiple tasks: 1. Write a program that takes a single card as input and outputs its value. 2. Write a program that takes two cards as input and outputs their total value or indicates if it is a Blackjack. 3. Write a program that takes the dealer's card and a player's two cards, and outputs a suggested action of "Hit" or "Stand" according to strategy tables provided.

Uploaded by

Touch
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

U NIVERSITY OF THE W ITWATERSRAND , J OHANNESBURG

School of Computer Science & Applied Mathematics

Introduction to Algorithms & Programming (COMS1018A/1022A)


Assignment 1

Due Date: 25 April 2023 23h59

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).

4 The Plan (continued)

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

A single line representing a card, in the form described by Section 4.

5.2 Output

The value (or values) of the card.

5.3 Example Input-Output Pairs

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

Input consists of two cards on individual lines.

6.2 Output

The value (or values) of the cards, or Blackjack!.

6.3 Example Input-Output Pairs

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.

If the player has a usable ace

Player Dealer’s Card


2 3 4 5 6 7 8 9 10 A
12 Hit Hit Hit Hit Hit Hit Hit Hit Hit Hit
13 Hit Hit Hit Hit Hit Hit Hit Hit Hit Hit
14 Hit Hit Hit Hit Hit Hit Hit Hit Hit Hit
15 Hit Hit Hit Hit Hit Hit Hit Hit Hit Hit
16 Hit Hit Hit Hit Hit Hit Hit Hit Hit Hit
17 Hit Hit Hit Hit Hit Hit Hit Hit Hit Hit
18 Stand Stand Stand Stand Stand Stand Stand Hit Hit Hit
19+ Stand Stand Stand Stand Stand Stand Stand Stand Stand Stand

If the player does not have a usable ace

Player Dealer’s Card


2 3 4 5 6 7 8 9 10 A
4-8 Hit Hit Hit Hit Hit Hit Hit Hit Hit Hit
9 Hit Hit Hit Hit Hit Hit Hit Hit Hit Hit
10 Hit Hit Hit Hit Hit Hit Hit Hit Hit Hit
11 Hit Hit Hit Hit Hit Hit Hit Hit Hit Hit
12 Hit Hit Stand Stand Stand Hit Hit Hit Hit Hit
13 Stand Stand Stand Stand Stand Hit Hit Hit Hit Hit
14 Stand Stand Stand Stand Stand Hit Hit Hit Hit Hit
15 Stand Stand Stand Stand Stand Hit Hit Hit Hit Hit
16 Stand Stand Stand Stand Stand Hit Hit Hit Hit Hit
17+ Stand Stand Stand Stand Stand Stand Stand Stand Stand Stand

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

Output either Hit or Stand, according to the above strategy

6
7.3 Example Input-Output Pairs

Sample Input #1 Sample Input #2


4d Ac
Ac 10s
As 10h

Sample Output #1 Sample Output #2


Hit Stand

Sample Input #3 Sample Input #4


2d 8d
Ah 7d
6c 7h

Sample Output #3 Sample Output #4


Hit Hit

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.3 Example Input-Output Pairs

Sample Input #1 Sample Input #2


Ah 10d
Kd 10h
end Ac
end

Sample Output #1 Sample Output #2


Blackjack! 21

Sample Input #3 Sample Input #4


Ac Ac
6h 6h
10d 10d
end 5d
end

Sample Output #3 Sample Output #4


17 Bust!

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):

1. The player wins if he holds a Blackjack hand.


2. The dealer wins if the player holds a hand that is bust.
3. The dealer wins if he holds a Blackjack hand.
4. The player wins if the dealer holds a hand that is bust.
5. The higher scores of both hands are compared. The person with the higher value wins. If
the values are identical, it is a push (tie)

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

Sample Input #1 Sample Input #2


Ah 4d
Kd 3h
end Ac
10s end
end Ks
8d
end

Sample Output #1 Sample Output #2


Player wins! Push!

Sample Input #3 Sample Input #4


10d 5c
6h 6h
end 10d
6d end
Qs Ah
Js 10s
end end

Sample Output #3 Sample Output #4


Player wins! Dealer wins!

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.

def get_advice(player_hand, dealer_card)

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.

def get_winner(player_hand, dealer_hand)

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

(N)ew round or (Q)uit?

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.

10.3 Sample Run

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

You might also like