Skip to content
Advertisement

problem counting the cards in a blackjack game

i made a blackjack game and i’m having problems counting my cards with the aces. Can someone help me clean my code

class Player():
    def __init__(self, name):
        self.bust = False
        
        self.hand = []
        self.hand2 = []
        self.name = str(name)
        self.handlist = []
        
    def hit(self):
        for i in self.handlist:
            response = ''
            while response != 'h' or response != 's':
                print(self.name)
                print(i)
                print("Do you want to hit or stand? (h/s)")
                print(self.gettotal(i))
                response = str(input())
                if response == 'h':
                    phand = i
                    card = pickacard(deck)
                    phand.append(card)
                    i = phand
                    phand = []
                    grade = self.gettotal(i)
                    if grade > 21:
                        self.bust = True
                        print(i)
                        print(self.gettotal(i))
                        print("Player busts!")
                        break
                else:
                    break
        
    def gettotal(self, hand):
        total = 0
        if hand == []:
            total = 0
            return total
        for i in range(len(hand)):
            
            t = int(hand[i][0])
            if t == 11 or t == 12 or t == 13:
                total += 10
            elif t != 1:
                total += t
            elif t == 1 and total < 22:
                total += 11
        if total > 21:
            i = len(hand)-1
            while i >= 0:
                t = int(hand[i][0])
                if t == 1:
                    total -= 10
                    break
                i -= 1
        t = 0            
        return total

i have those exemple:

player1

[[‘1’, ‘D’], [’11’, ‘C’], [’11’, ‘D’], [‘1’, ‘H’]]

Do you want to hit or stand? (h/s)

total = 21

player1

[[‘1’, ‘D’], [’10’, ‘D’], [’11’, ‘H’], [‘1’, ‘H’]]

Do you want to hit or stand? (h/s)

total = 21

Advertisement

Answer

Something like this:

    def gettotal(self, hand):
        total = 0
        aces = False
        for card in hand:
            t = int(card[0])
            if t > 11:
                total += 10
            elif t == 1:
                aces = True
                total += 1
            else:
                total += t
        if aces and total <= 11:
            total += 10
        return total

And, by the way, any card game that shows 1, 11, 12 and 13 instead of A, J, Q, K is not worth playing. Since you’re storing the cards as strings anyway, why not just store the familiar letters? It would take very minor changes to your code, and the user experience would be much better:

    def gettotal(self, hand):
        total = 0
        aces = False
        for card in hand:
            t = card[0]
            if t in "JQK":
                total += 10
            elif t == 'A':
                aces = True
                total += 1
            else:
                total += int(t)
        if aces and total <= 11:
            total += 10
        return total
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement