Skip to content

Instantly share code, notes, and snippets.

@kyanny
Created Aug 6, 2021
Embed
What would you like to do?
def should_hit(dealer_total, player_total, player_low_aces, player_high_aces):
"""Return True if the player should hit (request another card) given the current game
state, or False if the player should stay.
When calculating a hand's total value, we count aces as "high" (with value 11) if doing so
doesn't bring the total above 21, otherwise we count them as low (with value 1).
For example, if the player's hand is {A, A, A, 7}, we will count it as 11 + 1 + 1 + 7,
and therefore set player_total=20, player_low_aces=2, player_high_aces=1.
"""
if dealer_total >= player_total or player_total <= 12:
if player_high_aces == 0:
if player_low_aces <= 0:
if player_total - ((player_low_aces * 1) + (player_high_aces * 11)) > 6:
return True
return False
q7.simulate(n_games=50000)
# win rate avg 41.2% ~ 41.3%
# win rate highest 41.7%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment