Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. import random
  2.  
  3.  
  4. class Deck:
  5. def __init__(self, seed=None):
  6. self.cards = [i for i in range(1, 10)] * 4 + [10] * 16
  7. random.seed(seed)
  8. random.shuffle(self.cards)
  9.  
  10. def deal(self, start, n):
  11. return self.cards[start:start + n]
  12.  
  13.  
  14. class Player:
  15. def __init__(self, hand):
  16. self.hand = hand
  17. self.total = 0
  18.  
  19. def deal(self, cards):
  20. self.hand.extend(cards)
  21. self.total = sum(self.hand)
  22.  
  23.  
  24. def cmp(x, y):
  25. return (x > y) - (x < y)
  26.  
  27.  
  28. def play(deck, start, scores):
  29. player = Player(deck.deal(start, 2))
  30. dealer = Player(deck.deal(start + 2, 2))
  31. results = []
  32.  
  33. for i in range(49 - start):
  34. count = start + 4
  35. player.deal(deck.deal(count, i))
  36. count += i
  37.  
  38. if player.total > 21:
  39. results.append((-1, count))
  40. break
  41.  
  42. while dealer.total < 17 and count < 52:
  43. dealer.deal(deck.deal(count, 1))
  44. count += 1
  45. if dealer.total > 21:
  46. results.append((1, count))
  47. else:
  48. results.append((cmp(player.total, dealer.total), count))
  49.  
  50. options = []
  51. for score, next_start in results:
  52. options.append(score +
  53. scores[next_start] if next_start <= 48 else score)
  54. scores[start] = max(options)
  55.  
  56.  
  57. def blackjack(seed=None):
  58. deck = Deck(seed)
  59. scores = [0 for _ in range(52)]
  60.  
  61. for start in range(48, -1, -1):
  62. play(deck, start, scores)
  63.  
  64. return scores[0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement