#!/usr/bin/env python
# coding: utf-8

# In[1]:


import random

colors=('Hearts','Diamonds','Clubs','Spades')
ranks=('Two','Three','Four','Five','Six','Seven','Eight','Nince','Ten','Jack','Queen','King','Ace')
values={'Two':2,'Three':3,'Four':4,'Five':5,'Six':6,'Seven':7,'Eight':8,'Nince':9,'Ten':10,'Jack':10,'Queen':10,'King':10,'Ace':11}
#-----------------------------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------
class Card():
    values={'Two':2,'Three':3,'Four':4,'Five':5,'Six':6,'Seven':7,'Eight':8,'Nine':9,'Ten':10,
       'Jack':10,'Queen':10,'King':10,'Ace':11}
    def __init__(self,color,rank):
        self.color=color
        self.rank=rank
        self.value=values[rank]
        
    def __str__(self):
        return self.rank +" of " + self.color
#-----------------------------------------------------------------------------------------------------------------------
class Deck():
    def __init__(self):
        self.deck=[]
        for color in colors:
            for rank in ranks:
                self.deck.append(Card(color,rank))
                
    def __str__(self):
        for _ in self.deck:
            print (_)
        return ''
        '''
        s=''
        for _ in self.deck:
            s+=_.__str__()+'\n'
        return s
        '''    
    def shuffle(self):
        random.shuffle(self.deck)
        
    def deal(self):
        return  self.deck.pop()
#-----------------------------------------------------------------------------------------------------------------------
class Hand():
    def __init__(self):
        self.cards=[]
        self.value=0
        self.dealer_value=0
        self.aces=0
        
    def __str__(self):
        for _ in self.cards:
            print (_)
        return ''
    
    def add_dealer_card(self,card):
        self.cards.append(card)
        self.value+=card.value
        if card.rank == 'Ace':
            self.aces+=1
            
    def add_card(self,card):
        self.cards.append(card)
        self.value+=card.value
        self.dealer_value+=card.value
        if card.rank == 'Ace':
            self.aces+=1
        
    def special_ace(self):
        while self.value>21 and self.aces >0:
            self.value-=10
            self.aces-=1   
#-----------------------------------------------------------------------------------------------------------------------
class Chips():
    def __init__(self,value=500):
        self.value=int(value)
        self.bet=0  
        
    """
    def add(self,value):
        self.value+=value
        
    def check(self):
        return self.value
    
    def remove(self,value):
        if value<self.value:
            self.value+=-value
        else:
            return "You don't have enough chips!"
    """         

    def win_bet(self):
        self.value+=self.bet
        
    def lose_bet(self):
        self.value-=self.bet     
#-----------------------------------------------------------------------------------------------------------------------    
#-----------------------------------------------------------------------------------------------------------------------    
def take_bet(chips):
    while True:
        try:
            chips.bet=int(input("Input bet: "))
        except ValueError:
            print("Enter integer")
        else:
            if chips.bet > chips.value:
                print("You dont have enough chips. Enter lesser bet. Your chips is {chips.value}")
            else:
                break
            
def hit(deck,hand):

    hand.add_card(deck.deal())
    hand.special_ace
            
def hit_dealer(deck,hand):

    hand.add_dealer_card(deck.deal())
    hand.special_ace
    
def hit_or_stand(deck,hand):
    global playing  
    
    while playing:
        s= input("Do you wanna take card? Input Yes or No: ").lower()
        if s in ('y','yes','n','no'):
            if s in('y','yes'):
                hit(deck,hand)
            else:
                print("Player Stands, Dealer turn")
                playing = False
            break
        else:
            print("Enter Yes or No!")
    
def show_some(player,dealer):
    print("\nDEALER CARDS")
    print("One card is hidden")
    print(dealer.cards[1])
    print("Value is: {}".format(dealer.dealer_value))
    print("\nPLAYER CARDS")
    for _ in range(0,len(player.cards)):
        print(player.cards[_])
    print("Value is: {}".format(player.value))
    
    
def show_all(player,dealer):
    print("\nDEALER CARDS")
    for _ in dealer.cards:
        print(_)
    print("Value is: {}".format(dealer.value))
    print("\nPLAYER CARDS")
    for _ in player.cards:
        print(_)
    print("Value is: {}".format(player.value))
    
def player_busts(player,dealer,chips):
    #if player.value> 21:
    print("\nDEALER WIN! PLAYER BUST!")
    chips.lose_bet()
    
def player_wins(player,dealer,chips):
    #if player.value> dealer.value:
    print("\nPLAYER WIN!")
    chips.win_bet()
    
def dealer_busts(player,dealer,chips):
    #if dealer.value> 21:
    print("\nPLAYER WIN! DEALER BUST!")  
    chips.win_bet()   
    
def dealer_wins(player,dealer,chips):
    #if dealer.value> player.value:
    print("\nDEALER WIN")
    chips.lose_bet()

def push(player,dealer,chips):
    #if dealer.value==21 and player.value==21:
    print("\nPUSH, DEALER AND PLAYER GOT 21")
        


# In[ ]:


from IPython.display import clear_output

print("WELCOME TO BLACKJACK! HAVE FUN!")

player_chip=Chips(input("Starting amount of gold coins: "))
print("You have {} coins".format(player_chip.value))

while True:
    clear_output()
    playing=True
    deck = Deck()
    deck.shuffle()
    
    player=Hand()
    dealer=Hand()
    
    hit_dealer(deck,dealer)
    hit(deck,player)
    
    hit(deck,dealer)
    hit(deck,player)
    
    take_bet(player_chip)
    show_some(player,dealer)
    
    while playing:
        hit_or_stand(deck,player)
        clear_output()
        show_some(player,dealer)
        
        if player.value>21:
            player_busts(player,dealer,player_chip)
            break
    
    if player.value<=21:
        clear_output()
        
        while dealer.value<=player.value:
            hit(deck,dealer)

        show_all(player,dealer)
        
        if dealer.value>21:
            dealer_busts(player,dealer,player_chip) 

        elif player.value>dealer.value:
            player_wins(player,dealer,player_chip)

        elif dealer.value>player.value:
            dealer_wins(player,dealer,player_chip)
        else:
            push(player,dealer,player_chip)
                    

    print("You have {} coins".format(player_chip.value))
    
    x=input("\nDo you wanna play again? Press Yes or No: ")
    if x[0].lower()=='y':
        continue
    elif x[0].lower()=='n':
        break
    else:
        print("Enter Yes or No")


# In[ ]:




