Blackjack Table Class¶
The bets in Blackjack are associated with a hand. A player may have more than one hand in play. This will lead us to create a subclass of table to handle this complexity. In order to manage the relationships between hand and bet, we’ll rework hand, also.
We’ll look at the table in Blackjack Table Analysis.
Based on the classes defined so far, we can look at the design for the table in BlackjackTable Class.
We’ll look at some minor rework to Hand in Hand Rework.
In Blackjack Table Deliverables we’ll enumerate the deliverables for this chapter.
Blackjack Table Analysis¶
When we look at the game of Blackjack, we note that a player’s Hand object
can be split into multiple Hand instances.
In some casinos, resplits are allowed, leading to the
possibility of three or more Hand instances. Each individual Hand object
has a separate ante Bet and seperate resolution. This is
different from the way bets are currently managed for Roulette and Craps,
where the bets are associated with the player.
We have several alternatives for modeling this:
Assign responsibility to the
Tableclass to keep track of bets tied to the variousHandinstances. This would make theHanda potential key into a mapping to associate aHandobject with one or moreBetinstances. BecauseHandinstances change state, this is a poor choice for keys to a dictionary.We could put a reference to a
Handobject into eachBetobject. In this way, as eachBetobject is resolved, the relevantHandobject can be evaluated to determine theOutcomeinstance that applies and what they payout odds are.We could put a reference to the Ante
Betobject into theHandinstance. In this way, as eachHandinstance is resolved, the relevantBetinstances can be paid or lost.
It’s helpful to look at the alternatives carefullly and try to identify the various forces and consequences.
We suggest designing the Hand class to contain the associated ante Bet object.
This is least disruptive to the Bet class definition, which is a simple thing
used widely in other games.
Additional Bets.
While most Bet instances are associated with a specific Hand object,
the insurance Bet instance is always resolved before an additional hand
can be created. There doesn’t seem to be an essential association between
the initial Hand object and the insurance Bet object. We
can treat insurance as a Bet instance that follows the model
established for Craps and Roulette – it belongs to the player, rather than a particular hand.
Currently, the Bet instances are collected by the Table instance. If
we create a BlackjackTable subclass to use a Hand object
when creating a Bet instance, we can have this method do both
tasks: it can attach the Bet object to the Hand instance, and
it can save the Bet object on the Table instance, also.
BlackjackTable Class¶
-
class
BlackjackTable¶ The
BlackjackTableclass is an extension toTablethat handles the additional association betweenBetinstances and specificHandinstances in Blackjack.
Constructors¶
Methods¶
-
BlackjackTable.placeBet(self, bet: Bet, hand: Hand) → None¶ - Parameters
Updates the given
handto reference the givenbet. Then uses the superclassplaceBet()to add this bet to the list of working bets.
-
BlackjackTable.__str__(self) → str¶ Provides a nice string display of the state of the table.
Hand Rework¶
The Hand class contains a collection of individual Card instances,
and determines an appropriate total point value for the hand.
We need to add a field and some appropriate methods for associating a Bet with a Hand.
Fields¶
Methods¶
We have two implementation choices here. We’ll show these as setters and getters. However, it’s common to make these both properties of a hand.
-
setBet(self, ante: Bet) → None¶ - Parameters
ante (
Bet) – The initial bet required to play
Sets the ante
Betthat will be resolved when this hand is finished.
Here’s the alternative implementation. We can use properties for this feature.
Properties for getter and setter
class Hand:
@property
def bet(self):
return self.ante
@bet.setter
def bet(self, bet):
self.ante = bet
In this example, we’ve created a property, bet, so that can write code
like this: h.bet to fetch the bet associated with the hand.
By itself, this isn’t too useful. The setter property, however, allows us
to write code like this h.bet = Bet("Ante",1). We can then implement
any additional processing in the hand that needs to be done when the bet is changed.
Blackjack Table Deliverables¶
There are four deliverables for this exercise.
The revised
Handclass.A class which performs a unit tests of the
Handclass. The unit test should create several instances ofCard,FaceCardandAceCard, and add these to instances ofHand, to create various point totals. Additionally, the unit test should create aBetand associate it with theHand.The
BlackjackTableclass.A class which performs a unit tests of the
BlackjackTableclass. The unit test should create several instances ofHandandBetto create multipleHandinstances, each with uniqueBetinstances.
Looking Forward¶
Now that we have the core objects – card, deck, hand, and table – we can
look at the details of how the game proceeds. This involves creating multiple
hands, oferring specialized betting and playing choices, and tracking the evolving
state of a number of hands. The next chapter will look at the Game subclass
for Blackjack.