The mechanics of play generally work as follows. The details can vary, but the outline is similar:
- First, the player and dealer each get two cards. The player, of course, knows the value of both of their cards. They're dealt face up in a casino.
- One of the dealer's cards is revealed to the player. It's displayed face up. The player, therefore, knows a little bit about the dealer's hand, but not everything. This is typical of more complex simulations where partial information is available and statistical modeling is required to make appropriate decisions.
- If the dealer has an Ace showing, the player is offered the opportunity to place an additional insurance bet. This is a special case, and is typical of more complex simulations where there are exceptions.
- For the balance of the game, the player can elect to receive cards, or stop receiving cards. There are four choices available:
- The player can hit, which means take another card.
- They player can or stand or stand pat with the cards dealt.
- If the player's cards match, the hand can be split. This entails an additional bet, and the two hands are played separately.
- The player can double their bet before taking one last card. This is called doubling down.
The final evaluation of the hand works as follows:
- If the player went over 21, the hand is a bust, the player loses, and the dealer's face-down card is irrelevant. This provides an advantage to the dealer.
- If the player's total is 21 or under, then the dealer takes cards according to a simple, fixed rule. The dealer must hit a hand that totals less than 18; the dealer must stand on a hand that totals 18 or more.
- If the dealer goes bust, the player wins.
- If both the dealer and player are 21 or under, the hands are compared. The higher total is the winner. In the event of a tie, the game is a push, neither a win nor a loss. If the player wins with 21, they win a larger payout, usually 1.5 times the bet.
The rules can vary quite a bit. We'll elide these details to focus on the Python code required for simulation.