二十一点式蟒蛇(计算机)游戏循环不正确

时间:2013-12-09 21:28:49

标签: python if-statement blackjack

我遇到的主要问题是当我在stay上选择hand_one,然后在hit选择hand_two时会发生什么。

不再让我再次hit or stay hand_two,而是hit or stay {}返回hand_one,当我已选择留在手边时,所以hand_one应该没有更多选项。这会导致出现多个打印语句和错误游戏的问题。

我的代码有什么问题导致它循环回hand_one
完整代码位于:http://labs.codecademy.com/Bjaf/2#:workspace

以下是可能导致问题的部分。

def hit_or_stay(person):
    hit_or_stay = raw_input("Do you want to hit or stay? You can type h or s.")
    if hit_or_stay == 'h' or hit_or_stay == 'hit':
        deal_one_card(person)
        value_of_current_cards(person)
        number_value_of_hand()
    elif hit_or_stay == 's'or hit_or_stay == 'stay':
        print "You stayed"
        return
    else:
        hit_or_stay(person)

def number_value_of_hand():
    if number_of_hands > 0:
        value_of_hand_one = value_of_current_cards(hand_one)
        if value_of_hand_one < 18:
            print "\n" "You have %i" % (value_of_hand_one)
            hit_or_stay(hand_one)
        elif value_of_hand_one > 18:
            print "You Lose"
            return
        elif value_of_hand_one == 18:
            print "You hit HOT 18!!!"
            return
        if number_of_hands > 1:
            value_of_hand_two = value_of_current_cards(hand_two)
            if value_of_hand_two < 18:
                print "\n" "Your second hand has %i" % (value_of_hand_two)
                hit_or_stay(hand_two)
            elif value_of_hand_two > 18:
                print "You Lose"
                return
            elif value_of_hand_two == 18:
                print "You hit HOT 18!!!"
                return

number_value_of_hand()

任何人都可以看到为什么它循环回来给hand_one另一个选项?可能我怎么解决它?非常感谢!

1 个答案:

答案 0 :(得分:2)

您在此步骤中出现问题:

hit_or_stay(hand_two)

当你点击hand_two时,你的代码会这样做:

deal_one_card(person)
value_of_current_cards(person)
number_value_of_hand()

问题就在于此,因为number_value_of_hand()会将您带回到该函数的开头,并再次通过hand_one选项。

您可能需要重写number_value_of_hand()函数以包含一个参数,告诉它从哪里开始(hand_one,hand_two等)

我可能会制作list手,并遍历列表。然后,您可以致电number_of_hands(hands[i])i手。