Advertisement
Guest User

blackjack.py

a guest
Feb 17th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. import random
  2.  
  3.  
  4. class Card(object):
  5. RANKS = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
  6.  
  7. SUITS = ('S', 'D', 'H', 'C')
  8.  
  9. def __init__(self, rank=12, suit='S'):
  10. if (rank in Card.RANKS):
  11. self.rank = rank
  12. else:
  13. self.rank = 12
  14.  
  15. if (suit in Card.SUITS):
  16. self.suit = suit
  17. else:
  18. self.suit = 'S'
  19.  
  20. def __str__(self):
  21. if self.rank == 1:
  22. rank = 'A'
  23. elif self.rank == 13:
  24. rank = 'K'
  25. elif self.rank == 12:
  26. rank = 'Q'
  27. elif self.rank == 11:
  28. rank = 'J'
  29. else:
  30. rank = self.rank
  31. return str(rank) + self.suit
  32.  
  33. def __eq__(self, other):
  34. return (self.rank == other.rank)
  35.  
  36. def __ne__(self, other):
  37. return (self.rank != other.rank)
  38.  
  39. def __lt__(self, other):
  40. return (self.rank < other.rank)
  41.  
  42. def __le__(self, other):
  43. return (self.rank <= other.rank)
  44.  
  45. def __gt__(self, other):
  46. return (self.rank > other.rank)
  47.  
  48. def __ge__(self, other):
  49. return (self.rank >= other.rank)
  50.  
  51.  
  52.  
  53. class Deck(object):
  54. def __init__(self):
  55. self.deck = []
  56. for suit in Card.SUITS:
  57. for rank in Card.RANKS:
  58. card = Card(rank, suit)
  59. self.deck.append(card)
  60.  
  61. def shuffle(self):
  62. random.shuffle(self.deck)
  63.  
  64. def deal(self):
  65. if len(self.deck) == 0:
  66. return None
  67. else:
  68. return self.deck.pop(0)
  69.  
  70.  
  71.  
  72. class Player(object):
  73. # cards is a list of card objects
  74. def __init__(self, cards):
  75. self.cards = cards
  76.  
  77. def hit(self, card):
  78. self.cards.append(card)
  79.  
  80. def getPoints(self):
  81. count = 0
  82. for card in self.cards:
  83. if card.rank > 9:
  84. count += 10
  85. elif card.rank == 1:
  86. count += 11
  87. else:
  88. count += card.rank
  89.  
  90. # deduct 10 if Ace is there and needed as 1
  91. for card in self.cards:
  92. if count <= 21:
  93. break
  94. elif card.rank == 1:
  95. count = count - 10
  96.  
  97. return count
  98.  
  99. # does the player have 21 points or not
  100. def hasBlackjack(self):
  101. return len(self.cards) == 2 and self.getPoints() == 21
  102.  
  103. # complete the code so that the cards and points are printed
  104. def __str__(self):
  105. clist = ''
  106. for c in self.cards:
  107. clist = clist + ' ' + str(c)
  108. return 'Cards: ' + clist + ' - ' + str(self.getPoints())+' Points'
  109.  
  110.  
  111. # Dealer class inherits from the Player class
  112. class Dealer(Player):
  113. def __init__(self, cards):
  114. Player.__init__(self, cards)
  115. self.show_one_card = True
  116.  
  117. # over-ride the hit() function in the parent class
  118. # add cards while points < 17, then allow all to be shown
  119. def hit(self, deck):
  120. self.show_one_card = False
  121. while self.getPoints() < 17:
  122. self.cards.append(deck.deal())
  123.  
  124. # return just one card if not hit yet by over-riding the str function
  125. def __str__(self):
  126. if self.show_one_card:
  127. return str(self.cards[0])
  128. else:
  129. return Player.__str__(self)
  130.  
  131.  
  132. class Blackjack(object):
  133. def __init__(self, numPlayers=1):
  134. self.deck = Deck()
  135. self.deck.shuffle()
  136.  
  137. self.numPlayers = numPlayers
  138. self.Players = []
  139.  
  140. # create the number of players specified
  141. # each player gets two cards
  142. for i in range(self.numPlayers):
  143. self.Players.append(Player([self.deck.deal(), self.deck.deal()]))
  144.  
  145. # create the dealer
  146. # dealer gets two cards
  147. self.dealer = Dealer([self.deck.deal(), self.deck.deal()])
  148.  
  149. def play(self):
  150. # Print the cards that each player has
  151. for i in range(self.numPlayers):
  152. print('Player ' + str(i + 1) + ': ' + str(self.Players[i]))
  153.  
  154. # Print the cards that the dealer has
  155. print('Dealer: ' + str(self.dealer))
  156.  
  157. # Each player hits until he says no
  158. playerPoints = []
  159. for i in range(self.numPlayers):
  160. while True:
  161. choice = input('do you want to hit? [y / n]: ')
  162. if choice in ('y', 'Y'):
  163. (self.Players[i]).hit(self.deck.deal())
  164. points = (self.Players[i]).getPoints()
  165. print('Player ' + str(i + 1) + ': ' + str(self.Players[i]))
  166. if points >= 21:
  167. break
  168. else:
  169. break
  170. playerPoints.append((self.Players[i]).getPoints())
  171.  
  172. # Dealer's turn to hit
  173. self.dealer.hit(self.deck)
  174. dealerPoints = self.dealer.getPoints()
  175. print('Dealer: ' + str(self.dealer) + ' - ' + str(dealerPoints))
  176.  
  177. # determine the outcome; you will have to re-write the code
  178. # it was written for just one player having playerPoints
  179. # do not output result for dealer
  180. for i in range(self.numPlayers):
  181. if dealerPoints > 21:
  182. print('Dealer loses')
  183. elif dealerPoints > playerPoints[i]:
  184. print('Dealer wins')
  185. elif (dealerPoints < playerPoints[i] and playerPoints[i] <= 21):
  186. print('Player wins')
  187. elif dealerPoints == playerPoints[i]:
  188. if self.player.hasBlackjack() and not self.dealer.hasBlackjack():
  189. print('Player wins')
  190. elif not self.player.hasBlackjack() and self.dealer.hasBlackjack():
  191. print('Dealer wins')
  192. else:
  193. print('There is a tie')
  194.  
  195.  
  196. def main():
  197. numPlayers = eval(input('Enter number of players: '))
  198. while (numPlayers < 1 or numPlayers > 6):
  199. numPlayers = eval(input('Enter number of players: '))
  200. game = Blackjack(numPlayers)
  201. game.play()
  202.  
  203.  
  204. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement