blackjack21 package
Submodules
blackjack21.dealer module
- class blackjack21.dealer.Dealer(name: str, *, hit_soft_17: bool = False)[source]
Bases:
objectDealer class.
- Parameters:
name – str
hit_soft_17 – bool, whether the dealer hits on a soft 17
- property bust: bool
Dealer’s bust status (total > 21).
- property is_soft: bool
Checks if the hand is ‘soft’ (an Ace is counted as 11).
- property name: str
Dealer’s name.
- 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:
objectCard 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:
objectDeck 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:
InvalidSuits – if length of suits is not 4
InvalidRanks – if length of ranks is not 13
- draw_card() Card[source]
Draw a card. Auto-resets from drawn pile if empty.
- Returns:
Card object
- Raises:
EmptyDeckError – if the deck and drawn pile are both empty
- property penetration: float
Returns the percentage of the deck that has been used, from 0.0 to 1.0.
blackjack21.exceptions module
- exception blackjack21.exceptions.BlackjackException(*args)[source]
Bases:
ExceptionBlackjack base class exception.
- exception blackjack21.exceptions.EmptyDeckError[source]
Bases:
BlackjackExceptionRaised when attempting to draw from an empty deck.
- exception blackjack21.exceptions.InvalidActionError(message: str)[source]
Bases:
BlackjackExceptionRaised when an action is attempted in an invalid game state.
- exception blackjack21.exceptions.InvalidPlayersData(player_data: tuple[str, int])[source]
Bases:
BlackjackExceptionRaised when the input player data is invalid.
- exception blackjack21.exceptions.InvalidRanks(ranks: Sequence[CardRank])[source]
Bases:
BlackjackExceptionRaised when length of ranks tuple is not 13.
- exception blackjack21.exceptions.InvalidSuits(suits: Sequence[CardSuit])[source]
Bases:
BlackjackExceptionRaised when length of suits tuple is not 4.
- exception blackjack21.exceptions.PlayDealerFailure(name: str)[source]
Bases:
BlackjackExceptionRaised when dealer tries to play their hand before all players have finished.
- exception blackjack21.exceptions.PlayFailure(name: str, action: str)[source]
Bases:
BlackjackExceptionRaised when player tries to play double down/split when they are not eligible.
blackjack21.players module
- class blackjack21.players.GameResult(value)[source]
Bases:
str,EnumRepresents 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:
str,EnumRepresents the current phase of the game.
- DEALER_TURN = 'DEALER_TURN'
- INIT = 'INIT'
- PLAYERS_TURN = 'PLAYERS_TURN'
- ROUND_OVER = 'ROUND_OVER'
- class blackjack21.players.Hand(bet: BetAmount)[source]
Bases:
objectA single hand of cards. Does not know its owner.
- Parameters:
bet – int
- property bet: BetAmount
Player’s bet amount.
- property bust: bool
Player’s bust status.
- property is_complete: bool
Hand cannot take more actions.
- result: GameResult | None
- property surrendered: bool
Player’s surrender status.
- property total: int
Player’s hand total.
- class blackjack21.players.Player(name: PlayerName, bet: BetAmount)[source]
Bases:
objectPlayer class, containing one or more hands.
- Parameters:
name – str
bet – int
- insert_hand_after(existing: Hand, new_hand: Hand) None[source]
Insert a new hand immediately after an existing one (for splitting).
- property name: PlayerName
Player name.
blackjack21.table module
- class blackjack21.table.Action(value)[source]
Bases:
str,EnumPlayer actions during their turn.
- DOUBLE = 'double'
- HIT = 'hit'
- SPLIT = 'split'
- STAND = 'stand'
- SURRENDER = 'surrender'
- class blackjack21.table.CardSource(*args, **kwargs)[source]
Bases:
ProtocolAnything that can provide cards.
- class blackjack21.table.Table(players: Iterable[tuple[str, int]], deck: CardSource, *, dealer_name: str = 'Dealer', hit_soft_17: bool = False, on_round_reset: Callable[[], None] | None = None)[source]
Bases:
objectCreate object for this class to initialize a blackjack table (Iterable through players).
- Parameters:
players – An iterable of player tuples (name, bet).
deck – A card source 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.
on_round_reset – Optional callback invoked when a round resets.
- available_actions() frozenset[Action][source]
Returns the set of legal actions for the current hand.
- 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: CardSource
The table’s card source.
Module contents
blackjack21.
- class blackjack21.Action(value)[source]
Bases:
str,EnumPlayer actions during their turn.
- DOUBLE = 'double'
- HIT = 'hit'
- SPLIT = 'split'
- STAND = 'stand'
- SURRENDER = 'surrender'
- exception blackjack21.BlackjackException(*args)[source]
Bases:
ExceptionBlackjack base class exception.
- class blackjack21.Card(suit: CardSuit, rank: CardRank, value: int)[source]
Bases:
objectCard 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.CardSource(*args, **kwargs)[source]
Bases:
ProtocolAnything that can provide cards.
- class blackjack21.Dealer(name: str, *, hit_soft_17: bool = False)[source]
Bases:
objectDealer class.
- Parameters:
name – str
hit_soft_17 – bool, whether the dealer hits on a soft 17
- property bust: bool
Dealer’s bust status (total > 21).
- property is_soft: bool
Checks if the hand is ‘soft’ (an Ace is counted as 11).
- property name: str
Dealer’s name.
- 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:
objectDeck 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:
InvalidSuits – if length of suits is not 4
InvalidRanks – if length of ranks is not 13
- draw_card() Card[source]
Draw a card. Auto-resets from drawn pile if empty.
- Returns:
Card object
- Raises:
EmptyDeckError – if the deck and drawn pile are both empty
- property penetration: float
Returns the percentage of the deck that has been used, from 0.0 to 1.0.
- exception blackjack21.EmptyDeckError[source]
Bases:
BlackjackExceptionRaised when attempting to draw from an empty deck.
- class blackjack21.GameResult(value)[source]
Bases:
str,EnumRepresents 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:
str,EnumRepresents the current phase of the game.
- DEALER_TURN = 'DEALER_TURN'
- INIT = 'INIT'
- PLAYERS_TURN = 'PLAYERS_TURN'
- ROUND_OVER = 'ROUND_OVER'
- class blackjack21.Hand(bet: BetAmount)[source]
Bases:
objectA single hand of cards. Does not know its owner.
- Parameters:
bet – int
- property bet: BetAmount
Player’s bet amount.
- property bust: bool
Player’s bust status.
- property is_complete: bool
Hand cannot take more actions.
- result: GameResult | None
- property surrendered: bool
Player’s surrender status.
- property total: int
Player’s hand total.
- class blackjack21.HandTotal(value: int, is_soft: bool)[source]
Bases:
NamedTupleComputed hand total with metadata.
- is_soft: bool
Alias for field number 1
- value: int
Alias for field number 0
- exception blackjack21.InvalidActionError(message: str)[source]
Bases:
BlackjackExceptionRaised when an action is attempted in an invalid game state.
- exception blackjack21.InvalidPlayersData(player_data: tuple[str, int])[source]
Bases:
BlackjackExceptionRaised when the input player data is invalid.
- exception blackjack21.InvalidRanks(ranks: Sequence[CardRank])[source]
Bases:
BlackjackExceptionRaised when length of ranks tuple is not 13.
- exception blackjack21.InvalidSuits(suits: Sequence[CardSuit])[source]
Bases:
BlackjackExceptionRaised when length of suits tuple is not 4.
- exception blackjack21.PlayDealerFailure(name: str)[source]
Bases:
BlackjackExceptionRaised when dealer tries to play their hand before all players have finished.
- exception blackjack21.PlayFailure(name: str, action: str)[source]
Bases:
BlackjackExceptionRaised when player tries to play double down/split when they are not eligible.
- class blackjack21.Player(name: PlayerName, bet: BetAmount)[source]
Bases:
objectPlayer class, containing one or more hands.
- Parameters:
name – str
bet – int
- insert_hand_after(existing: Hand, new_hand: Hand) None[source]
Insert a new hand immediately after an existing one (for splitting).
- property name: PlayerName
Player name.
- class blackjack21.Table(players: Iterable[tuple[str, int]], deck: CardSource, *, dealer_name: str = 'Dealer', hit_soft_17: bool = False, on_round_reset: Callable[[], None] | None = None)[source]
Bases:
objectCreate object for this class to initialize a blackjack table (Iterable through players).
- Parameters:
players – An iterable of player tuples (name, bet).
deck – A card source 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.
on_round_reset – Optional callback invoked when a round resets.
- available_actions() frozenset[Action][source]
Returns the set of legal actions for the current hand.
- 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: CardSource
The table’s card source.