Advertisement
Guest User

blackjack.py

a guest
Oct 23rd, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. print("#####################")
  2. print("# BlackJack #")
  3. print("# Matthew #")
  4. print("#####################")
  5.  
  6. import random, player1
  7. suits = ['\u2660','\u2661','\u2662','\u2663']
  8. ranks = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
  9.  
  10. def shuffledDeck():
  11. deck=[]
  12. for suit in suits:
  13. for rank in ranks:
  14. deck.append(rank+ '' +suit)
  15. random.shuffle(deck)
  16. return deck
  17.  
  18.  
  19. def dealCard(deck, hand):
  20. deal=deck.pop()
  21. hand.append(deal)
  22.  
  23.  
  24. def total(hand):
  25. values = {'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'1':10,
  26. 'J':10,'Q':10,'K':10,'A':11}
  27. result = 0
  28. numAces = 0
  29.  
  30. for card in hand:
  31. result += values[card[0]]
  32. if card == 'A':
  33. numAces + 1
  34. return numAces
  35.  
  36. while result > 21 and numAces>0:
  37. result - 10, numAces - 1
  38.  
  39. return result
  40.  
  41.  
  42. def compareHands(house,player):
  43.  
  44. if house > player:
  45. currentBet = player1.GetBet(currentBet, None, False)
  46. print(currentBet)
  47. print("you lose")
  48. print("___________________________")
  49. elif house < player:
  50. currentBet = player1.GetBet(currentBet, None, True)
  51. print(currentBet)
  52. print("YOU WIN")
  53. print("___________________________")
  54. elif house==21 and len(house_hand)==2 and len(player_hand)>2:
  55. currentBet = player1.GetBet(currentBet, None, False)
  56. print(currentBet)
  57. print("you lose")
  58. print("___________________________")
  59. elif player==21 and len(player_hand)==2 and len(house_hand)>2:
  60. currentBet = player1.GetBet(currentBet, None, True)
  61. print(currentBet)
  62. print("YOU WIN")
  63. print("___________________________")
  64. else:
  65. currentBet = player1.GetBet(currentBet, None, None)
  66. print(currentBet)
  67. print("Tie")
  68. print("___________________________")
  69.  
  70. def printHand(hand):
  71. hand = " ".join(hand)
  72. return hand
  73.  
  74.  
  75. def blackjack():
  76.  
  77. deck = shuffledDeck()
  78. house_hand = []
  79. player_hand = []
  80. househand =printHand(house_hand)
  81. playerhand = printHand(player_hand)
  82.  
  83. for i in range(2):
  84. dealCard(deck, player_hand)
  85. dealCard(deck, house_hand)
  86. houseScore = total(house_hand)
  87.  
  88. while houseScore < 17:
  89. dealCard(deck, house_hand)
  90. houseScore = total(house_hand)
  91.  
  92. if houseScore > 21:
  93. househand =printHand(house_hand)
  94. playerhand =printHand(player_hand)
  95. currentBet = player1.GetBet(currentBet, None, True)
  96. print(currentBet)
  97. print("House Hand:" + househand)
  98. print("Player Hand:" + playerhand)
  99. print("House Busted")
  100. print("___________________________")
  101. return
  102. else:
  103. househand =printHand(house_hand)
  104. playerhand =printHand(player_hand)
  105. print("House Hand:" + househand)
  106. print("Player Hand:" + playerhand)
  107. playerScore = total(player_hand)
  108.  
  109. if houseScore == 21:
  110. currentBet = player1.GetBet(currentBet, None, False)
  111. print(currentBet)
  112. print("House Score: " + str(houseScore))
  113. print ("you lose!")
  114. print("___________________________")
  115. return
  116.  
  117. while playerScore < 21:
  118. print("Player Score: " + str(playerScore))
  119. hit = player1.HitStand(playerScore, houseScore)
  120.  
  121. if hit == 'y':
  122. dealCard(deck, player_hand)
  123. playerScore = total(player_hand)
  124. playerhand =printHand(player_hand)
  125. print("New Player Hand:" + playerhand)
  126. print("House Score: " + str(houseScore))
  127. else:
  128.  
  129. compareHands(houseScore, playerScore)
  130. return
  131.  
  132. if playerScore == 21:
  133. print("Player Score: " + str(playerScore))
  134. compareHands(houseScore, playerScore)
  135. return
  136. else:
  137. currentBet = player1.GetBet(currentBet, None, False)
  138. print(currentBet)
  139. print("Player Score: " + str(playerScore))
  140. print("Over 21! You Lose")
  141. print("___________________________")
  142. return
  143.  
  144. def playgame():
  145. for i in range(15):
  146. blackjack()
  147. playgame()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement