Blackjack python 脚本,重新启动游戏时出现流程错误

Blackjack python script, flow error when restarting game

我是 Python 新手,但我尝试编写了 21 点脚本。在调试并纠正所有明显的错误后,我遇到了一个我无法弄清楚的奇怪事件。

total >21 时,它似乎跳过了 while (total < 21) and (stand != True): 代码块,即使在游戏循环开始时我将所有相关变量归零。

我花了太多时间试图解决这个问题,我不禁认为这有一个明显的解决方案。

我不明白为什么 while (total < 21) and (stand != True): 似乎被跳过了,尽管它应该是每场比赛开始时的真实陈述。

下面是完整的代码。请自行测试以了解我的意思。

import pygame
import random

print("Welcome to PyBlackjack V1.0")
done =  False

while not done:

    # --- Reset our Hands ---

    dealerhand = 0
    split = False
    stand = False
    total = 0

    # --- Dealing the Player's hand. ---

    print("Dealer is dealing hand.")
    firstcard = random.randrange(1, 15)
    print("First card:",str(firstcard),)
    secondcard = random.randrange(1, 15)
    print("Second card:",str(secondcard),)

    total = (firstcard + secondcard)

    print("Hand:",str(total),)

        # --- Bust Check ---

    if total > 21:
        print("Bust! Game Over.")
        newgame = input("Play again? Y/N: ")
        if str(newgame) == "n":
            done = True
            break
        else:
            print("Starting new game! Good Luck!")

    dealerfirstcard = random.randrange(1, 15)
    dealerholecard = random.randrange(1, 15)

    dealerhand = (dealerfirstcard + dealerholecard)

    print("Dealer's Hand:",str(dealerfirstcard))

    # --- Player decides what to do ---

    while (total < 21) and (stand != True):

        if split != True:
            print("Hand:",str(total))
        elif split == True:
            print("Left hand:",str(lefthand),"| Right hand:",str(righthand))

        playerchoice = input("Hit (1), Double Down(2), Split(3), Stand(4)?")

        if int(playerchoice) == 1:
            total += random.randrange(1, 15)
        elif int(playerchoice) == 2:
            #Reserved
            break
        elif int(playerchoice) == 3:
            if ((firstcard + secondcard) / 2) == firstcard and split != True:
                lefthand = (firstcard + random.randrange(1, 15))
                righthand = (secondcard + random.randrange(1, 15))
                split = True
            else:
                print("You cannot split this hand!")
        elif int(playerchoice) == 4:
            print("You stand.")
            stand = True
        else:
            print("Invalid Choice!")

    print("Hand:",total,)

    if total > 21:
        print("Bust! Game Over.")

    newgame = input("Play again? Y/N: ")
    if str(newgame) == "n":
        done = True
        break
    else:
        print("Starting new game! Good Luck!")

    print("Dealer reveals hole card...")
    print("Dealer Hand:",str(dealerhand),)

    # --- Dealer hits until >= 17 ---

    while dealerhand < 17:
        print("Dealer hits...")
        dealerhand = (dealerhand + random.randrange(1, 15))
        print("Dealer hand:",dealerhand,)

    # --- Deciding who wins ---

    if dealerhand > 21:
        print("Dealer busts! You win!")
    elif dealerhand >= 17:
        print("Your hand:",total,"| Dealer hand:",dealerhand,)
        if split != True:
            if dealerhand >= total:
                print("You lose!")
            elif dealerhand < total:
                print("You win!")
        elif split == True:
            if lefthand > dealerhand:
                print("Left hand wins!")
            elif lefthand < dealerhand:
                print("Left hand loses!")
            else:
                print("An error occured. Ending program.")
                done = True
                break

            if righthand > dealerhand:
                print("Right hand wins!")
            elif righthand < dealerhand:
                print("Right hand loses!")
            else:
                print("An error occured. Ending program.")
                done = True
                break

    # --- To loop or not to loop ---

    newgame = input("Play again? Y/N: ")
    if str(newgame) == "n":
        done = True
        break
    else:
        print("Starting new game! Good Luck!")

如果用户失败并选择开始新游戏,您的代码不会从 while not done 循环的开头开始,它只会继续前进。因此,当您到达 while (total < 21) and (stand != True) 行时,总计仍然是导致用户破产的总计。

您似乎正在 {while not done:} 中玩游戏 它只会在玩家输入 "n" 时脱离它 但是开始新游戏的功能在代码中晚于变量赋值,我相信它永远不会重新启动。 此外,如果您将其分解为更小的函数或方法,这将有助于您的编码。

下面是(某种)工作代码。它仍然实现了 Blackjack 的一个奇怪变体(等级从 1 到 15,没有算作 1 或 11 的 A 之类的东西,击中后允许拆分)。一般拆分在这里处理得不好......我认为你可以拆分然后仍然 hit/etc.?但是击球不会更新任何一个分裂的手,再次分裂也不会做任何事情。我会留给你来解决这些逻辑错误。

我认为@Martin 的回答可以很好地解释您所描述的问题。我最终只是用 else 简化了这个逻辑来处理非半身像的情况。顺便说一句,如果您真正想要的只是 break.

,则无需使用 standdone 之类的标志来退出循环

我还清理了一些杂项...删除了一些不必要的转换为 str,清理了检测玩家半身、检测和打印推送等的逻辑。请参阅下面的完整代码并付款注意差异。

import random

print("Welcome to PyBlackjack V1.0")

while True:
    # --- Reset our Hands ---
    dealerhand = 0
    split = False
    total = 0

    # --- Dealing the Player's hand. ---
    print("Dealer is dealing hand.")
    firstcard = random.randrange(1, 15)
    print("First card:", firstcard)
    secondcard = random.randrange(1, 15)
    print("Second card:", secondcard)

    total = firstcard + secondcard

    print("Hand:", total)

    dealerfirstcard = random.randrange(1, 15)
    dealerholecard = random.randrange(1, 15)

    dealerhand = dealerfirstcard + dealerholecard

    print("Dealer's hole card:", dealerfirstcard)

    # --- Player decides what to do ---
    while total < 21:
        if not split:
            print("Hand:", total)
        else:
            print("Left hand:", lefthand, "| Right hand:", righthand)

        playerchoice = int(input("Hit (1), Double Down(2), Split(3), Stand(4)? "))

        if playerchoice == 1:
            total += random.randrange(1, 15)
        elif playerchoice == 2:
            #Reserved
            break
        elif playerchoice == 3:
            # NOTE: This will allow splitting even after hitting
            if firstcard == secondcard and not split:
                lefthand = firstcard + random.randrange(1, 15)
                righthand = secondcard + random.randrange(1, 15)
                split = True
            else:
                print("You cannot split this hand!")
        elif playerchoice == 4:
            print("You stand.")
            break
        else:
            print("Invalid Choice!")

    print("Hand:", total)

    if total > 21:
        print("Bust! Game Over.")
    else:
        print("Dealer reveals hole card...")
        print("Dealer hand:", dealerhand)

        # --- Dealer hits until >= 17 ---
        while dealerhand < 17:
            print("Dealer hits...")
            dealerhand += random.randrange(1, 15)
            print("Dealer hand:", dealerhand)

        # --- Deciding who wins ---
        if dealerhand > 21:
            print("Dealer busts! You win!")
        else:
            print("Your hand:", total, "| Dealer hand:", dealerhand)
            if not split:
                if dealerhand >= total:
                    print("You lose!")
                elif dealerhand < total:
                    print("You win!")
                else:
                    print("Push.")
            else:
                if lefthand > dealerhand:
                    print("Left hand wins!")
                elif lefthand < dealerhand:
                    print("Left hand loses!")
                else:
                    print("Push.")

                if righthand > dealerhand:
                    print("Right hand wins!")
                elif righthand < dealerhand:
                    print("Right hand loses!")
                else:
                    print("Push.")

    # --- To loop or not to loop ---
    newgame = input("Play again? Y/N: ")
    if str(newgame) == "n":
        break
    else:
        print("Starting new game! Good Luck!")