2014-02-17 4 views
0

과제의이 부분에 문제가 있습니다. 게임의 승자를 선언하고 함수에 입력해야합니다. if 문을 모두 입력하면 함수 def playGame()을 만들어야합니다. 여기에는 다음을 포함해야합니다.게임에서 기능 구현

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

어떻게해야할지 모르겠습니다. 도와주세요. 다음은

은 지금까지 완료 한 코드입니다 : (나는 경우 가위 문뿐만 아니라 할 필요가시겠습니까?)

#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 
+0

기능 그것들은 본질적으로 I/O로서의 역할을합니다. 즉, 입력 (또는'void')과 리턴 값 (또는'void')만을 받아야한다는 것을 의미합니다. 함수의 결과에 따라 다음 주요 동작을 결정할 수 있습니다. 작성하는 모든 함수에서 인쇄를 시작하면 응용 프로그램에 대한 모든 제어 권한을 잃을 수 있습니다. 매우 드문 경우에만 함수 내부에서 인쇄해야합니다 (주로 오류 또는 로깅에서만 사용). –

+0

첫째, 현재 기능이 모두 올바르게 작동하는지 확인해야합니다. 규칙 인쇄를 적절히 명명 된 함수에 캡슐화하지 않은 경우 사용자가 문자열을 선택하지만 정수를 테스트하면 컴퓨터는 '3'또는 '없음'만 선택하고'declarewinner '의 들여 쓰기는 검토해야합니다. – jonrsharpe

답변

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