fork download
  1. SHOE_SIZE = 6
  2. STAND_ON_SOFT_SEVENTEEN = True
  3.  
  4. def blackjack_score(hand):
  5. soft = sum(hand) < 12 and 1 in hand
  6. return sum(hand) + (10 if soft else 0), soft
  7.  
  8. def busted(hand):
  9. return blackjack_score(hand)[0] > 21
  10.  
  11. def stand(hand):
  12. score, soft = blackjack_score(hand)
  13. return score > 17 or (score == 17 and (STAND_ON_SOFT_SEVENTEEN or not soft))
  14.  
  15. def hand_probability(hand):
  16. total_cards = 52 * SHOE_SIZE
  17. value_counts = [SHOE_SIZE * rank for rank in [0,4,4,4,4,4,4,4,4,4,16]]
  18. numerator = 1
  19. denominator = 1
  20. for card in hand:
  21. numerator *= value_counts[card]
  22. denominator *= total_cards
  23. value_counts[card] -= 1
  24. total_cards -= 1
  25. return numerator/denominator
  26.  
  27. cards = list(range(1,11))
  28.  
  29. initial_hands = [(c,d) for c in cards for d in cards]
  30. hits = []
  31. stands = []
  32. busts = []
  33.  
  34. for hand in initial_hands:
  35. if stand(hand):
  36. stands.append(hand)
  37. else:
  38. hits.append(hand)
  39.  
  40. while(hits):
  41. old_hand = hits.pop()
  42. for card in cards:
  43. new_hand = old_hand + (card,)
  44. if busted(new_hand):
  45. busts.append(new_hand)
  46. elif stand(new_hand):
  47. stands.append(new_hand)
  48. else:
  49. hits.append(new_hand)
  50.  
  51. print(sum(hand_probability(hand) for hand in stands))
  52. print(sum(hand_probability(hand) for hand in busts))
  53. print(len(stands))
  54. print(len(busts))
Success #stdin #stdout 0.22s 27800KB
stdin
Standard input is empty
stdout
0.7180786583295092
0.28192134167069766
30109
24324