2016-11-15 49 views
0
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]) 

當我運行它,它的工作原理好,但如果你按「搖滾」,那麼你將永遠繫帶/輸,永遠不會贏。如果你按下「剪刀」,那麼你總是會打/贏,永不輸。 我很確定這是同樣的問題,但如果有人可以查看它,我將非常感激。問題與勝利和損失...(必須有easygui)

回答

0

你確定勝利者的邏輯似乎有點令人費解。也許嘗試簡化並提取一些輔助函數。

(此功能是特意不必要的冗長)

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選項。