我正在尝试创建二十一点模拟,以便可以测试各种策略并无限期地模拟它们。我遇到的最大问题是如何获得一张王牌。理想情况下,如果您获得一张王牌,它会查看您的得分,并相应地为您提供1或11,但是我无法避免如果您获得了五(5),王牌(11)然后是六(6)当你应该有十二岁时给你一个半身像。

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()

最佳答案

只需将1用作创建ace的数字,将每个ace加11并计算每个ace,然后如果您破产,则减去10,直到不使用或减少用完的ace为止。

关于python - 如何编程二十一点得分王牌,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58387080/

10-12 22:36