为BlackJ添加列表的组件

2024-04-20 13:00:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为我的一个项目制作21点游戏。我在一个列表中列出了一副牌,包括等级和套装,但是当我试图添加玩家的手牌时,我只有一个返回值,而不是总和。在

我不知道该怎么办。首先,我必须获取列表中的所有元素,然后去掉西装以获得一个数字,但我的一些数字是字母(Jack、queen、king、ace……),并且对它们有特定的值。我怎么把它们加起来?我试过用for,while和其他的东西,但是没有成功。在

有什么秘诀可以帮助你实现这个目标吗?在

下面是我的代码示例:

def create_deck(): #this creates the deck of cards
    suit_string = 'hdcs'
    rank_string = '23456789TJQKA'
    global deck
    deck = []
    for suit in range(4):
        for suit in range(13):
            cards = rank_string[rank] + suit_string[suit]
            deck.append(cards)
            random.shuffle(deck)
    return deck

def deal_cards(): #This takes one card from the deck and gives it to the player
    return deck.pop(0)

def hand(): #Initial two cards
    global player1_hand
    player1_hand = []
    print "Player's hand:"
    for i in range(2): #initial cards
        player1_hand.append(deal_cards())
    print player1_hand
    print value()   

def value():
    i = 0
    while i < len(player1_hand):
        card = player1_hand[i]
        value = card[i]
        if value in ('T','J','Q','K'):
            return 10
        elif value == 'A':
            print "Please choose between 1 and 11."
            value_a = input("---> ")
            return value_a
        else:
            return value
    i += 1

现在它给我的是:

^{pr2}$

我知道我并没有真正把这些价值加在一起,但我不知道如何管理它。在

任何帮助都将不胜感激。在

希望我说得够清楚,因为英语不是我的主要语言。在


Tags: theinforstringreturnvaluedefrange
3条回答

好吧,首先,你的代码中有一些错误可能会导致一些问题。

1)在create_deck方法中,两个for循环都使用变量suit作为它们的迭代器,我假设第一个应该设置为rank?

2)应该用return语句结束所有方法,即使它们什么也不返回。这将迫使方法退出,这是一个很好的编码实践。

你应该把你的问题解决好。现在,在返回手牌的总值之前退出该方法,只返回一个值。要解决此问题,请在while循环之外创建一个全局值,并将value元素添加到其中:

def value():
    i = 0
    total_value = 0
    while i < len(player1_hand):
        card = player1_hand[i]
        value = card[i]
        if value in ('T','J','Q','K'):
            total_value += 10
        elif value == 'A':
            print "Please choose between 1 and 11."
            value_a = input(" -> ")
            total_value += value_a
        else:
            total_value += value
    i += 1
    print(value)
    return

通过使用一个dict的“等级值”,你只需在玩家的手上循环,并将值相加。

def value():
    hand_total = 0
    # Add in a dict of "rank values" (excluding the Ace)
    rank_values = {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8,
                   '9':9, '10':10, 'J':10, 'Q':10, 'K':10}
    # No need for the while loop 
    for i in xrange(len(player1_hand)):
        card = player1_hand[i][0] # the added index will get the card          
        if card == 'A':          # without the suit.
            print "Please choose between 1 and 11."
            value_a = input(" -> ")
            hand_total += int(value_a) # add a type check before this
        else:
            hand_total += rank_values[card]
    return hand_total

使用dict您不必区分面牌或编号牌,只需使用键(卡)值并合计金额。

您已经编写了一个函数value(),它看起来像是要计算player1_hand的总值。但该函数只返回它看到的第一张牌的值:

while i < len(player1_hand):
    if value in ('T','J','Q','K'):
        return 10
    elif value == 'A':
        ...
        return value_a
    else:
        ...
        return value

return语句使value()立即返回,而不继续循环。我想你要做的是让循环的每一步都将卡的价值加到手的总价值上:

^{pr2}$

相关问题 更多 >