2016-07-28 89 views
1

我正在爲一個班級創建一個Rock,Paper,Scissors遊戲。作爲遊戲的一部分,我需要在屏幕上顯示一個武器菜單顯示供用戶選擇。然後電腦會從列表中隨機選擇一件武器。我面臨的問題(我相信)是列表項從[0,2]我的菜單項列表[1,3]。我搜索了幾個小時,但我不明白我一直在網上閱讀的複雜的東西,所以我不知道如何應用它們。將用戶輸入與隨機選擇的列表項目進行比較 - Python

# random integer 
from random import randint 

# list for weapon 
WEAPON = ["Rock", "Paper", "Scissors"] 

# one player mode 
def onePlayer(): 
scoreP = 0 
scoreC = 0 
again = "" 
player = False 

print("---------------------------------------------") 
print("\n\tPlayer VS Computer") 

while player == False: 
    print("Weapons:") 
    print("1. Rock") 
    print("2. Paper") 
    print("3. Scissors") 
    print("4. Quit") 

    player = input("\nSelect your weapon: ") 
    if player == "quit" or player == "q" or player == "4": 
     player = True 
     main() 
    else: 
     try: 
      player = int(player) 
      if player == 1: 
       player = WEAPON[0] 
      elif player == 2: 
       player = WEAPON[1] 
      elif player == 3: 
       player = WEAPON[2] 
     except: 
      print("please enter a number 1 through 4\n") 

    computer = WEAPON[randint(0,2)] 

    if player == computer: 
     print(player," vs ",computer) 
     print("It's a tie!\n") 
     print("Player:",scoreP,"\nComputer:",scoreC) 
     print("") 
    elif player == 1: 
     # computer == paper 
     if computer == 1: 
      print(player," vs ",computer) 
      print("Paper covers rock! You lose!\n") 
      scoreC = scoreC + 1 
      print("Player:",scoreP,"\nComputer:",scoreC) 
      print("") 
     else: 
      print("Rock smashes scissors. You win!\n") 
      scoreP = scoreP + 1 
      print("Player:",scoreP,"\nComputer:",scoreC) 
      print("") 
    elif player == 2: 
     if computer == 2: 
      print(player," vs ",computer) 
      print("Scissors cut paper! You lose!\n") 
      scoreC = scoreC + 1 
      print("Player:",scoreP,"\nComputer:",scoreC) 
      print("") 
     else: 
      print("Paper covers rock. You win!\n") 
      scoreP = scoreP + 1 
      print("Player:",scoreP,"\nComputer:",scoreC) 
      print("") 
    elif player == 3: 
     if computer == 0: 
      print(player," vs ",computer) 
      print("Rock smashes scissors! You lose!\n") 
      scoreC = scoreC + 1 
      print("Player:",scoreP,"\nComputer:",scoreC) 
      print("") 
     else: 
      print("Scissors cut paper. You win!\n") 
      scoreP = scoreP + 1 
      print("Player:",scoreP,"\nComputer:",scoreC) 
      print("") 
    #else: 
    # print("Please select a valid play option\n") 
    player = False  

請不要介意if/else語句中的打印語句。我意識到這些將需要改變。我的主要問題是將用戶輸入與計算機的隨機列表選擇進行比較的邏輯。

+0

代碼有什麼問題?你能把它變得更簡潔嗎? (這是什麼問題?) – Jerrybibo

回答

1

在if語句中,您好像將變量computer(字符串)與整數進行比較。您分配computer = WEAPON[randint(0,2)],因此計算機是以下之一:["Rock", "Paper", "Scissors"]。然而,在你的if語句中,你的意思是:if computer == 1:將其與人物進行比較(你的人物變量是相同的;在你將它與整數進行比較之前,給它分配一個字符串)。

你一定要確保你比較蘋果和蘋果

3

你必須要小心你的變量的內容:

# this is storing a string  
computer = WEAPON[randint(0,2)] 

# this expects an integer 
elif player == 1: 
     # computer == paper 
     if computer == 1: 

這將是對一些問題的根源在於你看到了。另外,一般來說,當編碼嘗試使用有意義的變量名稱並避免將其重複用於多個目的時:在這種情況下,像player_weapon和computer_weapon(而不是重用播放器和計算機)這樣的兩個新變量可能會被阻止你的錯誤。聲明變量時不要懶惰! ;)

1

比較的字符串,而不是數字,這樣

if player == computer: 
     print(player," vs ",computer) 
     print("It's a tie!\n") 
     print("Player:",scoreP,"\nComputer:",scoreC) 
     print("") 
    elif player == 'Rock': 
     # computer == paper 
     if computer == 'Paper': 
      print(player," vs ",computer) 
      print("Paper covers rock! You lose!\n") 
      scoreC = scoreC + 1 
      print("Player:",scoreP,"\nComputer:",scoreC) 
      print("") 
     else: 
      print("Rock smashes scissors. You win!\n") 
      scoreP = scoreP + 1 
      print("Player:",scoreP,"\nComputer:",scoreC) 
      print("") 
    elif player == 'Paper': 
     if computer == 'Scissors': 
      print(player," vs ",computer) 
      print("Scissors cut paper! You lose!\n") 
      scoreC = scoreC + 1 
      print("Player:",scoreP,"\nComputer:",scoreC) 
      print("") 
     else: 
      print("Paper covers rock. You win!\n") 
      scoreP = scoreP + 1 
      print("Player:",scoreP,"\nComputer:",scoreC) 
      print("") 
    elif player == 'Scissors': 
     if computer == 'Rock': 
      print(player," vs ",computer) 
      print("Rock smashes scissors! You lose!\n") 
      scoreC = scoreC + 1 
      print("Player:",scoreP,"\nComputer:",scoreC) 
      print("") 
     else: 
      print("Scissors cut paper. You win!\n") 
      scoreP = scoreP + 1 
      print("Player:",scoreP,"\nComputer:",scoreC) 
      print("") 
    #else: 
    # print("Please select a valid play option\n") 
    player = False 
1

我已經實現小型dict_map凝結您的大多數代碼。它可以進一步濃縮,但爲什麼要麻煩。

# random integer 
from random import randint 

# list for weapon 
WEAPON = ["Rock", "Paper", "Scissors"] 
MAPP = {"Rock":{"Win":'Scissors', "Loss":"Paper", "Adj":"Smashes"}, 
     "Paper":{"Win":"Rock", "Loss":"Scissors", "Adj":"Covers"}, 
     "Scissors":{"Win":"Paper", "Loss":"Rock", "Adj":'Cuts'}} 

def have_won(player, computer): 
    #determines if the players choice has beaten the computers 
    if MAPP[player]["Win"] == computer: 
     adj = MAPP[player]['Adj'] 
     return True, ' '.join([player, adj, computer]) 
    else: 
     adj = MAPP[computer]['Adj'] 
     return False, ' '.join([computer, adj, player]) 

# one player mode 
def onePlayer(): 
    scoreP = 0 
    scoreC = 0 
    again = "" 
    player = False 

    print("---------------------------------------------") 
    print("\n\tPlayer VS Computer") 

    while player == False: 
     print("Weapons:") 
     print("1. Rock") 
     print("2. Paper") 
     print("3. Scissors") 
     print("4. Quit") 

     player = input("\nSelect your weapon: ") 
     if player == "quit" or player == "q" or player == "4": 
      player = True 
     else: 
      try: 
       player = int(player) 
       if player == 1: 
        player = WEAPON[0] 
       elif player == 2: 
        player = WEAPON[1] 
       elif player == 3: 
        player = WEAPON[2] 
      except: 
       print("please enter a number 1 through 4\n") 

     computer = WEAPON[randint(0,2)] 
     print player, computer 
     outcome = have_won(player, computer) 
     if player == computer: 
      print(player," vs ",computer) 
      print("It's a tie!\n") 
      print("Player:",scoreP,"\nComputer:",scoreC) 
      print("") 

     elif outcome[0] == True: 
      print(outcome[1]+"! You Win!!") 
      scoreP += 1 
     elif outcome[0] == False: 
      print(outcome[1]+"! You Lose!!") 
      scoreC += 1 
     #else: 
     # print("Please select a valid play option\n") 
     print("Player:",scoreP,"\nComputer:",scoreC) 
     player = False 
onePlayer() 
+0

這是我在班上學到的更先進的東西,但這基本上是我想要完成的。非常感謝你!目前我有點困惑,因爲我確實需要提供驗證用戶輸入的方法,但我不確定代碼的哪個區域正在處理輸入以及如何處理輸入。 – sedkil

+0

目前幾乎沒有驗證。這可以簡單地添加進來。 – TheLazyScripter

相關問題