Guest User

Untitled

a guest
Feb 14th, 2020
74
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2. import itertools
  3. import sys
  4.  
  5. class Card:
  6.     def __init__(self, value, suit):
  7.         self.value = value
  8.         self.suit = suit
  9.        
  10.     def __repr__(self):
  11.         return f"{self.value} of {self.suit}"
  12.  
  13.  
  14. class Deck:
  15.     def __init__(self):
  16.         self.card_val = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 10, 10, 10]
  17.         self.card_deck = list(itertools.chain.from_iterable(itertools.repeat(x, 4) for x in self.card_val)) # Repeat card_val values up to 52
  18.  
  19.  
  20.     # Shuffle and build deck
  21.     def build_deck(self):
  22.  
  23.         for shuff in range(len(self.card_deck)):
  24.             s = random.randint(0, shuff)
  25.             self.card_deck[shuff], self.card_deck[s] = self.card_deck[s], self.card_deck[shuff]
  26.  
  27.         return self.card_deck
  28.  
  29.     # Remove cards once they have been played
  30.     def remove_cards(self, rm_card: int):
  31.        
  32.         try:
  33.             self.card_deck.remove(rm_card)
  34.             return self.card_deck
  35.         except ValueError:
  36.             super().build_deck()
  37.             return self.card_deck.remove(rm_card)
  38.        
  39.  
  40. class Game(Deck):
  41.     user_wallet = 100
  42.     pot = 0
  43.  
  44.     def __init__(self):
  45.         self.user_card_count = 0
  46.         self.computer_card_count = 0
  47.         self.break_value = 21
  48.         super().__init__()
  49.  
  50.     @classmethod
  51.     def set_bet(cls):
  52.  
  53.         b = True
  54.         bet = int(input("[+] How much would you like to bet? "))
  55.  
  56.         while b:
  57.             if cls.user_wallet >= bet:
  58.                 cls.user_wallet -= bet
  59.                 cls.pot = cls.pot + bet * 2
  60.                 print(f"\nPot --> ${cls.pot}")
  61.                 b = False
  62.                 return cls.pot, cls.user_wallet
  63.             elif cls.user_wallet < bet:
  64.                 print("[-] Not enough funds available in your wallet")
  65.                 bet = int(input("[+] How much would you like to bet? "))
  66.             else:
  67.                 print("[-] You are completely broke. Goodbye")
  68.                 sys.exit(0)
  69.  
  70.     def computer_get_hand(self):
  71.    
  72.         computer_hand = random.choices(super().build_deck(), k=2)
  73.        
  74.         for pc in computer_hand:
  75.             self.computer_card_count += pc
  76.             super().remove_cards(pc)
  77.             if self.computer_card_count <= 10:
  78.                 next_hand = random.choice(super().build_deck())
  79.                 self.computer_card_count += next_hand
  80.                 super().remove_cards(next_hand)
  81.      
  82.         return self.computer_card_count
  83.    
  84.     def user_get_hand(self):
  85.        
  86.         user_hand = random.choice(super().build_deck())
  87.  
  88.         self.user_card_count += user_hand
  89.         super().remove_cards(user_hand)
  90.  
  91.         return self.user_card_count
  92.  
  93.     @classmethod
  94.     def show_pot(cls):
  95.  
  96.         return cls.pot
  97.    
  98.     @classmethod
  99.     def show_wallet(cls):
  100.  
  101.         return cls.user_wallet
  102.  
  103.     @classmethod
  104.     def win(cls):
  105.  
  106.         print("[W] You win!")
  107.         cls.user_wallet += cls.pot
  108.         cls.pot = 0
  109.         print(f"\nWallet --> ${cls.show_wallet()}\n")
  110.  
  111.         return cls.pot, cls.user_wallet
  112.  
  113.     @classmethod
  114.     def lose(cls):
  115.  
  116.         print("[L] You lose!")
  117.         cls.pot = 0
  118.         print(f"\nWallet --> ${cls.show_wallet()}\n")
  119.  
  120.         return cls.pot, cls.user_wallet
  121.    
  122.     @classmethod
  123.     def tie(cls):
  124.  
  125.         print("[T] Tie!")
  126.         cls.user_wallet += int(cls.pot / 2)
  127.         cls.pot = 0
  128.         print(f"\nWallet --> ${cls.show_wallet()}\n")
  129.  
  130.         return cls.pot, cls.user_wallet
  131.  
  132.     def play_again(self):
  133.      
  134.         play = input("[+] Play Again ('y' or 'n')? ")
  135.         if play == 'y':
  136.             self.user_card_count = 0
  137.             self.computer_card_count = 0
  138.             super().build_deck()
  139.             self.set_bet()
  140.         else:
  141.             sys.exit(0)
  142.        
  143.         return play
  144.  
  145.  
  146.     def start_game(self):
  147.  
  148.         start = True
  149.         self.set_bet()
  150.         while start:
  151.             hit_stay = input("\n[+] Hit or Stay ('h' or 's')?: ")
  152.             if hit_stay == 'h':
  153.                 print(f"[!] Your hand total is {self.user_get_hand()}")
  154.                 if self.user_card_count > self.break_value:
  155.                     self.lose()
  156.                     self.play_again()
  157.                     start = True
  158.             elif hit_stay == 's':
  159.                 print(f"[!] The computers hand total is {self.computer_get_hand()}")
  160.                 start = False
  161.                 if self.computer_card_count > self.break_value and self.user_card_count <= self.break_value:
  162.                     self.win()
  163.                     self.play_again()
  164.                     start = True
  165.                 elif self.computer_card_count == self.user_card_count:
  166.                     self.tie()
  167.                     self.play_again()
  168.                     start = True
  169.                 elif self.computer_card_count <= self.break_value and self.computer_card_count > self.user_card_count:
  170.                     self.lose()
  171.                     self.play_again()
  172.                     start = True
  173.                 elif self.computer_card_count < self.user_card_count and self.computer_card_count < self.break_value and self.user_card_count < self.break_value:
  174.                     self.win()
  175.                     self.play_again()
  176.                     start = True
  177.                 else:
  178.                     self.win()
  179.                     self.play_again()
  180.                     start = True
  181. def main():
  182.  
  183.     print("\n~Welcome to Blackjack~\n")
  184.  
  185.     g = Game()
  186.    
  187.     print(f"Pot --> ${g.show_pot()}")
  188.     print(f"Wallet --> ${g.show_wallet()}\n")
  189.  
  190.     g.start_game()
  191.    
  192.    
  193. if __name__ == '__main__':
  194.     main()
RAW Paste Data