首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python中岩石、纸、剪刀游戏的简单实现

Python中岩石、纸、剪刀游戏的简单实现
EN

Code Review用户
提问于 2023-01-24 16:53:46
回答 1查看 174关注 0票数 4

这是我第一次从头开始编写代码。

我的视力受损,我想知道这段代码的结构、注释和可读性,对于将来可能阅读它的合作者来说

它也是可用的论GitHub

代码语言:javascript
复制
# Blinding Slow Artsy Stuff
# A simple Rock, Paper Scissors game
# Just trying out things I'm learning about coding

# import libraries
import random, os, time

# String animation
def animaString(string):
    name = string
    nameLength = len(name)
    roll = 0
    while roll <=nameLength :
        os.system("clear")
        print(name[:roll])
        roll += 1
        time.sleep(.05)

# call intro animation
animaString("Blinding Slow Artsy Stuff kinda proudly presents:")

# main game function
def startGame() :

# set initial score
    pOneScore = 0
    pTwoScore = 0

# "tutorial"    
    print("""
Rock, Paper, Scissors...
-------------------------------------
Type a number and press Enter/Return
1 = Rock
2 = Paper
3 = Scissors      
-------------------------------------
    """)

# set hands list
    handList = ("Rock", "Paper", "Scissors")

# validate player choice
    while pOneScore < 3  and pTwoScore < 3:
        pOnePick = input("Choose your hand: ")
        while pOnePick != "1" and pOnePick != "2" and pOnePick != "3" :
                print()
                print("Invalid option! Use 1 for rock, 2 for paper or 3 for scissors.")
                pOnePick = input("Go: ")
                continue

# select hand option from list
        if pOnePick == "1" :
            pOnePick = handList[0]
        elif pOnePick == "2" :
            pOnePick = handList[1]
        elif pOnePick == "3" :
            pOnePick = handList[2]

# pick a random option from list for the computer hand
        pTwoPick = random.choice(handList)

# clear screen and print picked hands to the console
        print()
        os.system("clear")
        print(f"Your hand:      {pOnePick}")
        print(f"Computer hand:  {pTwoPick}")

# set the game winner and increase score
        winner = ""
        if pOnePick == pTwoPick :
            winner = "Draw"
        if pOnePick == "Rock" :
            if pTwoPick == "Paper" :
                pTwoScore += 1
                winner = "Computer"
            if pTwoPick == "Scissors" :
                pOneScore += 1
                winner = "Player"
        if pOnePick == "Paper" :
            if pTwoPick == "Scissors" :
                pTwoScore += 1
                winner = "Computer"
            if pTwoPick == "Rock" :
                pOneScore += 1
                winner = "Player"
        if pOnePick == "Scissors" :
            if pTwoPick == "Rock" :
                pTwoScore += 1
                winner = "Computer"
            if pTwoPick == "Paper" :
                pOneScore += 1
                winner = "Player"

# get game winner and print it to the console
        print()
        if winner == "Player" :
            print("You won!")
        elif winner == "Computer" :
            print("Computer won!")
        elif winner == "Draw" :
            print("It's a draw!")
        
# print the new score
        print()
        print(f"""Match Score (Best of 3):
You:       {pOneScore}
Computer:  {pTwoScore}""")
        print()

# check if there is a match winner        
        if pOneScore == 3 :
            print()
            print("You are the match winner!!!")
        if pTwoScore == 3 :
            print()
            print("Computer is the match winner!")

# reset or quit function        
    def again() :

# get and validate player choice        
        print()
        resetGame = input("Play again! Yes or No?")
        while resetGame != "y" and resetGame != "n" :
            print("Invalid option! Use 'y' for YES or 'n' for NO")
            resetGame = input("Let's play again? ")
            continue

# if YES, clear screen and reset game
        if resetGame == "y" :
            os.system("clear")
            startGame()

# if NO, print animated quiting messages and exit game
        if resetGame == "n" :
            animaString("""Blinding Slow Artsy Stuff kinda thank you for playing...
...seriously though, thanks!""")
            time.sleep(1)
            animaString("contact me! github.com/blindingslow")
            time.sleep(5)
            animaString("Goodbye!!!")
            time.sleep(2)
            os.system("clear")
            exit()

# call reset or quit function
    again()

# call main game function
startGame()
EN

回答 1

Code Review用户

发布于 2023-01-24 17:21:22

一些事情,因为python语法是基于间距的,有一些注释与相应的行不一致是令人不快的。而不是

代码语言:javascript
复制
# if YES, clear screen and reset game
        if resetGame == "y" :

代码语言:javascript
复制
        # if YES, clear screen and reset game
        if resetGame == "y" :

由于您已经验证了输入为1、2或3(字符串),所以将pOnePick = handList[0] (以及周围的if...elif)转换为pOnePick = handList[int(pOnePick) - 1]

当您检查set the game winner and increase score时,将这些输入到if...elif中会很容易地增加清晰性,表明得分和胜利者都在链中的某个位置,并且没有必要进行进一步的检查。

当您提示用户输入resetGame = input("Play again! Yes or No?")时,您并不表示最初希望用户输入什么输入(y或n),添加到提示中将有助于resetGame = input("Play again! Yes(y) or No(n)?")

一个安全风险是你的电子邮件以纯文本形式出现在公共页面上。互联网巨魔/机器人在各种方法中获取电子邮件地址,原因很多,但对你没有任何好处。有些人以my_email at domain dot com格式发布(但由于这是一种常见的做法,IDK实际上对您的保护有多好)

票数 3
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/282820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档