2013-12-19 38 views
0

我有這樣的代碼,現在我的剪刀石頭布遊戲JS。在我的比較功能中,我試圖對其進行編程,以便在未輸入「搖滾」,「紙張」或「剪刀」時顯示警告消息。它雖然不起作用,但當我輸入的字符串不同於3個選項時,我沒有迴應。剪刀石頭布的JavaScript函數

var userChoice = prompt("Rock, Paper, or Scissors"); 
var computerChoice = Math.random(); 

var compchoice = function() 
{ 
    if (computerChoice <= 0.34) 
    { 
     return computerChoice = "Rock"; 
    } 
    else if(computerChoice <= 0.67 && computerChoice >= 0.35) 
    { 
     return computerChoice = "Paper"; 
    } 
    if (computerChoice >= 0.68) 
    { 
     return computerChoice = "Scissors"; 
    } 

}; 

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

     else if (choice1 === "Rock") 
     { 
      if (choice2 === "Scissors") 
      { 
       return alert("Rock crushes Scissors!"); 
      } 
      else if (choice2 === "Paper") 
      { 
       return alert("Paper covers Rock!"); 
      } 
     } 
     if (choice1 === "Scissors") 
     { 
      if (choice2 === "Rock") 
      { 
       return alert("Rock crushes Scissors!"); 
      } 
      else if (choice2 === "Paper") 
      { 
       return alert("Scissors cuts Paper!"); 
      } 
     } 
     else if (choice1 === "Paper") 
     { 
      if (choice2 === "Rock") 
      { 
       return alert("Paper covers Rock!"); 
      } 
      else if (choice2 === "Scissors") 
      { 
       return alert("Scissors cuts Paper!"); 
      } 
     } 
    } 
    else 
    { 
     return alert("Please type Rock, Paper, or Scissors next   time"); 
    } 
}; 

compchoice(); 

compare(userChoice, computerChoice); 

任何原因爲什麼?

+0

大聲笑...你能否告訴答案顯而易見...我正在看更多的答案幻燈片... HEHE –

+0

現在試試Rock-paper-scissors-lizard-Spock – Willem

+0

你可以清理代碼如果你使用這個:'var computerChoice = Math.floor((Math.random()* 3));'然後computerChoice是0,1或2,你可以使用switch語句。 –

回答

3

computerChoice === "Rock" || "Paper" || "Scissors"始終是真實的,因爲它解析爲:

(computerChoice === "Rock") || ("Paper") || ("Scissors") 

而且"Paper"是truthy值。

而且,你似乎比較computerChoice,不userChoice

修正:

if (userChoice === "Rock" || userChoice === "Paper" || userChoice === "Scissors") 

或者:

// array and indexOf 
if (["Rock", "Paper", "Scissors"].indexOf(userChoice) > -1) 
// doesn't work in IE8 

或者:

// regex 
if (/^(Rock|Paper|Scissors)$/.test(userChoice)) 
+0

很好的答案和非常具有說服力的,謝謝! –

0

你在你的第一個具有邏輯錯誤,如果在比較:

if (computerChoice === "Rock" || "Paper" || "Scissors") 

應該是:

if (computerChoice === "Rock" || computerChoice === "Paper" || computerChoice === "Scissors") 

編輯:爲什麼你有這樣的,如果在第一時間沒有意義的語句對我來說。你想比較choice1和choice2。

1
if (computerChoice === "Rock" || computerChoice === "Paper" || computerChoice === "Scissors") 

田田...... 「紙」 作爲一個字符串解析爲真! d

0

你不能這樣做比較是這樣的:

if (computerChoice === "Rock" || "Paper" || "Scissors") 

您需要單獨檢查每一樣:

if(computerChoice === "Rock" || computerChoice === "Paper" || computerChoice === "Scissors") 
-1

我認爲你需要比較要像

if (userChoice === "Rock" || "Paper" || "Scissors") 
    { 
+0

這個答案是錯誤的,並且總會評估爲真。 -1 – Doorknob

0
userChoice

已經給出了答案,但您可以更優化您的代碼。這是我會寫:

var options=["Rock","Paper","Scissors"]; 
var beats=["crushes","covers","cuts"]; 
function play(choice){// "choice" = index of option 

    if(choice<0||choice>options.length-1){alert("invalid input");} 

    var loser=choice>0?choice-1:options.length-1; // what would lose from "choice" 
    var winner=choice<options.length-1?choice+1:0; // what would beat "choice" 
    switch(Math.floor(Math.random()*3)){ //chance of 1 in 3 
     case 0: alert(options[choice]+beats[choice]+options[loser]); break;// win :-) 
     case 1: alert(options[winner]+beats[winner]+options[choice]); break;// lose :-(
     case 2: alert("The result is a tie!"); break; 
    } 
} 
play(0);// 0=rock! 

由於機會是1/3,你只需要創建3次靜態的結果(贏,輸或繪製),併爲它的消息。