Advertisement
tokyoedtech

blackjack

Jul 8th, 2019
3,487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.55 KB | None | 0 0
  1. # Blackjack by @TokyoEdTech Version 0.9
  2. # YouTube Channel: https://www.youtube.com/channel/UC2vm-0XX5RkWCXWwtBZGOXg
  3.  
  4. import random
  5. import os
  6. import time
  7.  
  8. os.system("clear")
  9.  
  10. class Card:
  11.     def __init__(self, name, suit, value):
  12.         self.name = name
  13.         self.suit = suit
  14.         self.value = value
  15.  
  16.     def print_card(self):
  17.         print("+---+")
  18.         print("|{}  |".format(self.suit))
  19.         print("| {} |".format(self.name))
  20.         print("|  {}|".format(self.suit))
  21.         print("+---+")
  22.  
  23. class Deck:
  24.     def __init__(self):
  25.         self.cards = []
  26.         self.reset()
  27.  
  28.     def reset(self):
  29.         self.cards.clear()
  30.         values = [11, 10, 10, 10, 10, 9, 8, 7 ,6, 5, 4, 3, 2]
  31.         names = ["A", "K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3", "2"]
  32.         suits = ["♣", "♠", "♦", "♥"]
  33.            
  34.         for i in range(len(values)):
  35.             for suit in suits:
  36.                 self.cards.append(Card(names[i], suit, values[i]))
  37.  
  38.         self.shuffle()
  39.  
  40.     def print_deck(self):
  41.         for card in self.cards:
  42.             card.print_card()
  43.  
  44.     def shuffle(self):
  45.         random.shuffle(self.cards)
  46.  
  47.     def draw(self):
  48.         card = self.cards.pop()
  49.         return card
  50.  
  51.  
  52. class Player():
  53.     def __init__(self):
  54.         self.hand = []
  55.         self.chips = 100
  56.         self.choice = ""
  57.  
  58.     def print_hand(self):
  59.         number_of_cards = len(self.hand)
  60.        
  61.         # Top row
  62.         for i in range(number_of_cards):
  63.             print("+---+ ", end="")
  64.  
  65.         print()
  66.         for i in range(number_of_cards):
  67.             card = self.hand[i]
  68.             print("|{}  | ".format(card.suit), end="")
  69.  
  70.         print()
  71.         for i in range(number_of_cards):
  72.             card = self.hand[i]
  73.             print("| {} | ".format(card.name), end="")
  74.  
  75.         print()
  76.         for i in range(number_of_cards):
  77.             card = self.hand[i]
  78.             print("|  {}| ".format(card.suit), end="")
  79.  
  80.         print()    
  81.         for i in range(number_of_cards):
  82.             print("+---+ ", end="")
  83.  
  84.  
  85.     def calculate_hand(self):
  86.         value = 0
  87.         for card in self.hand:
  88.             value += card.value
  89.  
  90.         # Check for aces
  91.         if value > 21:
  92.             for card in self.hand:
  93.                 if card.value == 11:
  94.                     value -= 10
  95.                 if value <= 21:
  96.                     break
  97.  
  98.         return value
  99.  
  100. def print_header():
  101.     print("*********************************************")
  102.     print("***Welcome to TokyoEdTech's Blackjack Game***")
  103.     print("*********************************************")
  104.  
  105. # Create the players
  106. dealer = Player()
  107. player1 = Player()
  108.  
  109. # Create the deck
  110. deck = Deck()
  111.  
  112. def reset_game():
  113.         # Reset
  114.         deck.reset()
  115.  
  116.         player1.hand.clear()
  117.         player1.choice = ""
  118.  
  119.         dealer.hand.clear()
  120.         dealer.choice = ""
  121.  
  122.         print_game()
  123.  
  124.         # Deal
  125.  
  126.         player1.hand.append(deck.draw())
  127.         print_game()
  128.  
  129.         dealer.hand.append(deck.draw())
  130.         print_game()
  131.  
  132.         player1.hand.append(deck.draw())
  133.         print_game()
  134.  
  135.  
  136. def print_game():
  137.     time.sleep(0.5)
  138.     os.system("clear")
  139.     print_header()
  140.  
  141.     print("\n---DEALER---")
  142.     dealer.print_hand()
  143.     print("\nChips: {}".format(dealer.chips))
  144.     if(dealer.choice == "H" or dealer.choice == "S"):
  145.         print("Hand value: {}".format(dealer.calculate_hand()))
  146.     else:
  147.         print()
  148.    
  149.     print("\n---PLAYER 1---")
  150.     player1.print_hand()
  151.     print("\nChips: {}".format(player1.chips))
  152.     print("Hand value: {}".format(player1.calculate_hand()))
  153.    
  154. reset_game()
  155.  
  156. while True:
  157.  
  158.     # Player's turn
  159.  
  160.     while(player1.choice != "S"):
  161.         print_game()
  162.  
  163.         player1.choice = input("\nWould you like to H)it or S)tay? > ").upper()
  164.  
  165.         if(player1.choice == "H"):
  166.             player1.hand.append(deck.draw())
  167.  
  168.         print_game()
  169.  
  170.         # Check for a player bust
  171.  
  172.         if(player1.calculate_hand() > 21):
  173.             dealer.choice = "S"
  174.             dealer.hand.append(deck.draw())
  175.             player1.chips -= 10
  176.             dealer.chips += 10
  177.             print_game()
  178.             print("\nPLAYER 1 BUSTS")
  179.             print("***DEALER WINS!***")
  180.             play_again = input("Press ENTER to continue.")
  181.             reset_game()
  182.             continue
  183.  
  184.  
  185.     # Dealer's turn
  186.     while(dealer.choice != "S"):
  187.         print_game()
  188.  
  189.         if(dealer.calculate_hand() < player1.calculate_hand()):
  190.             dealer.hand.append(deck.draw())
  191.             dealer.choice = "H"
  192.         elif (dealer.calculate_hand() < 17):
  193.             dealer.hand.append(deck.draw())
  194.             dealer.choice = "H"
  195.         else:
  196.             dealer.choice = "S"
  197.  
  198.         print_game()
  199.  
  200.         if(dealer.calculate_hand() > 21):
  201.             player1.chips += 10
  202.             dealer.chips -= 10
  203.             print_game()
  204.             print("\nDEALER BUSTS")
  205.             print("***PLAYER 1 WINS!***")
  206.             play_again = input("\nPress ENTER to continue.")
  207.             reset_game()    
  208.  
  209.  
  210.     # Determine the winner
  211.     if(player1.calculate_hand() > dealer.calculate_hand()):
  212.         player1.chips += 10
  213.         dealer.chips -= 10
  214.         print_game()
  215.         print("***PLAYER 1 WINS!***")
  216.         play_again = input("Press ENTER to continue.")
  217.         reset_game()    
  218.     else:
  219.         player1.chips -= 10
  220.         dealer.chips += 10
  221.         print_game()
  222.         print("***DEALER WINS!***")
  223.         play_again = input("Press ENTER to continue.")
  224.         reset_game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement