在游戏中实现功能

时间:2014-02-17 22:46:32

标签: python

我在执行这部分工作时遇到了麻烦。我必须宣布游戏的赢家,然后输入一个功能。一旦我输入了所有if语句,我就必须创建一个函数def playGame()。这必须包括:

showRules()
user = getUserChoice()
computer = getComputerChoice()
declareWinner(user, computer)

我不太清楚如何做到这一点。请帮忙。

以下是我到目前为止完成的代码:(我是否还需要为剪刀做if语句?)

#Display game rules
print("Rules: Each player chooses either Rock, Paper, or Scissors.")
print("\tThe winner is determined by the following rules:")
print("\t\tScissors cuts Paper   --> Scissors wins")
print("\t\tPaper covers Rock     --> Paper wins")
print("\t\tRock smashes Scissors --> Rock Wins")



def userchoice():
    choice = (input("Make your selection (Rock, Paper, or Scissors). ")).lower()
    return choice

#obtain computer input
def getComputerchoice():
    import random
    rnum = random.randint(1,3)
    if rnum == 1:
        print("The computer has chosen Rock.")
    if rnum == 2:
        print("The computer has chosen Paper.")
    if rnum == 3:
        print("The computer has chosen Scissors.")
        return rnum

#Declare winner
 def declarewinner (user, rnum):
    if userchoice == 1:
        print("You have chosen rock.")
    if getComputerchoice == 1:
        print("Computer has chose rock as well.")
        return 0
     else:
        print("The computer has chosen scissors. Rock breaks scissors. You WIN!")
        return 1
    if userchoice == 2:
        print("You have chosen paper.")   
    if getComputerchoice == 1:
        print("The computer has chosen rock. Paper covers rock. You WIN!")
        return 1
    elif getComputerchoice == 2:
        print("The computer has chosen paper as well.")
        return 0
    else:
        print("The computer has chosen scissors. Scissors cut paper. You LOSE!")
        return 1
    if userchoice == 3: 
        print("You have chosen scissors")
    if getComputerchoice == 1:
        print("The computer has chosen rock. Rock breaks scissors. You LOSE!")
        return 1
    elif getComputerchoice == 2:
        print("The computer has chosen paper. Scissors cut paper. You WIN!")
        return 1

1 个答案:

答案 0 :(得分:0)

怎么回合

class hand:
     def __init__(self,rps):
        self.rps = ["rock","paper","scissors"].index(rps.lower())
     def __cmp__(self,other): #take advantage of pythons __cmp__ and override it
        #this is the magic
        wins_against = {0:2,1:0,2:1}
        #if their equal return 0
        if self.rps == other.rps: return 0
        #if this hand wins return 1
        if wins_against[self.rps] == other.rps:return 1
        #else (This hand loses) return -1
        return -1
     def __str__(self):
        return ["rock","paper","scissors"][self.rps]

print( hand("rock") > hand("paper") )
print( hand("scissors") > hand("paper") )
print( hand("rock") > hand("scissors") )

认为它可能有点矫枉过正,但它是一种很酷的技术......如果你现在可以把它搞定,它可以帮助你完成未来的任务

def get_cpu_pick():
    return random.choice(["rock", "paper", "scissors"])

def get_player_pick():
    valid_inputs = {
       'r':'rock','p':'paper','s':'scissors'
    }
    user_choice = input("Select RPS:")
    while user_choice.lower() not  in valid_inputs:
          user_choice = input("Select RPS:")
    return valid_inputs.get(user_choice.lower())     

def play():
    user_hand = hand(get_player_pick())
    cpu_hand = hand(get_cpu_pick())
    if user_hand > cpu_hand:
       print ("User Wins {0} v. {1}".format(user_hand,cpu_hand))
    elif user_hand < cpu_hand:
       print ("CPU Wins {0} v. {1}".format(user_hand,cpu_hand))
    else:
       print("TIE!!!")