Task 2: A blackjack dealer In this task you will implement a class for storing and scoring a hand of blackjack, and the
Posted: Fri May 20, 2022 5:56 pm
Task 2: A blackjack dealer
In this task you will implement a class for storing and scoring
a hand of blackjack, and the AI of a blackjack dealer (don't worry,
it's not that intelligent or complex).
Background
Scoring a blackjack hand
In blackjack, the score of a player is determined by their hand
of cards. More specifically, it's determined by the ranks of their
cards (suits have no effect on scores) :
Each numeric card is worth the same as the card's rank. For
example, a 2 of hearts is worth 2 points, a 7 of diamonds is worth
7 points, etc.
Each of the face cards Jack, Queen and King are worth 10
points.
The ace is worth either 1 point or 11 points (the player is free
to choose).
The objective is to get a hand with a score that is as close as
possible to 21, without going over. If the player gets a score that
is over 21, they go "bust" and the hand is worthless.
Players are initially given two cards, and the choice to either
"stay" (meaning to not accept any new cards) or "hit" (meaning to
accept an additional card). If they choose to stay, then their hand
(and its resulting score) is finalised. If they choose to hit, then
they will be given another card, which will increase their score
and potentially make them go bust. Players can continue "hitting"
as many times as they like until they either go bust, or choose to
stay.
The dealer
In blackjack, players don't compete against each other. Instead,
each player competes against the dealer. While players are
completely free to make their own decisions, dealers have strict
rules to follow that determine whether they stay or fold, depending
on the cards in their hand.
If the value of the dealer's hand is less than 17, they must hit
and take an additional card.
If the value of the dealer's hand is 17 or more, they must
stay.
They also have strict rules to follow when deciding how to use
any aces in their hand.
If the dealer has an ace, they must count it as 11 unless it
would cause them to go bust. Otherwise they must count it as a
1.
In other words, the dealer's decisions are entirely predictable
and automatic. This makes it a good candidate for us to
automate!
Task Details
Your job is to implement two classes: BlackjackHand and
BlackjackDealer .
Your workspace includes compiled versions of the Card and Deck
class which have been implemented exactly as specified in Task 1.
You won't be able to see the source code of these classes, but you
can can (and should) use them in your solution. Refer back to the
task description for details about how these classes work and which
methods they have.
The BlackjackHand class
You have been provided an empty BlackjackHand class, within
which you must implement the following methods:
A constructor that accepts no parameters, and results in an
empty hand of cards that has a score of 0.
A add method, which accepts an instance of Card and returns
nothing. Calling this method should result in the given card being
added to the hand, and the hand's score being updated.
A getSize method, which accepts no parameters, and returns the
number of cards in the hand.
A get method, which accepts an index as an int, and returns the
card that is at the given index. E.g. calling get(0) should return
the first card, get(1) should return the second, etc.
A getScore method, which returns the score of the hand. This
score must be calculated according to the rules described above in
Background > Scoring a blackjack hand. If
there are Aces in the hand, then this score must make the best use
of them to ensure the score is as high as possible without going
bust (unless going bust is unavoidable).
An isBust method, which returns true if the hand score
is greater than 21, or false otherwise.
The BlackjackDealer class
You have been provided an empty BlackjackDealer class, within
which you must implement the following methods:
A constructor that accepts no parameters, and results in a
dealer that has an empty hand of cards, and is not staying.
An isStaying method, which accepts no parameters, and returns
true if the dealer is currently staying, or false
if not.
A getHand method, which accepts no parameters, and returns the
dealers hand as an instance of BlackjackHand.
A take method, which accepts a Card parameter, and
returns false if the dealer is unable to accept the card
(i.e. if they are staying or bust) or true if they do accept the
card. If the dealer can accept the card, then calling this method
should result in the given card being added to the dealer's hand,
and the dealer updating whether they are staying or not. Their
decision to stay or not must be based on the rules discussed above
in Background > The dealer.
A reset method, which accepts no parameters and returns nothing.
Calling this method will result in the dealer having an empty hand,
and not staying.
The runner class
Again, we have provided a Runner class with a main method that
will be executed when you click the Run button. You are
welcome to change this method however you like. This file is not
used in testing and is provided purely to allow you to interact
with and explore your own code.
In this task you will implement a class for storing and scoring
a hand of blackjack, and the AI of a blackjack dealer (don't worry,
it's not that intelligent or complex).
Background
Scoring a blackjack hand
In blackjack, the score of a player is determined by their hand
of cards. More specifically, it's determined by the ranks of their
cards (suits have no effect on scores) :
Each numeric card is worth the same as the card's rank. For
example, a 2 of hearts is worth 2 points, a 7 of diamonds is worth
7 points, etc.
Each of the face cards Jack, Queen and King are worth 10
points.
The ace is worth either 1 point or 11 points (the player is free
to choose).
The objective is to get a hand with a score that is as close as
possible to 21, without going over. If the player gets a score that
is over 21, they go "bust" and the hand is worthless.
Players are initially given two cards, and the choice to either
"stay" (meaning to not accept any new cards) or "hit" (meaning to
accept an additional card). If they choose to stay, then their hand
(and its resulting score) is finalised. If they choose to hit, then
they will be given another card, which will increase their score
and potentially make them go bust. Players can continue "hitting"
as many times as they like until they either go bust, or choose to
stay.
The dealer
In blackjack, players don't compete against each other. Instead,
each player competes against the dealer. While players are
completely free to make their own decisions, dealers have strict
rules to follow that determine whether they stay or fold, depending
on the cards in their hand.
If the value of the dealer's hand is less than 17, they must hit
and take an additional card.
If the value of the dealer's hand is 17 or more, they must
stay.
They also have strict rules to follow when deciding how to use
any aces in their hand.
If the dealer has an ace, they must count it as 11 unless it
would cause them to go bust. Otherwise they must count it as a
1.
In other words, the dealer's decisions are entirely predictable
and automatic. This makes it a good candidate for us to
automate!
Task Details
Your job is to implement two classes: BlackjackHand and
BlackjackDealer .
Your workspace includes compiled versions of the Card and Deck
class which have been implemented exactly as specified in Task 1.
You won't be able to see the source code of these classes, but you
can can (and should) use them in your solution. Refer back to the
task description for details about how these classes work and which
methods they have.
The BlackjackHand class
You have been provided an empty BlackjackHand class, within
which you must implement the following methods:
A constructor that accepts no parameters, and results in an
empty hand of cards that has a score of 0.
A add method, which accepts an instance of Card and returns
nothing. Calling this method should result in the given card being
added to the hand, and the hand's score being updated.
A getSize method, which accepts no parameters, and returns the
number of cards in the hand.
A get method, which accepts an index as an int, and returns the
card that is at the given index. E.g. calling get(0) should return
the first card, get(1) should return the second, etc.
A getScore method, which returns the score of the hand. This
score must be calculated according to the rules described above in
Background > Scoring a blackjack hand. If
there are Aces in the hand, then this score must make the best use
of them to ensure the score is as high as possible without going
bust (unless going bust is unavoidable).
An isBust method, which returns true if the hand score
is greater than 21, or false otherwise.
The BlackjackDealer class
You have been provided an empty BlackjackDealer class, within
which you must implement the following methods:
A constructor that accepts no parameters, and results in a
dealer that has an empty hand of cards, and is not staying.
An isStaying method, which accepts no parameters, and returns
true if the dealer is currently staying, or false
if not.
A getHand method, which accepts no parameters, and returns the
dealers hand as an instance of BlackjackHand.
A take method, which accepts a Card parameter, and
returns false if the dealer is unable to accept the card
(i.e. if they are staying or bust) or true if they do accept the
card. If the dealer can accept the card, then calling this method
should result in the given card being added to the dealer's hand,
and the dealer updating whether they are staying or not. Their
decision to stay or not must be based on the rules discussed above
in Background > The dealer.
A reset method, which accepts no parameters and returns nothing.
Calling this method will result in the dealer having an empty hand,
and not staying.
The runner class
Again, we have provided a Runner class with a main method that
will be executed when you click the Run button. You are
welcome to change this method however you like. This file is not
used in testing and is provided purely to allow you to interact
with and explore your own code.