Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.35 KB | None | 0 0
  1. """
  2. AI BLACKJACK GAME
  3. AI VS COMPUTER
  4.  
  5.  
  6.  
  7.  
  8. """
  9. import random
  10.  
  11. ##############################################################################################################
  12.  
  13. ace = 11
  14. king = 10
  15. queen = 10
  16. jack = 10
  17.  
  18. deck = [ace,king,queen,jack,10,9,8,7,6,5,4,3,2,ace,king,queen,jack,10,9,8,7,6,5,4,3,2,ace,king,queen,jack,10,9,8,7,6,5,4,3,2,ace,king,queen,jack,10,9,8,7,6,5,4,3,2]
  19. count = 0
  20.  
  21. ##############################################################################################################
  22.  
  23. def Menu():
  24.         print("\n=== AI BLACKJACK MENU ===")
  25.         print("Type 'play' to play BlackJack!")
  26.         print("Type 'balance' to check your balance!")
  27.         print("Type 'stats' to view your wins and losses!")
  28.         print("Type 'credits' to view the credits of the game!\n")
  29.         answer = input("")
  30.         return answer
  31.  
  32. ##############################################################################################################
  33.  
  34. money = 10000
  35.        
  36. #bet = input("How much would you like to bet: $")
  37.  
  38.  
  39. wins = 0
  40. losses = 0
  41.  
  42. class BlackJack:
  43.     def __init__(self):
  44.         z = 0
  45.         self.hand = []
  46.         self.hand.append(deck.pop(random.randint(0,len(deck)-1)))
  47.         self.hand.append(deck.pop(random.randint(0,len(deck)-1)))
  48.         #self.cardone = x
  49.         #self.cardtwo = y
  50.         self.total = self.calc_total()
  51.  
  52.    
  53.     def __repr__(self):
  54.         result = ""
  55.         for i, item in enumerate(self.hand):
  56.             result += str(item)
  57.             if not(i == len(self.hand) -1):
  58.                 result += " and "
  59.         return ("\nCards drawn: " + result)
  60.    
  61.     def hit(self):
  62.         z = deck.pop(random.randint(0,len(deck)-1))
  63.         self.hand.append(z)
  64.         self.calc_total()
  65.         return z
  66.  
  67.     def calc_total(self):
  68.         total = 0
  69.         for item in self.hand:
  70.             total += item
  71.         return total
  72.  
  73. ##############################################################################################################
  74.  
  75. def check_count():
  76.     count = 0
  77.     for item in deck:
  78.         if item > 1 and item < 7:
  79.             count -= 1
  80.         if item > 9:
  81.             count += 1
  82.     return count
  83.        
  84.        
  85.  
  86. ##############################################################################################################
  87.  
  88.  
  89.  
  90. while True:
  91.     choice = Menu()
  92.     if choice == "play":
  93.  
  94.  
  95. ##############################################################################################################
  96.  
  97.         x = 0
  98.         while x < 100:
  99.            
  100.             if len(deck) < 10:
  101.                 deck = [ace,king,queen,jack,10,9,8,7,6,5,4,3,2,ace,king,queen,jack,10,9,8,7,6,5,4,3,2,ace,king,queen,jack,10,9,8,7,6,5,4,3,2,ace,king,queen,jack,10,9,8,7,6,5,4,3,2]
  102.                 count = 0
  103.            
  104.             print("\n============= You Go First ! =============\n")
  105.  
  106.             if money == 0:
  107.                 print("The casino has gifted you an extra $100\nUse it wisely!\n")
  108.                 money += 100
  109.  
  110.             print("Your balance is: " + str(money) + "$\n")
  111.  
  112.  
  113.             count = check_count()
  114.  
  115.  
  116.             if count <= 0:
  117.                 bet = 75
  118.                 print("The count is " + str(count) + " so we'll bet " + str(bet) + "$")
  119.             if count < -3:
  120.                 bet = 50
  121.                 print("The count is " + str(count) + " so we'll bet " + str(bet) + "$")
  122.             if count < -5:
  123.                 bet = 25
  124.                 print("The count is " + str(count) + " so we'll bet " + str(bet) + "$")
  125.             if count > 0:
  126.                 bet = 100
  127.                 bet = count*bet
  128.                 print("The count is " + str(count) + " so we'll bet " + str(bet) + "$")
  129.  
  130.            
  131.  
  132.  
  133.             player = BlackJack()
  134.            
  135.             dealer = BlackJack()
  136.        
  137.            
  138.             print(player)   # Prints user cards
  139.            
  140.             print("Your total is: " + str(player.calc_total()))  # Prints total cards user has before hit/pass
  141.            
  142.             if player.calc_total() < 16:
  143.                 hitorpass = "HIT"
  144.             else:
  145.                 hitorpass = "PASS"
  146.            
  147.            
  148.             while True:
  149.                 if hitorpass == "HIT":
  150.                     print("Your next card is: " + str(player.hit()))   # Draws user new card
  151.                     print("Your total is: " + str(player.calc_total()))
  152.                     if player.calc_total() < 16 and count < 0:
  153.                         hitorpass == "HIT"
  154.                         continue
  155.                     elif player.calc_total() >= 17:
  156.                         hitorpass == "PASS"
  157.                     elif player.calc_total() > 21 and 11 in player.hand:
  158.                         print("Your ace changes to a 1 because you have over 21")
  159.                         for a,b in enumerate(player.hand):
  160.                             if b == 11:
  161.                                 player.hand[a] = 1
  162.                                 print("Your new total is: " + str(player.calc_total()))
  163.                                 break
  164.                     if player.calc_total() < 18:
  165.                         for a,b in enumerate(player.hand):
  166.                             if b == 11:
  167.                                 player.hand[a] = 1
  168.                                 continue
  169.                     if player.calc_total() <= 16:
  170.                         hitorpass == "HIT"
  171.                         continue
  172.                     if player.calc_total() > 21:
  173.                         print("\nYou Busted! The Dealer Wins! ×")
  174.                         break
  175.                     else:
  176.                         break
  177.                 elif hitorpass == "1":
  178.                     break
  179.                 else:
  180.                     print("\n============= Dealer's Turn! =============\n")
  181.                     break
  182.             if hitorpass == "1":
  183.                 break
  184.             if player.calc_total() < 22:
  185.                 print(dealer)
  186.                 print("Dealer total is: " + str(dealer.calc_total()))
  187.            
  188.            
  189.            
  190.             while dealer.calc_total() < 22 and dealer.calc_total() <= 16 and player.calc_total() < 22:
  191.                 while dealer.calc_total() <= 16:
  192.                     print("Dealer drew: " + str(dealer.hit()) + "\nDealer total is: " + str(dealer.calc_total()))
  193.            
  194.            
  195.            
  196.             if dealer.calc_total() <= 17 and 11 in dealer.hand:
  197.                 print("Dealer drew: " + str(dealer.hit()) + "\nDealer total is: " + str(dealer.calc_total()))
  198.  
  199.  
  200.  
  201.             while True:
  202.                 if dealer.calc_total() > 21 and 11 in dealer.hand:
  203.                     print("Dealer's ace changes to a 1 because they have over 21")
  204.                     for a,b in enumerate(dealer.hand):
  205.                         if b == 11:
  206.                             dealer.hand[a] = 1
  207.                             print("Dealer total is: " + str(dealer.calc_total()))
  208.                             break
  209.                 break
  210.  
  211.  
  212.             if player.calc_total() == 21:
  213.                 wins += 1
  214.                 money += (bet*1.5)
  215.                 print("You got BlackJack! You Win! ✔")
  216.             elif dealer.calc_total() < 22 and dealer.calc_total() > player.calc_total() and player.calc_total() < 22 and dealer.calc_total() > 16:
  217.                 print("\nThe Dealer Has Won! ×")
  218.                 losses += 1
  219.                 money -= bet
  220.             elif player.calc_total() >= 22:
  221.                 ""
  222.                 losses += 1
  223.                 money -= bet
  224.             elif dealer.calc_total() >= 22:
  225.                 print("\nThe Dealer Busted! You Win! ✔")
  226.                 wins += 1
  227.                 money += bet
  228.             else:
  229.                 print("\nYou won! ✔")
  230.                 wins += 1
  231.                 money += bet
  232.             x += 1    
  233.             print("\n\nx-x-x-x-x-x-x   ℕ𝔼𝕎 𝔾𝔸𝕄𝔼 !   x-x-x-x-x-x-x\n              Type 1 to Quit")
  234.  
  235.  
  236. #######################################################################################################
  237.  
  238.  
  239.     elif choice == "balance":
  240.         print("\nBalance: $" + str(money) + "\n")
  241.     elif choice == "stats":
  242.         print("\nWins: " + str(wins) + "\nLosses: " + str(losses) + "\n")
  243.     elif choice == "credits":
  244.         print("\nDeveloped and created by Nathan Corwin-Shah, 2019")
  245.         print("Mr. Florido helped too.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement