首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >输赢的问题...(必须有easygui)

输赢的问题...(必须有easygui)
EN

Stack Overflow用户
提问于 2016-11-16 02:32:39
回答 1查看 40关注 0票数 0
代码语言:javascript
复制
ComputerChoice = ""
x = 0
wins = 0
losses = 0
ties = 0
rounds = 0
abc = 0
CurrentStatus = 'started'

Choices = ['Rock','Paper','Scissors']
#################################################
def computerchoice(ComputerChoice, Choices, UserChoice): #number
          listthing = Choices[:]
          #listthing.remove[UserChoice]
          ComputerChoice = random.choice(listthing)
          ComputerChoice = Choices.index(ComputerChoice)
          return ComputerChoice
#################################################
import easygui
import random
easygui.msgbox( "Hello, This is a standard game of Rock, Paper, Scissors.","Welcome!","Next>>>")

while x == 0:
         UserChoice = easygui.buttonbox(' __ __ __ __ __ __ __ __ __ __ You just ' +CurrentStatus+ '. __ __ __ __ __ __ __ __ __ __ \n You currently have won '+str(wins)+ ' times, lost ' +str(losses)+' times, and tied '+ str(ties)+' times.  \n\n\nClick your next move: ','Choice Picker 2000',['Rock','Paper','Scissors','Done'])
         UserChoice = ['Rock','Paper','Scissors','Done'].index(UserChoice)
         ComputerChoice = computerchoice(ComputerChoice, Choices, UserChoice)
         if UserChoice == ComputerChoice:
                  ties = ties +1
                  rounds = rounds +1
                  CurrentStatus = "Tied"


         if UserChoice== 3:
                  x = 1
                  break

         elif UserChoice > ComputerChoice and UserChoice + ComputerChoice != 4:
                  wins = wins +1
                  rounds = rounds + 1
                  CurrentStatus = "Won"

         elif UserChoice < ComputerChoice and UserChoice + ComputerChoice != 4:
                  losses = losses +1
                  rounds = rounds +1
                  CurrentStatus = "Lost"

         elif UserChoice + ComputerChoice ==4 and UserChoice != ComputerChoice:
                  if Userchoice == 1:
                           score = score +1
                           rounds = rounds +1
                           CurrentStatus = "Won"
                  elif ComputerChoice == 1:
                          losses = losses +1
                          rounds = rounds +1

                          CurrentStatus = "Lost"


result = ["Cool.","Okay.","I am a failure"]
if wins>losses:
         easygui.msgbox("You won "+str(wins)+ " times, lost " +str(losses)+" times, tied "+ str(ties)+ " and won " +str(int(float(wins)/float(rounds)*100))+ "% of the time.","",result[0])
elif wins==losses:
         easygui.msgbox("You won "+str(wins)+ " times, lost " +str(losses)+" times, tied "+ str(ties)+ " and won " +str(int(float(wins)/float(rounds)*100))+ "% of the time.","",result[1])
elif wins<losses:
         easygui.msgbox("You won "+str(wins)+ " times, lost " +str(losses)+" times, tied "+ str(ties)+ " and won " +str(int(float(wins)/float(rounds)*100))+ "% of the time.","",result[2])

当我运行它时,它工作得很好,但如果你按下“摇滚”,那么你将总是平局/失败,永远不会赢。如果你按“剪刀”,那么你将永远平局/赢,永远不会输。我很确定这是同样的问题,但如果有人能看一下它,我将非常感激。

EN

回答 1

Stack Overflow用户

发布于 2016-11-16 02:43:46

你决定赢家的逻辑看起来有点复杂。也许可以尝试简化并将其中的一些内容提取到一个辅助函数中。

(此函数故意显得不必要的冗长)

代码语言:javascript
复制
def userWon(userChoice, computerChoice):
    if (userChoice == (computerChoice + 1 % 3)):
        return True
    if (computerChoice == (userChoice + 1 % 3)):
        return False
    if (computerChoice == userChoice):
        return None

请注意,你的石头-布-剪刀列表的顺序是,对于位置c的任何给定选择,击败位置c + 1 mod 3的选择。使用它,你可以使用这个助手函数,如果用户赢了,它将返回True,如果用户输了,它将返回False,如果是平局,则返回None

然后,在调用此函数之前,只需检查Quit选项即可。

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

https://stackoverflow.com/questions/40617214

复制
相关文章

相似问题

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