Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. #!/python3
  2.  
  3. import os
  4. import random
  5. import sys
  6.  
  7.  
  8. deck = ["Ace", 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"]*4
  9.  
  10. def deal(deck):
  11. hand = []
  12. for x in range(2):
  13. random.shuffle(deck)
  14. card = deck.pop()
  15. hand.append(card)
  16. return hand #end of a function
  17.  
  18. def play_again():
  19. again = input("Play with me daddy. (Y/N) : ").lower()
  20. if again == "y":
  21. dealer_hand = []
  22. player_hand= []
  23. deck = deck = ["Ace", 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"]*4
  24. game()
  25. else:
  26. sys.exit("Come again soon daddy!")
  27.  
  28. def total(hand):
  29. total = 0
  30. for card in hand:
  31. if card == "Jack" or card == "Queen" or card == "King":
  32. total+= 10
  33. elif card == "Ace":
  34. if total >= 11:
  35. total+= 1
  36. else:
  37. total+= 11
  38. else:
  39. total += card
  40. return total
  41.  
  42. def spankme(hand):
  43. card = deck.pop()
  44. hand.append(card)
  45. return hand
  46.  
  47. def clear():
  48. if os.name == 'nt':
  49. os.system('CLS')
  50. if os.name == 'posix':
  51. os.system('clear')
  52.  
  53. def print_results(dealer_hand, player_hand):
  54. clear()
  55. print("The dealer has a " + str(dealer_hand) + " for a total of " + str(total(dealer_hand)))
  56. print("You have a " + str(player_hand) + " for a total of " + str(total(player_hand)))
  57.  
  58. def blackjack(dealer_hand, player_hand):
  59. if total(player_hand) == 21:
  60. print_results(dealer_hand, player_hand)
  61. print("Congratulations daddy! You got a Blackjack!\n")
  62. play_again()
  63. elif total(dealer_hand) == 21:
  64. print_results(dealer_hand, player_hand)
  65. print("Sorry daddy, better luck next time!.\n")
  66. play_again()
  67.  
  68. def score(dealer_hand, player_hand):
  69. if total(player_hand) == 21:
  70. print_results(dealer_hand, player_hand)
  71. print("Congratulations! You got a Blackjack!\n")
  72. elif total(dealer_hand) == 21:
  73. print_results(dealer_hand, player_hand)
  74. print("Sorry, you lose. The dealer got a blackjack.\n")
  75. elif total(player_hand) > 21:
  76. print_results(dealer_hand, player_hand)
  77. print("Sorry. You busted a nut. You lose.\n")
  78. elif total(dealer_hand) > 21:
  79. print_results(dealer_hand, player_hand)
  80. print("Dealer busted a nut. You win!\n")
  81. elif total(player_hand) < total(dealer_hand):
  82. print_results(dealer_hand, player_hand)
  83. print("Sorry. Your score isn't higher than the dealer. You lose.\n")
  84. elif total(player_hand) > total(dealer_hand):
  85. print_results(dealer_hand, player_hand)
  86. print("Congratulations. Your score is higher than the dealer. You win\n")
  87.  
  88. def game():
  89. choice = 0
  90. clear()
  91. print("WELCOME TO NEGROJACK!\n")
  92. dealer_hand = deal(deck)
  93. player_hand = deal(deck)
  94. while choice != "q":
  95. print("The dealer is showing a " + str(dealer_hand[0]))
  96. print("You have a " + str(player_hand) + " for a total of " + str(total(player_hand)))
  97. blackjack(dealer_hand, player_hand)
  98. choice = input("Do you want to S[p]ankme, Spankme [T]wice, [S]tand, or [Q]uit: ").lower()
  99. clear()
  100. if choice == "p":
  101. spankme(player_hand)
  102. while total(dealer_hand) < 17:
  103. spankme(dealer_hand)
  104. score(dealer_hand, player_hand)
  105. play_again()
  106. if choice == "t":
  107. spankme(player_hand)
  108. spankme(player_hand)
  109. while total(dealer_hand) < 17:
  110. spankme(dealer_hand)
  111. score(dealer_hand, player_hand)
  112. play_again()
  113. elif choice == "s":
  114. while total(dealer_hand) < 17:
  115. spankme(dealer_hand)
  116. score(dealer_hand, player_hand)
  117. play_again()
  118. elif choice == "q":
  119. print("Bye!")
  120. exit()
  121.  
  122. if __name__ == "__main__":
  123. game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement