如何修复我的"二十一点播放器模拟器的逻辑"?

时间:2015-12-26 21:12:13

标签: python

我目前正在使用python中的Blackjack播放器模拟器,用户可以在其中完成游戏步骤。我在程序的结构上取得了一些成功,但在尝试完成应该在绘制ace时触发的过程时,我的逻辑面临错误。例如,通常在绘制ace时," New Total"文本重复一次。该程序的另一个问题是,在绘制ace时使用的两个总数不会像我希望的那样进入下一张牌的抽牌/总数。作为编程的新手,我提前道歉,因为我无法深入识别错误,所以这种描述的潜力听起来很模糊。

感谢所有帮助。

import random

def play_hand_dealer():
    total = 0
    player_is_bust = False
    ace_was_thrown = False
    another_card = True
    ace_total = 0
    high_ace = 10

    while player_is_bust == False and another_card != "n":
        card = random.choice([1,2,3,4,5,6,7,8,9,10,10,10,10])
        total = total + int(card)

        if card == 1:
            ace_was_thrown = True
            ace_total = high_ace + int(total)
            print("Ace     New Total is", total, "or", ace_total, end = "   ")

        if ace_total < 21 and ace_was_thrown:
            ace_total = total + 10
            print("New Total is ", total, "or", ace_total, end = "    ")

        else:
            print(card, "New Total is ", total, end = "    ")

        if total > 21:
                player_is_bust = True

        if total < 21:
            another_card = input("Another Card? ")

    if ace_was_thrown:
        if  total + 10 >= 17 and total + 10 <= 21:
            total = total + 10

    if player_is_bust:
        print("Bust")

    else:
        print("        Final Total:",total)


choice = ""
while(choice != "q"):
    play_hand_dealer()
    print()
    choice = input("Enter to play again , or 'q' to quit: ")
    print()

1 个答案:

答案 0 :(得分:0)

我不知道二十一点的规则,所以我只能评论代码本身,而不是游戏逻辑。

total = total + int(card)
ace_total = high_ace + int(total)

你不需要int(),因为这些已经是数字了。

    if card == 1:
        ace_was_thrown = True
        ace_total = high_ace + int(total)
        print("Ace     New Total is", total, "or", ace_total, end = "   ")

    if ace_total < 21 and ace_was_thrown:
        ace_total = total + 10
        print("New Total is ", total, "or", ace_total, end = "    ")

    else:
        print(card, "New Total is ", total, end = "    ")

你可能应该用if,elif,else替换它。这就是为什么你得到两次“新总计”。或者您可以将所有打印件移到if if else下面,这取决于您需要做什么。

    if total > 21:
            player_is_bust = True

    if total < 21:
        another_card = input("Another Card? ")

如果总= = 21,这个地方不知道该怎么办。您可能应该将其更改为:

if total > 21:
        player_is_bust = True

elif total < 21:
    another_card = input("Another Card? ")

else:
    break

在一个游戏中可以获得多个ace,我希望你的程序正确处理这个。

如果你需要更好的帮助,请解释这个游戏的规则。