Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- AI BLACKJACK GAME
- AI VS COMPUTER
- """
- import random
- ##############################################################################################################
- ace = 11
- king = 10
- queen = 10
- jack = 10
- 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]
- count = 0
- ##############################################################################################################
- def Menu():
- print("\n=== AI BLACKJACK MENU ===")
- print("Type 'play' to play BlackJack!")
- print("Type 'balance' to check your balance!")
- print("Type 'stats' to view your wins and losses!")
- print("Type 'credits' to view the credits of the game!\n")
- answer = input("")
- return answer
- ##############################################################################################################
- money = 10000
- #bet = input("How much would you like to bet: $")
- wins = 0
- losses = 0
- class BlackJack:
- def __init__(self):
- z = 0
- self.hand = []
- self.hand.append(deck.pop(random.randint(0,len(deck)-1)))
- self.hand.append(deck.pop(random.randint(0,len(deck)-1)))
- #self.cardone = x
- #self.cardtwo = y
- self.total = self.calc_total()
- def __repr__(self):
- result = ""
- for i, item in enumerate(self.hand):
- result += str(item)
- if not(i == len(self.hand) -1):
- result += " and "
- return ("\nCards drawn: " + result)
- def hit(self):
- z = deck.pop(random.randint(0,len(deck)-1))
- self.hand.append(z)
- self.calc_total()
- return z
- def calc_total(self):
- total = 0
- for item in self.hand:
- total += item
- return total
- ##############################################################################################################
- def check_count():
- count = 0
- for item in deck:
- if item > 1 and item < 7:
- count -= 1
- if item > 9:
- count += 1
- return count
- ##############################################################################################################
- while True:
- choice = Menu()
- if choice == "play":
- ##############################################################################################################
- x = 0
- while x < 100:
- if len(deck) < 10:
- 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]
- count = 0
- print("\n============= You Go First ! =============\n")
- if money == 0:
- print("The casino has gifted you an extra $100\nUse it wisely!\n")
- money += 100
- print("Your balance is: " + str(money) + "$\n")
- count = check_count()
- if count <= 0:
- bet = 75
- print("The count is " + str(count) + " so we'll bet " + str(bet) + "$")
- if count < -3:
- bet = 50
- print("The count is " + str(count) + " so we'll bet " + str(bet) + "$")
- if count < -5:
- bet = 25
- print("The count is " + str(count) + " so we'll bet " + str(bet) + "$")
- if count > 0:
- bet = 100
- bet = count*bet
- print("The count is " + str(count) + " so we'll bet " + str(bet) + "$")
- player = BlackJack()
- dealer = BlackJack()
- print(player) # Prints user cards
- print("Your total is: " + str(player.calc_total())) # Prints total cards user has before hit/pass
- if player.calc_total() < 16:
- hitorpass = "HIT"
- else:
- hitorpass = "PASS"
- while True:
- if hitorpass == "HIT":
- print("Your next card is: " + str(player.hit())) # Draws user new card
- print("Your total is: " + str(player.calc_total()))
- if player.calc_total() < 16 and count < 0:
- hitorpass == "HIT"
- continue
- elif player.calc_total() >= 17:
- hitorpass == "PASS"
- elif player.calc_total() > 21 and 11 in player.hand:
- print("Your ace changes to a 1 because you have over 21")
- for a,b in enumerate(player.hand):
- if b == 11:
- player.hand[a] = 1
- print("Your new total is: " + str(player.calc_total()))
- break
- if player.calc_total() < 18:
- for a,b in enumerate(player.hand):
- if b == 11:
- player.hand[a] = 1
- continue
- if player.calc_total() <= 16:
- hitorpass == "HIT"
- continue
- if player.calc_total() > 21:
- print("\nYou Busted! The Dealer Wins! ×")
- break
- else:
- break
- elif hitorpass == "1":
- break
- else:
- print("\n============= Dealer's Turn! =============\n")
- break
- if hitorpass == "1":
- break
- if player.calc_total() < 22:
- print(dealer)
- print("Dealer total is: " + str(dealer.calc_total()))
- while dealer.calc_total() < 22 and dealer.calc_total() <= 16 and player.calc_total() < 22:
- while dealer.calc_total() <= 16:
- print("Dealer drew: " + str(dealer.hit()) + "\nDealer total is: " + str(dealer.calc_total()))
- if dealer.calc_total() <= 17 and 11 in dealer.hand:
- print("Dealer drew: " + str(dealer.hit()) + "\nDealer total is: " + str(dealer.calc_total()))
- while True:
- if dealer.calc_total() > 21 and 11 in dealer.hand:
- print("Dealer's ace changes to a 1 because they have over 21")
- for a,b in enumerate(dealer.hand):
- if b == 11:
- dealer.hand[a] = 1
- print("Dealer total is: " + str(dealer.calc_total()))
- break
- break
- if player.calc_total() == 21:
- wins += 1
- money += (bet*1.5)
- print("You got BlackJack! You Win! ✔")
- elif dealer.calc_total() < 22 and dealer.calc_total() > player.calc_total() and player.calc_total() < 22 and dealer.calc_total() > 16:
- print("\nThe Dealer Has Won! ×")
- losses += 1
- money -= bet
- elif player.calc_total() >= 22:
- ""
- losses += 1
- money -= bet
- elif dealer.calc_total() >= 22:
- print("\nThe Dealer Busted! You Win! ✔")
- wins += 1
- money += bet
- else:
- print("\nYou won! ✔")
- wins += 1
- money += bet
- x += 1
- print("\n\nx-x-x-x-x-x-x ℕ𝔼𝕎 𝔾𝔸𝕄𝔼 ! x-x-x-x-x-x-x\n Type 1 to Quit")
- #######################################################################################################
- elif choice == "balance":
- print("\nBalance: $" + str(money) + "\n")
- elif choice == "stats":
- print("\nWins: " + str(wins) + "\nLosses: " + str(losses) + "\n")
- elif choice == "credits":
- print("\nDeveloped and created by Nathan Corwin-Shah, 2019")
- print("Mr. Florido helped too.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement