简体   繁体   中英

Python Blackjack OOP - calling bust and blackjack class function keep reprinting card values

I'm trying to use OOP to create a blackjack game and every time I call the blackjack or bust function, it reprints the the hand value of the player and dealer ever. I figure it has to do with the Hand class being initialized, but I'm not sure what the work around is there.

Code below for Hand Class + variables:

class Hand(object):

    def __init__(self):

        self.hand = []

    def add_card(self, card):

        self.hand.append(card)
        return self.hand

    def __str__(self):

        hand = [str(card) for card in self.hand]
        print 'Current hand is: ',hand

    def get_value(self):
        value = 0
        for card in self.hand:
            print(card)
            value += card.value
        for card in self.hand:
            if card.card == 'Ace' and value > 21:
                value -= 10
        return value

    def bust(self):
        if self.get_value() > 21:
            print ('Busted!')
        else:
            pass

    def hit_or_stand(self):

        self.hit = 'Hit'
        self.stand = 'Stand'
        self.prompter = raw_input('Would you like to hit or stand? ').lower()
        return self.prompter

    def show_hand(self):

        for card in self.hand:
            print(str(card))

    def show_dealerHand(self):

        print('Dealers Hand: '+ str(self.hand[0]) + ' ' + '[X]') 

    def blackjack(self):

        self.bj = False
        if self.get_value() == 21:
            self.bj = True
            print ('Blackjack!')
        else:
            pass

    def dealerHit(self):

        if self.get_value < 17:
            self.add_card(card)
        else:
            pass 

Here is the main function to run the game:

def runGame():
    player1 = Player()
    deck1 = Deck()
    playerHand = Hand()
    dealerHand = Hand()

    # deal out cards to player
    i = 0
    while i < 2:
        playerHand.add_card(deck1.drawCard())
        dealerHand.add_card(deck1.drawCard())
        i += 1
    playerHand.__str__()
    dealerHand.show_dealerHand()
    player_move = playerHand.hit_or_stand()
    if player_move == playerHand.hit.lower():
        playerHand.add_card(deck1.drawCard())
        while dealerHand.get_value() < 17:
            dealerHand.add_card(deck1.drawCard())
        if playerHand.bust():
            print('Dealer wins!')
        elif dealerHand.bust():
            print('Player wins!')
        if playerHand.blackjack() and dealerHand.blackjack():
            print('Both the Player and the Dealer have a blackjack!')
            player1.winnings += player1.amount
        elif playerHand.get_value() > dealerHand.get_value():
            print('Player wins')
            player1.winnings += player1.amount * 2
        elif playerHand.get_value() < dealerHand.get_value():
            print('Dealer wins')
            player1.winnings -= player1.amount

Notice that the bust function returns nothing:

def bust(self):
    if self.get_value() > 21:
        print ('Busted!')
    else:
        pass

However, in the main you "get" its return value:

if playerHand.bust():
    print('Dealer wins!')
elif dealerHand.bust():
    print('Player wins!')

In that case what you get is None . So what happens here is that playerHand.bust() returns None, which evaluates to False . Therefore, the elif dealerHand.bust() is also being executed, and that is why the dealer's hand is also being printed.

In order to fix this you'll have to return True or False , according to your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM