簡體   English   中英

在游戲中實現功能

[英]implementing functions in a game

我在這部分作業中遇到麻煩。 我必須聲明游戲的獲勝者,然后輸入函數。 輸入所有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

怎么樣

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!!!")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM