Guest User

Untitled

a guest
Mar 21st, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. import random
  2.  
  3. values = ['A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K']
  4. suits = ['card:Spades', 'card:Hearts', 'card:Clubs', 'card: Diamonds']
  5.  
  6.  
  7. class Card(object):
  8. def __init__(self, number, face, value, back='card:BackRed4'):
  9. self.number = number
  10. self.face = face
  11. self.value = value
  12. self.back = back
  13.  
  14. def __repr__(self):
  15. return f"Card(number = {self.number}, face = {self.face}, value = {self.value}, back = {self.back})"
  16.  
  17. def __str__(self):
  18. return str(self.face)
  19.  
  20. def __add__(self, other):
  21. return self.value + other.value
  22.  
  23. def __radd__(self, other):
  24. return other + self.value
  25.  
  26.  
  27. #card contains 4 attributes: number, string for rendering image,
  28. #numeric value: 10 if face card, 11 if 'Ace' otherwise value is same
  29. #as number, string for rendering the back of the card.
  30.  
  31. #Create a full deck (52 cards) with list comprehension
  32.  
  33. cards = [
  34. Card(n, ("'" + suit + str(n) + "'"), 10 if n in ['J', 'Q', 'K'] else 11
  35. if n == 'A' else n) for n in values for suit in suits
  36. ]
  37.  
  38. cards = list(cards)
  39. random.shuffle(cards)
  40.  
  41.  
  42. def nextCard():
  43. return cards.pop()
  44.  
  45.  
  46. #print(cards[4])
  47. '''for card in cards:
  48. print(card, card.number, card.value)'''
  49.  
  50.  
  51. class Hand(object):
  52.  
  53. hit = True
  54. stand = False
  55. doubleOn = [9, 10, 11]
  56. total = []
  57.  
  58. def __init__(self, player = True):
  59. self.player = True
  60.  
  61. def handValue(self):
  62. return sum(self.total)
  63.  
  64. def reachedTwentyOne(self):
  65. if (sum(self.total) > 21):
  66. self.hit = False
  67. #lose bet, do next hand
  68. elif (sum(self.total) == 21) and (any(x.value != 11 for x in self.total)):
  69. self.hit = False
  70. #dealer move
  71.  
  72. def blackjackCheck(self):
  73. if (len(self.total) == 2) and (sum(self.total) == 21):
  74. hit = False
  75. if self.player == True:
  76. #check if push, pay player bet times blackjack bonus
  77. print('Player Blackjack')
  78. else:
  79. #do something with insurance or whatever
  80. print('Dealer Blackjack')
  81.  
  82. def add(self, draw):
  83. if (draw.value == 11) and (any(x.value == 11 for x in self.total)):
  84. draw.value = 1
  85. self.total.append(draw)
  86. elif (draw.value == 11) and (sum(self.total) + draw.value > 21):
  87. draw.value = 1
  88. self.total.append(draw)
  89. elif (draw.value + sum(self.total) > 21) and (any(
  90. x.value == 11 for x in self.total)):
  91. for object in self.total:
  92. if object.value == 11:
  93. object.value = 1
  94. self.total.append(draw)
  95. else:
  96. self.total.append(draw)
  97.  
  98. def playerHit(self):
  99. self.reachedTwentyOne()
  100. if self.hit == True:
  101. self.add(cards.pop())
  102. self.blackjackCheck()
  103. print(sum(self.total))
  104. if (sum(self.total) > 21):
  105. print('Busted!')
  106. self.hit = False
  107. else:
  108. print('Not allowed to hit')
  109.  
  110. def playerStand(self):
  111. self.hit = False
  112. self.stand = True
  113. #dealer move
  114.  
  115. def doubleDown(self):
  116. if self.handValue() in self.doubleOn:
  117. #double bet
  118. print(f'doubling down on {sum(self.total)}')
  119. self.playerHit()
  120. self.playerStand()
  121. else:
  122. print(f'not allowed to double down on {sum(self.total)}')
  123.  
  124. def playerSplit(self):
  125. if self.total[0].value == self.total[1].value:
  126. playerHand2 = self.__class__()
  127. playerHand2.add(self.total[1])
  128. else:
  129. print(f'Split not allowed on {self.total[0].number} and {self.total[1].number}')
  130.  
  131. class Round():
  132. def __init__(self, player, bet, dealer):
  133. self.player = player
  134. self.bet = bet
  135. self.dealer = dealer
  136.  
  137. def newRound(self):
  138. pass
  139.  
  140.  
  141. playerHand = Hand()
  142. #dealerHand = Hand()
  143.  
  144. #playerHand.add(nextCard())
  145. #dealerHand.add(nextCard())
  146. #playerHand.add(nextCard())
  147. #dealerHand.add(nextCard())
  148. playerHand.playerHit()
  149. playerHand.playerHit()
  150.  
  151.  
  152.  
  153. '''for card in playerHand.total:
  154. if card.value == 11:
  155. print('found one')'''
  156.  
  157.  
  158.  
  159. #print(repr(cards[0]))
  160.  
  161. #print(eval('cards[0]'))
Add Comment
Please, Sign In to add comment