blackjack.py 3.57 KB
from random import randrange 

SUITS = (':clubs:', ':spades:', ':hearts:', ':diamonds:')
CARDS = ('A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K')
CARD_VALUES = {'A':11, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':10, 'Q':10, 'K':10}

class Hand:
    def __init__(self):
        self.cards = []

    def add_cards(self, cards):
        if isinstance(cards, list):
            for card in cards:
                self.cards.append(card)
        else:
            self.cards.append(cards)

    def remove_card(self, card):
        self.cards.remove(card)

    def get_cards(self):
        return self.cards

    def get_points(self):
        value = 0
        number_of_aces = 0
        for card in self.cards:
            value = value + CARD_VALUES[card[0]]
            if card[0] == 'A':
                number_of_aces = number_of_aces + 1
        
        while value > 21 and number_of_aces > 0:
            value = value - 10
            number_of_aces = number_of_aces - 1
        return value

    def is_blackjack(self):
        if self.get_points() == 21 and len(self.cards) == 2:
            return True
        else:
            return False


class Deck:
    def __init__(self, number_of_decks=1):
        self.shuffle(number_of_decks)

    def shuffle(self, number_of_decks):
        self.cards = []
        for d in range(number_of_decks):
            for suit in SUITS:
                for card in CARDS:
                    self.cards.append(card + suit)

    def draw(self,number_of_cards=1):
        drawn_cards = []
        for n in range(number_of_cards):
            choice = randrange(0,len(self.cards))
            drawn_cards.append(self.cards.pop(choice))
        return drawn_cards

    def cards_left(self):
        return len(self.cards)

class Blackjack:
    def __init__(self, bet):
        self.bet = bet
        self.deck = Deck()
        self.player_hand = Hand()
        self.player_hand.add_cards(self.deck.draw(2))
        #print(player_hand.get_cards())
        self.player_points = self.player_hand.get_points()
        #print("Player: {}".format(player_points,))

        self.dealer_hand = Hand()
        self.dealer_hand.add_cards(self.deck.draw(2))
        #print(dealer_hand.get_cards())
        self.dealer_points = self.dealer_hand.get_points()
        #print("Dealer: {}".format(dealer_points,))

    def is_busted(self):
        if self.player_points > 21:
            return 1
        elif self.dealer_points > 21:
            return 2
        else:
            return 0

    def is_win(self):
        # TODO: Check for blackjack Ace + 10 pt
        if self.player_hand.is_blackjack() and not self.dealer_hand.is_blackjack():
            return (self.bet + self.bet * 1.5, 'Blackjack, you win: {}')
        elif self.dealer_hand.is_blackjack():
            return (-self.bet, 'You lose: {}')
        elif self.player_points > self.dealer_points:
            return (self.bet * 2, 'You win: {}')
        elif self.player_points == self.dealer_points:
            return (self.bet, 'push your bet is returned: {}')
        else:
            return (-self.bet, 'You lose: {}')

    def print_hand(self):
        dealers = ' '.join(self.dealer_hand.get_cards()[1:])
        print("Dealer's Hand: {}".format(dealers))
        

        print("Player's Hand: {} for {} points".format(' '.join(self.player_hand.get_cards()), self.player_points))
bet = 10
bj = Blackjack(bet)
bj.print_hand()
busted = bj.is_busted()
if busted == 1:
    print("YOU BUST")
elif busted == 2:
    print("Dealer Busts, YOU WIN!")
else:
    win, response = bj.is_win()
    print(response.format(win,))