2013-12-19 70 views
0

我有一個簡單的岩石剪刀剪刀遊戲的代碼,但是當我運行它時,它只是要求我輸入Rock,Paper或Scissors,然後再沒有其他任何東西。之後沒有任何警報。Javascript中的岩石紙剪刀遊戲功能

var userChoice = prompt("Do you choose rock, paper or scissors?"); 
var computerChoice = Math.random(); 
if (computerChoice < 0.34) { 
    computerChoice = "rock"; 
} else if(computerChoice <= 0.67) { 
    computerChoice = "paper"; 
} else { 
    computerChoice = "scissors"; 
} 

var compare = function (choice1, choice2) 
{ 
    if (choice1 === choice2) 
    { 
     return alert("The result is a tie!"); 
    } 

    if (choice1 === "Rock") 
    { 
     if (choice2 === "Scissors") 
     { 
      return alert("Rock wins!"); 
     } 
     else if (choice2 === "Paper") 
     { 
      return alert("Paper wins!"); 
     } 
    } 
    else if (choice1 === "Scissors") 
    { 
     if (choice2 === "Rock") 
     { 
      return alert("Rock wins!"); 
     } 
     else if (choice2 === "Paper") 
     { 
      return alert("Schissors wins!"); 
     } 
    } 
}; 

compare(userChoice, computerChoice); 

順便說一句,我使用的是在線編輯器。

+0

您可能會遇到混合情況下的問題。 「剪刀」!==「剪刀」。 – showdev

+0

var userChoice = prompt(「你選擇石頭,紙還是剪刀?」); var computerChoice = Math.random(); if(computerChoice <0.5){ alert(「Computer Wins!」) }':) –

+0

Duh!謝謝showdev! –

回答

1

你這裏的問題是,你是混合的情況。您將computerChoice設置爲"rock","paper""scissors",但隨後您將與"Rock""Paper""Scissors"進行比較。

只需使用小寫字母,並在調用prompt()後添加userChoice = userChoice.toLowerCase()

1

jsFiddle

沒有return小號需要。

var userChoice = prompt("Do you choose rock, paper or scissors?"); 

var computerChoice = Math.random(); 
if (computerChoice < 0.34) { 
    computerChoice = "rock"; 
} else if (computerChoice <= 0.67) { 
    computerChoice = "paper"; 
} else { 
    computerChoice = "scissors"; 
} 

function compare(choice1, choice2) { 
    if (choice1 === choice2) { 
     alert("The result is a tie!"); 
    } 

    if (choice1 === "Rock") { 
     if (choice2 === "Scissors") { 
      alert("Rock wins!"); 
     } else if (choice2 === "Paper") { 
      alert("Paper wins!"); 
     } 
    } else if (choice1 === "Scissors") { 
     if (choice2 === "Rock") { 
      alert("Rock wins!"); 
     } else if (choice2 === "Paper") { 
      alert("Schissors wins!"); 
     } 
    } 
}; 

compare(userChoice, computerChoice); 
+0

編寫代碼的方式,所有輸入都需要一致地大寫。這可以使用來自@ F.J的'toLowerCase()'方法來解決:http://jsfiddle.net/V8HCp/2/ – showdev

0

如果choice1是要麼"Rock""Scissors"精確匹配這將只顯示任何內容。

您遺漏了"Paper"。更重要的是,您排除了用戶拼寫錯誤的可能性。

你應該有一個'其他'分支。這至少會給你一些反饋。在JavaScript

var compare = function (choice1, choice2) 
{ 
    if (choice1 === choice2) 
    { 
     return alert("The result is a tie!"); 
    } 

    if (choice1 === "Rock") 
    { 
     if (choice2 === "Scissors") 
     { 
      return alert("Rock wins!"); 
     } 
     else if (choice2 === "Paper") 
     { 
      return alert("Paper wins!"); 
     } 
    } 
    else if (choice1 === "Scissors") 
    { 
     if (choice2 === "Rock") 
     { 
      return alert("Rock wins!"); 
     } 
     else if (choice2 === "Paper") 
     { 
      return alert("Schissors wins!"); 
     } 
    } 
    alert('Unhandled combination! choice1 = ' + choice1 + ', choice2 = ' + choice2); 
}; 
0

字符串比較是區分大小寫:

"rock" != "Rock" 
0

一個明顯的錯誤是,計算機的選擇都是小寫,而compare函數使用titlecase的岩石,紙和剪刀。

我還會對您的代碼進行一些改進。首先,compare函數現在正在做兩件事情:計算遊戲結果並將其發送給用戶。更好的方法是單獨做這件事。功能應該做單件事情:

function compare(choice1, choice2) { 
    ... 
     return "It's a tie"; 
    ... 
     return "Rock wins"; 
    ... 
} 

alert(compare(...)) 

二,小寫用戶輸入。這將使其與電腦選擇相同的格式,並可能從用戶問題中解救出來。

三,檢查用戶輸入的,如果它是可用的變種之一:

var userChoice = prompt("Do you ...?").toLowerCase(); 

var variants = ['rock', 'paper', 'scissors']; 
if (variants.indexOf(userChoice) === -1) { 
    alert("Don't cheat! Choose one of ...") 
} 

第四,也有在這場比賽只有六個可能的選擇對,所以compare功能不應該那麼久。使其更簡單的方法之一是明確列出所有可能的選擇對:

var choicePairs = { 
    'rock.rock': "It's a tie", 
    'rock.paper': "Paper wins", 
    'rock.scissors': "Rock wins", 
    'paper.rock': "Paper wins", 
    ... 
} 

function compare(choice1, choice2) { 
    return choicePairs[choice1 + '.' + choice2]; 
} 

和最後一個可以重複使用variants陣列選擇計算機選擇:

var computerChoice = variants[Math.floor(Math.random() * variants.length))]; 

您可以藏匿這個選擇隨機元素爲了清晰起見,在單獨的函數中放置數組。

有一個很好的編碼!