0

我正在尝试模拟二十一点游戏的庄家,我真的很困惑。

基本上,我使用一副仅由 J、Q、K、A 和 2-10 组成的纸牌来简化这些内容,而不是使用普通的纸牌设置。

我意识到这一切都很混乱,我已经在课堂上工作了 3 个小时。

class Deck:
    '''Simulates a deck of cards.'''

    def __init__(self):
        '''Deck() -> Deck
        Creates a deck of cards'''
        self.Deck = ['J', 'J', 'J', 'J', 'Q', 'Q', 'Q', 'Q', 'K', 'K', 'K', 'K', 'A', 'A', 'A', 'A', '2', '2', '2', '2', '3', '3', '3', '3', '4', '4', '4', '4', '5', '5', '5', '5', '6', '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '8', '9', '9', '9', '9', '10', '10', '10', '10']

    def __str__(self):
        '''str(Deck) -> str
        Returns string showing number of cards left'''
        output = str(len(self.Deck)) + ' cards'
        return(output)

    def shuffle(self):
        '''shuffle(self) -> Deck
        Shuffles deck.'''
        import random
        random.shuffle(self.Deck)

     def draw(self):
        if len(self.Deck) > 0:
            return(self.Deck.pop(0))
        else:
            return(-1)

    def add(self, card):
        self.Deck.append(str(card))

class BlackJackPlayer(Deck):
    '''Represents someone playing blackjack.'''

    def __init__(self, Deck):
        self.BlackJackPlayer = Deck
        self.hand = ''
        self.value = 0

    def __str__(self):
        return self.hand

    def clean_up(self):
        self.BlackJackPlayer = Deck
        self.hand = ''
        self.value = 0

    def get_value(self):
        for x in self.hand:
            if x != ',':
                if x not in range(1, 11):
                    if x != 'A':
                        self.value += 10
                    else:
                        if (self.value + 11) > 17:
                            self.value += 1
                        else:
                            self.value += 11
                else:
                    self.value += x
        return(self.value)

    def hit(self):
        Deck.shuffle(self.BlackJackPlayer)
        card = Deck.draw(self.BlackJackPlayer)
        return(card)

    def play_dealer(self):
        while BlackJackPlayer.get_value(self) < 17:
            self.hand += BlackJackPlayer.hit(self)
            print(self.hand)
            if BlackJackPlayer.get_value(self) < 17:
                self.hand += ','
        if BlackJackPlayer.get_value(self) > 21:
            return('BUST')
        else:
            return

出于某种原因,每当我调用 jack.play_dealer() 时,它都会过早停止,因为它认为这手牌已经超过 21。

例如:

>>> deck = Deck()
>>> jack = BlackJackPlayer(deck)
>>> jack.play_dealer()
7
'BUST'
>>> jack.get_value()
40

我猜我的 get_value() 有问题,但我想不通。

提前致谢!

4

2 回答 2

1

你的Deck卡片都是字符串:

self.Deck = ['J', 'J', 'J', 'J', 'Q', 'Q', 'Q', 'Q', 'K', 'K', 'K', 'K', 'A', 'A', 'A', 'A', '2', '2', '2', '2', '3', '3', '3', '3', '4', '4', '4', '4', '5', '5', '5', '5', '6', '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '8', '9', '9', '9', '9', '10', '10', '10', '10']

注意'2'and'3'等字符串。

但是您尝试将它们视为整数:

for x in self.hand:
    if x != ',':
        if x not in range(1, 11):

x永远不会in range(1, 11),因为你所有的值都是字符串。所以上面的if语句总是 True.

下一部分是:

if x != 'A':
    self.value += 10
else:
    if (self.value + 11) > 17:
        self.value += 1
    else:
        self.value += 11

抽出“数字”牌时,not in range(1, 11)仍然为真,它们不等于'A',因此您将所有牌都算作值 10。

您需要重新考虑如何在此处处理数字卡。您可以测试字母数字顺序:

if x >= 'A':

也适用于您的数字卡,因为在 ASCII 标准中,所有数字都排在大写字母之前。使用它代替not in range(1, 11)测试。

下一个问题是,当卡片为数字时,您将尝试使用字符串将值相加:

else:
    self.value += x

但这会增加字符串;字符串值总是大于数字(在 Python 3 中,比较字符串和数字会引发错误)。

用于int()对实际数字求和:

else:
    self.value += int(x)

现在至少你有一些工作:

>>> deck = Deck()
>>> jack = BlackJackPlayer(deck)
>>> jack.play_dealer()
A
A,A
A,A,9
'BUST'
>>> jack.get_value()
60

但这仍然太高了。那是因为您使用self.value存储计算但从不重置它。您继续为每个新计算添加。

使用局部变量而不是self.value

def get_value(self):
    value = 0
    for x in self.hand:
        if x >= 'A':
            if x != 'A':
                value += 10
            else:
                if value > 6:
                    value += 1
                else:
                    value += 11
        else:
            value += int(x)

    return value

现在游戏按预期运行:

>>> jack = BlackJackPlayer(Deck())
>>> jack.play_dealer()
J
JA
JAA
JAA6
于 2013-08-23T22:42:11.183 回答
0

以下是您应该如何替换获取分数的逻辑。问题是您将字符串视为整数,并且您在不重置值的情况下重新评估整只手。

def get_value(self):
    self.value = 0 # reset for reevaluation
    aces11 = 0
    for x in self.hand.split(','):
        x = x.strip() # strip off excess whitespace
        if x in '2345678910': # numeric
            self.value += int(x)
        elif x == 'A': #An ace
            self.value += 11
            aces11 += 1
        else: # a face card
            self.value += 10
        #check to reduce aces if greater than 17
        while self.value>17 and aces11>0:
            aces11 -= 1
            self.value -= 10
    return(self.value)
于 2013-08-23T22:53:44.753 回答