Design an online blackjack game. Players bet, get two cards, and choose to hit or stand against a dealer who follows fixed rules. Closest to 21 without going over wins.
A **deck** (or several) of 52 cards is shuffled into a **shoe**.
Each player places a **bet**, then gets two cards; the dealer gets two with one hidden.
A player can **hit** or **stand**; over 21 is a **bust**.
The dealer draws until **17 or more**, then results are settled against each player.
An **ace** counts as 11 or 1, whichever helps without busting.
Stage 1: Cards, shoe and hand
Model the cards and how a hand is valued.
Blackjack's one tricky rule is the ace. Without a place for it, scoring ends up in many places. `Hand.value()` counts every ace as 11, then turns aces into 1 one at a time while the total is over 21. `Shoe` shuffles and deals. The hand owns scoring, so players and the dealer share it without copying. Recomputing the value on every call is fine for 2 to 6 cards; cache only if profiling asks.
Stage 2: Players and the dealer
Give each side its own behaviour.
Who holds the chips and bet, and who draws for the dealer? `Player` has chips, a bet and a hand. `Dealer` has a hand and plays by drawing from the shoe. Neither class shares a hand with the other, so a bug in one side cannot corrupt the other. `Player` and `Dealer` look alike. A shared `Participant` base is reasonable once more than the hand is shared.
Stage 3: The dealer's rule: Strategy
Make the draw rule swappable.
Some tables make the dealer hit on a soft 17. A `DealerRule` with `shouldHit(hand)`. `StandOn17` implements it and the dealer asks the rule each time. A casino variant is one new class (**Strategy**); the dealer's loop is untouched. Only one rule exists today. The interface is still worth it because the variants are well known.
Stage 4: A table and the round: State
Run a round from betting to settling.
Nothing stops a player hitting during betting or betting after the cards are dealt. `Table` holds a `RoundState`. Each state decides which actions are legal, and `SettlingState` pays winners. Illegal actions are refused by the state instead of by a pile of flags (**State**). Dealing, player-turn and dealer-turn states are omitted here for space. They follow the same shape.