KatInterview

Design a Blackjack Game Engine

Data Structure DesignModel answer
MediumModel answerData Structure DesignData Structure DesignObject-Oriented DesignState Machine

Last updated

Architect an object-oriented Blackjack engine handling multi-deck shoes, split hands, and soft/hard total calculations.

Design the core object-oriented components for a Blackjack engine. Your design must handle multi-deck shoes, rule-compliant score evaluation, splitting hands, and overall game lifecycle management.

Requirements

  1. Card and Multi-Deck Shoe

    • Model cards with standard ranks ('A', '2'–'10', 'J', 'Q', 'K') and suits.
    • Implement a Shoe that accepts a configurable number of 52-card decks. It must support shuffling, dealing, and detecting when it drops below a penetration threshold (e.g., 75% depleted) to signal a reshuffle.
  2. Hand Dynamics and Scoring

    • Calculate hand scores accurately. Aces count as either 1 or 11. A hand is considered soft if an Ace can be evaluated as 11 without exceeding a total of 21; otherwise, it is hard.
    • Differentiate a natural Blackjack (an initial 2-card total of 21 on an un-split hand) from a score of 21 achieved via hits or after splitting.
    • Implement a split method on a hand containing two cards of matching rank. Splitting creates two separate hands, each taking one of the original cards and immediately drawing a new card from the shoe.
  3. Game State Management

    • Design a coordinator class (Game Engine or Table) that maintains active player hands and the dealer's hand.
    • Handle active hand navigation when a player splits, stands, or busts. Ensure split hands are played sequentially.

Example Interaction

shoe = Shoe(num_decks=6)
initial_hand = Hand(cards=[Card(rank='8', suit='H'), Card(rank='8', suit='D')])

if initial_hand.can_split:
    hand1, hand2 = initial_hand.split(shoe)
    # hand1 and hand2 each contain one original '8' and one fresh card drawn from the shoe
    # both hand1 and hand2 have their `from_split` flag set to True

Constraints & Edge Cases

  • Shoe Depletion: What happens if the shoe runs out of cards mid-hand versus between rounds?
  • Multiple Aces: How does your scoring logic handle hands with multiple Aces (e.g., A-A, A-A-9, A-A-9-K)?
  • Splitting Rules: Can a hand created from a split achieve a natural Blackjack? How does splitting update the list of active hands under turn control?