如何在这个基于文本的游戏中确定赢家?

2024-09-29 17:16:02 发布

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

我做了一个游戏的工作版本,玩家对电脑。我不知道如何确定一个获胜者,经过大约30分钟的研究,我决定问这里

这是一个简单的游戏。计算机是随机的,所以你的对手没有策略。我试着让电脑读前一行,看看谁做了最后一步(对手赢了),我试着用减法。我已经两个月没写代码了,有什么遗漏吗

    import random

    print("That pen game you used to play as a kid (it's kinda like 21 
    dares)")
    newnum = random.randint(20,30)
    print("Starting number: {}".format(newnum))
    while newnum > 0:
        pRemove = int(input("How many do you want to remove: "))
        if pRemove >= 1 and pRemove <= 3:
            newnum = newnum - pRemove
            print("{} left".format(newnum))
            if newnum <= 0:
                break
        elif pRemove > 3 or pRemove < 1:
            print("Put a number from 1-3")
            break
        else:
            print("error")
            exit()
        CPUremove = random.randint(1, 3)
        newnum = newnum - CPUremove
        print("CPU removed {}. {} left!".format(CPUremove, newnum))

最后我要看“你赢了!”或者“你输了!”紧接着节目就结束了(我的意思是在它的末尾添加一些东西,而不是更改代码)


Tags: to代码youformat游戏numberifrandom
1条回答
网友
1楼 · 发布于 2024-09-29 17:16:02

while循环之前,设置一个变量:

human_went_last = False
while newnum > 0:
    ...

在播放器取出一些笔后的检查中,将变量设置为一个新值:

        if newnum <= 0:
            human_went_last = True
            break

然后,在循环之后,你可以检查谁最后去了

if human_went_last:
    print("Human made the last move")
else:
    print("Computer made the last move")

相关问题 更多 >

    热门问题