Noah
Noah

Reputation: 11

How to program blackjack scoring for an ace

I am trying to create a blackjack simulation so I can test various strategies and simulate them indefinitely. The biggest problem I have encountered is how to score an ace card. Ideally if you were dealt an ace, it would look at your score and it will give you 1 or 11 accordingly however I can't avoid if you were dealt a Five(5), Ace(11) and then a Six(6) to giving you a bust when you should have 12.

class Person:
    wins = 0

    def __init__(self, call):
        self.call = call
        self.hand = []

    def deal(self, cards_dealt):
        cards_in_deck = len(deck)
        for card in range(0, cards_dealt):
            card_position = (random.randint(0, cards_in_deck) - 1)
            self.hand.append(deck[card_position])
            deck.pop(card_position)
            cards_in_deck -= 1

    def total(self):
        my_total = 0
        for x in range(0, len(self.hand)):
            if self.hand[x][0] > 10:
                my_total += 10
            if self.hand[x][0] == 1:
                if self.total < 11:
                    my_total += 11
                else:
                    my_total += 1
            else:
                my_total += self.hand[x][0]
        return my_total


numbers_in_each_suit = 13
suits = ['Spades', 'Hearts', 'Diamonds', 'Clubs']
deck = []

for number in range(0, numbers_in_each_suit):
    for suit in suits:
        deck.append([(number + 1), suit])

cpu = Person(10)
cpu.deal(2)
cpu.total()

Upvotes: 1

Views: 125

Answers (1)

mattroberts
mattroberts

Reputation: 630

Just use 1 for number for creating ace, add 11 for every ace and count each ace, and then subtract 10 if you bust until you don’t or until you run out of aces to decrement.

Upvotes: 1

Related Questions