blackjack21 package

Submodules

blackjack21.dealer module

class blackjack21.dealer.Dealer(name: str, *, hit_soft_17: bool = False)[source]

Bases: object

Dealer class.

Parameters:
  • name – str

  • hit_soft_17 – bool, whether the dealer hits on a soft 17

add_card(card: Card) None[source]

Adds a card to the dealer’s hand.

property bust: bool

Dealer’s bust status (total > 21).

clear_hand() None[source]

Clears the dealer’s hand.

property hand: list[Card]

The list of Card objects in the dealer’s hand.

property is_soft: bool

Checks if the hand is ‘soft’ (an Ace is counted as 11).

property name: str

Dealer’s name.

play(deck: Deck) None[source]

Draws cards until the total is 17 or more.

property stand: bool

Dealer’s stand status (total >= 17).

property total: int

Dealer’s hand total.

blackjack21.deck module

class blackjack21.deck.Card(suit: CardSuit, rank: CardRank, value: int)[source]

Bases: object

Card class.

Parameters:
  • suit – suit of the card

  • rank – rank of the card

  • value – value of the card in the game

rank: CardRank
suit: CardSuit
value: int
class blackjack21.deck.Deck(suits: Sequence[CardSuit], ranks: Mapping[CardRank, int] = {'10': 10, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'A': 11, 'J': 10, 'K': 10, 'Q': 10}, *, count: int = 1)[source]

Bases: object

Deck of cards class (Iterable).

Parameters:
  • suits – sequence of 4 suits

  • ranks – A mapping of card ranks to their integer values.

  • count – int number of decks to be merged

Raises:
property cards: list[Card]

List of Card class objects currently in the deck.

draw_card() Card[source]

Draw a card from the deck.

Returns:

Card object

Raises:

EmptyDeckError – if the deck is out of cards

property drawn_cards: list[Card]

List of Card class objects drawn from the deck.

property penetration: float

Returns the percentage of the deck that has been used, from 0.0 to 1.0.

reset() None[source]

Resets the deck by returning all drawn cards and reshuffling.

shuffle() None[source]

Shuffle the cards in the deck.

blackjack21.exceptions module

exception blackjack21.exceptions.BlackjackException(*args, **kwargs)[source]

Bases: Exception

Blackjack base class exception.

exception blackjack21.exceptions.EmptyDeckError[source]

Bases: BlackjackException

Raised when attempting to draw from an empty deck.

exception blackjack21.exceptions.InvalidActionError(message: str)[source]

Bases: BlackjackException

Raised when an action is attempted in an invalid game state.

exception blackjack21.exceptions.InvalidPlayersData(player_data: tuple[PlayerName, BetAmount])[source]

Bases: BlackjackException

Raised when the input player data is invalid.

exception blackjack21.exceptions.InvalidRanks(ranks: Sequence[CardRank])[source]

Bases: BlackjackException

Raised when length of ranks tuple is not 13.

exception blackjack21.exceptions.InvalidSuits(suits: Sequence[CardSuit])[source]

Bases: BlackjackException

Raised when length of suits tuple is not 4.

exception blackjack21.exceptions.PlayDealerFailure(name: str)[source]

Bases: BlackjackException

exception blackjack21.exceptions.PlayFailure(name: str, action: str)[source]

Bases: BlackjackException

Raised when player tries to play double down/split when they are not eligible.

blackjack21.players module

class blackjack21.players.GameResult(value)[source]

Bases: StrEnum

Represents the outcome of a player’s hand.

BLACKJACK = 'BLACKJACK'
DEALER_BUST = 'DEALER_BUST'
DEALER_WIN = 'DEALER_WIN'
PLAYER_BUST = 'PLAYER_BUST'
PLAYER_WIN = 'PLAYER_WIN'
PUSH = 'PUSH'
SURRENDER = 'SURRENDER'
class blackjack21.players.GameState(value)[source]

Bases: StrEnum

Represents the current phase of the game.

DEALER_TURN = 'DEALER_TURN'
INIT = 'INIT'
PLAYERS_TURN = 'PLAYERS_TURN'
ROUND_OVER = 'ROUND_OVER'
class blackjack21.players.Hand(player: Player, bet: BetAmount)[source]

Bases: object

Represents a single hand of cards for a player.

Parameters:
  • player – The Player who owns this hand.

  • bet – int

add_card(card: Card) None[source]

Adds a card to the hand and updates bust/stand status.

property bet: BetAmount

Player’s bet amount.

property bust: bool

Player’s bust status.

property hand: list[Card]

List of Card class objects in the player’s hand.

property player: Player

The player who owns this hand.

result: GameResult | None
property stand: bool

Player’s stand status.

stand_action() None[source]

Sets the hand’s status to ‘stand’.

property surrendered: bool

Player’s surrender status.

property total: int

Player’s hand total.

class blackjack21.players.Player(name: PlayerName, bet: BetAmount)[source]

Bases: object

Player class, containing one or more hands.

Parameters:
  • name – str

  • bet – int

property hands: list[Hand]

List of Hand class objects for the player.

property name: PlayerName

Player name.

blackjack21.table module

class blackjack21.table.Table(players: Iterable[tuple[PlayerName, BetAmount]], deck: Deck, *, dealer_name: str = 'Dealer', hit_soft_17: bool = False)[source]

Bases: object

Create object for this class to initialize a blackjack table (Iterable through players).

Parameters:
  • players – An iterable of player tuples (name, bet).

  • deck – A Deck object to be used for the game.

  • dealer_name – The name of the dealer.

  • hit_soft_17 – bool, whether the dealer hits on a soft 17.

property current_hand: Hand | None

The hand that is currently being played.

property dealer: Dealer

Table’s Dealer class object.

property dealer_visible_hand: list[Card]

The dealer’s hand that is visible to players.

If the players’ turns are over, it returns the full hand. Otherwise, it returns only the first card (the up-card).

property deck: Deck

Table’s Deck class object.

double_down() Card[source]

The current hand doubles the bet, takes one more card, and stands.

hit() Card[source]

The current hand takes another card. Advances to the next hand if it busts or stands.

property players: list[Player]

List of Player class objects for the Table.

split() None[source]

Splits the current hand if the two cards have the same value.

stand() None[source]

The current hand stands. The game moves to the next hand.

start_game() None[source]

Deals the initial cards to start the game.

surrender() None[source]

The current hand surrenders. Only allowed on the first two cards.

Module contents

blackjack21.

exception blackjack21.BlackjackException(*args, **kwargs)[source]

Bases: Exception

Blackjack base class exception.

class blackjack21.Card(suit: CardSuit, rank: CardRank, value: int)[source]

Bases: object

Card class.

Parameters:
  • suit – suit of the card

  • rank – rank of the card

  • value – value of the card in the game

rank: CardRank
suit: CardSuit
value: int
class blackjack21.Dealer(name: str, *, hit_soft_17: bool = False)[source]

Bases: object

Dealer class.

Parameters:
  • name – str

  • hit_soft_17 – bool, whether the dealer hits on a soft 17

add_card(card: Card) None[source]

Adds a card to the dealer’s hand.

property bust: bool

Dealer’s bust status (total > 21).

clear_hand() None[source]

Clears the dealer’s hand.

property hand: list[Card]

The list of Card objects in the dealer’s hand.

property is_soft: bool

Checks if the hand is ‘soft’ (an Ace is counted as 11).

property name: str

Dealer’s name.

play(deck: Deck) None[source]

Draws cards until the total is 17 or more.

property stand: bool

Dealer’s stand status (total >= 17).

property total: int

Dealer’s hand total.

class blackjack21.Deck(suits: Sequence[CardSuit], ranks: Mapping[CardRank, int] = {'10': 10, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'A': 11, 'J': 10, 'K': 10, 'Q': 10}, *, count: int = 1)[source]

Bases: object

Deck of cards class (Iterable).

Parameters:
  • suits – sequence of 4 suits

  • ranks – A mapping of card ranks to their integer values.

  • count – int number of decks to be merged

Raises:
property cards: list[Card]

List of Card class objects currently in the deck.

draw_card() Card[source]

Draw a card from the deck.

Returns:

Card object

Raises:

EmptyDeckError – if the deck is out of cards

property drawn_cards: list[Card]

List of Card class objects drawn from the deck.

property penetration: float

Returns the percentage of the deck that has been used, from 0.0 to 1.0.

reset() None[source]

Resets the deck by returning all drawn cards and reshuffling.

shuffle() None[source]

Shuffle the cards in the deck.

exception blackjack21.EmptyDeckError[source]

Bases: BlackjackException

Raised when attempting to draw from an empty deck.

class blackjack21.GameResult(value)[source]

Bases: StrEnum

Represents the outcome of a player’s hand.

BLACKJACK = 'BLACKJACK'
DEALER_BUST = 'DEALER_BUST'
DEALER_WIN = 'DEALER_WIN'
PLAYER_BUST = 'PLAYER_BUST'
PLAYER_WIN = 'PLAYER_WIN'
PUSH = 'PUSH'
SURRENDER = 'SURRENDER'
class blackjack21.GameState(value)[source]

Bases: StrEnum

Represents the current phase of the game.

DEALER_TURN = 'DEALER_TURN'
INIT = 'INIT'
PLAYERS_TURN = 'PLAYERS_TURN'
ROUND_OVER = 'ROUND_OVER'
class blackjack21.Hand(player: Player, bet: BetAmount)[source]

Bases: object

Represents a single hand of cards for a player.

Parameters:
  • player – The Player who owns this hand.

  • bet – int

add_card(card: Card) None[source]

Adds a card to the hand and updates bust/stand status.

property bet: BetAmount

Player’s bet amount.

property bust: bool

Player’s bust status.

property hand: list[Card]

List of Card class objects in the player’s hand.

property player: Player

The player who owns this hand.

result: GameResult | None
property stand: bool

Player’s stand status.

stand_action() None[source]

Sets the hand’s status to ‘stand’.

property surrendered: bool

Player’s surrender status.

property total: int

Player’s hand total.

exception blackjack21.InvalidActionError(message: str)[source]

Bases: BlackjackException

Raised when an action is attempted in an invalid game state.

exception blackjack21.InvalidPlayersData(player_data: tuple[PlayerName, BetAmount])[source]

Bases: BlackjackException

Raised when the input player data is invalid.

exception blackjack21.InvalidRanks(ranks: Sequence[CardRank])[source]

Bases: BlackjackException

Raised when length of ranks tuple is not 13.

exception blackjack21.InvalidSuits(suits: Sequence[CardSuit])[source]

Bases: BlackjackException

Raised when length of suits tuple is not 4.

exception blackjack21.PlayDealerFailure(name: str)[source]

Bases: BlackjackException

exception blackjack21.PlayFailure(name: str, action: str)[source]

Bases: BlackjackException

Raised when player tries to play double down/split when they are not eligible.

class blackjack21.Player(name: PlayerName, bet: BetAmount)[source]

Bases: object

Player class, containing one or more hands.

Parameters:
  • name – str

  • bet – int

property hands: list[Hand]

List of Hand class objects for the player.

property name: PlayerName

Player name.

class blackjack21.Table(players: Iterable[tuple[PlayerName, BetAmount]], deck: Deck, *, dealer_name: str = 'Dealer', hit_soft_17: bool = False)[source]

Bases: object

Create object for this class to initialize a blackjack table (Iterable through players).

Parameters:
  • players – An iterable of player tuples (name, bet).

  • deck – A Deck object to be used for the game.

  • dealer_name – The name of the dealer.

  • hit_soft_17 – bool, whether the dealer hits on a soft 17.

property current_hand: Hand | None

The hand that is currently being played.

property dealer: Dealer

Table’s Dealer class object.

property dealer_visible_hand: list[Card]

The dealer’s hand that is visible to players.

If the players’ turns are over, it returns the full hand. Otherwise, it returns only the first card (the up-card).

property deck: Deck

Table’s Deck class object.

double_down() Card[source]

The current hand doubles the bet, takes one more card, and stands.

hit() Card[source]

The current hand takes another card. Advances to the next hand if it busts or stands.

property players: list[Player]

List of Player class objects for the Table.

split() None[source]

Splits the current hand if the two cards have the same value.

stand() None[source]

The current hand stands. The game moves to the next hand.

start_game() None[source]

Deals the initial cards to start the game.

surrender() None[source]

The current hand surrenders. Only allowed on the first two cards.