二十一点程序CPU不断处理。(Python)

时间:2018-10-15 17:20:46

标签: python python-3.x while-loop

我最近在学校开始使用python(3.7),最近的工作是创建一个简单的二十一点游戏。我几乎快要完成了,但是有一个问题,在用户使用Twists或Sticks之后,它直接进入了Cpu,他们只是继续发卡。我的加捻功能可以正常工作,该程序让您在选择粘贴后选择一张ace是1还是11,但是在显示用户轮到自己之后,程序不断重复“发牌人的第二张牌是...”经销商的总数是...”,因为在“经销商被困...”行前随机停了下来。我敢肯定,有人可以为我提供一些简单的解决方法,而问题可能是由于一些基本的误解while循环的工作原理,因此不胜感激。对不起,任何混乱的代码,这是我的第一个程序。

谢谢,代码在下面

#Allows user to Twist or Stick     
while userStick == False:
    userChoice = input("Twist (T) or Stick (S)?")
    if userChoice == ("T"):
        userNumThree = random.randint(1,13)
        if userNumThree == 11 or userNumThree ==12 or userNumThree == 13:
            userNumThree = 10
        userTotal = userTotal + userNumThree
        print ("Your next card is...")
        time.sleep(0.5)
        print (userNumThree)
        if userNumThree == 1 and userAce == False:
            userAce = True
            time.sleep(0.5)
        print ("Your total is...")
        time.sleep(0.5)
        print (userTotal)
        if userTotal > 21:
            userBust = True
            print ("You have bust!")
            break

    if userChoice == ("S"):
        userStick = True
        if userAce == True:
            aceNumber = input("Do you want your ace to be a 1 or and 11")
            if aceNumber == "11":
                userTotal = userTotal + 10
            elif aceNumber == "1":
                userTotal == userTotal
            else:
                print ("I did not understand that number, so one has been chosen as default")
        print ("Okay, your final total is...")
        print (userTotal)
    break


#Allows the dealer to twist or stick            
while cpuStick == False:
    cpuTotal = cpuNumTwo
    if cpuTotal < 17:
        cpuNumThree = random.randint(1,13)
        if cpuNumThree == 11 or cpuNumThree == 12 or userNumThree == 13:
            cpuNumThree = 10
        if cpuNumThree == 1 and cpuAce == False:
            cpuAce = True
        cpuTotal = cpuTotal + cpuNumThree
        if cpuAce == True and cpuTotal + 10  >= 17 and cpuTotal + 10 <= 21:
            cpuStick = True
            cpuTotal = cpuTotal + 10
            print ("The Cpu has stuck with...")
            print (cpuTotal)                
            break

        print ("The dealer's second card is...")
        print (cpuNumThree)
        print ("The dealer's total is...")
        time.sleep(0.5)
        print (cpuTotal)

    elif cpuTotal >= 17:
        cpuStick = True
        print("The cpu has stuck with...")
        print (cpuTotal)
        break


    if cpuTotal > 21:
        cpuBust = True
        print("The dealer has Bust! You win")
        break

if userStick == True and cpuStick == True:
    if userStick > cpuStick:
        print ("You win!")
    elif cpuStick > userStick:
        print ("You lose!")

0 个答案:

没有答案