首页 > 解决方案 > 石头、纸、剪刀和重启选项

问题描述

我刚开始使用 python 并且可以使用一些帮助!我正在开发一个石头、纸、剪刀游戏,一旦人类或计算机达到 3 胜,我想添加一个重新启动选项。

我已经到处寻找一些答案,但从我查看的所有其他代码看来,这似乎超出了我的范围,或者与我编写的代码完全不同。我没有尝试过使用 def 和 classes,我看到了很多,并且让它看起来很简单。我知道我可能会以一种非常全面的方式来解决这个问题,但我只想在不完全复制某人代码的情况下完成它。

    import random

    moves = ["Rock", "Paper", "Scissors"]
    player_score = 0
    computer_score = 0
    draws = 0

    keep_playing = True
    while keep_playing == True:
        cmove = random.choice(moves)
        pmove = input("What is your move: Rock, Paper, or Scissors?")
        print("The computer chose", cmove)
#Logic to game    
        if cmove == pmove:
            print("It's a DRAW!")

        elif pmove == "Rock" and cmove == "Scissors":
            print("--Player Wins!--")

        elif pmove == "Rock" and cmove == "Paper":
            print("--Computer Wins!--")

        elif cmove == "Paper" and cmove == "Rock":
            print("--Player Wins!--")

        elif pmove == "Paper" and cmove == "Scissors":
            print("--Computer Wins!--")

        elif pmove == "Scissors" and cmove == "Paper":
            print("--Player Wins!--")

        elif pmove == "Scissors" and cmove == "Rock":
            print("--Computer Wins!--")

#Scoreboard
        if pmove == cmove:
            draws = draws + 1
            print("Player:" + str(player_score) + ' | ' + "Computer:" + str(computer_score) + ' | ' + "Draws:" + str(draws))

        if pmove == "Rock" and cmove == "Scissors":
            player_score = player_score + 1
            print("Player:" + str(player_score) + ' | ' + "Computer:" + str(computer_score) + ' | ' + "Draws:" + str(draws))

        if pmove == "Rock" and cmove == "Paper":
            computer_score = computer_score + 1
            print("Player:" + str(player_score) + ' | ' + "Computer:" + str(computer_score) + ' | ' + "Draws:" + str(draws))

        if pmove == "Paper" and cmove == "Rock":
            player_score = player_score + 1
            print("Player:" + str(player_score) + ' | ' + "Computer:" + str(computer_score) + ' | ' + "Draws:" + str(draws))

        if pmove == "Paper" and cmove == "Scissors":
            computer_score = computer_score + 1
            print("Player:" + str(player_score) + ' | ' + "Computer:" + str(computer_score) + ' | ' + "Draws:" + str(draws))

        if pmove == "Scissors" and cmove == "Paper":
            player_score = player_score + 1
            print("Player:" + str(player_score) + ' | ' + "Computer:" + str(computer_score) + ' | ' + "Draws:" + str(draws))

        if pmove == "Scissors" and cmove == "Rock":
            computer_score = computer_score + 1
            print("Player:" + str(player_score) + ' | ' + "Computer:" + str(computer_score) + ' | ' + "Draws:" + str(draws))
#Win/lose restart point?
        if player_score == 3:
            print("-----You Win!-----")
            break

        if computer_score == 3:
            print("-----You Lose!-----")
            break

我希望代码结束时说,“你赢了!” 或“你输了!”然后询问他们是否要重新开始,然后它会重置分数并继续前进,或者如果他们说不,它就会中断。

标签: python

解决方案


您已经有循环来执行此操作。所以当你想“重新开始”你的游戏时,你真的只需要重新设置分数。

从您的赢/输条件开始:

#Win/lose restart point?
if player_score == 3:
    print("-----You Win!-----")
    replay = input("Would you like to play again?")
    if replay.upper().startswith('Y'):
        player_score = 0
        computer_score = 0
        draws = 0
    else:
        keep_playing = False


if computer_score == 3:
    print("-----You Lose!-----")
    if replay.upper().startswith('Y'):
        player_score = 0
        computer_score = 0
        draws = 0
    else:
        keep_playing = False

推荐阅读