2012-11-21 53 views
0

我试图做一个摇滚纸..游戏,但它似乎只有当我有一条领带的代码作品。我可能已经把它弄糟了。我还想问一下,你填写的提示窗口中的数字是字符串还是数字? 任何帮助表示赞赏。谢谢!早期JS岩石剪刀游戏

// rock beats scissors (1 beats 3) 
// paper beats rock (2 beats 1) 
// scissors beat paper (3 beat 2) 


var player1= prompt("Player ONE, choose (1) for rock, (2) for paper, (3) for scissors"); 
var player2 = prompt("Player TWO, choose (1) for rock, (2) for paper, (3) for scissors"); 

function game (player1,player2) 
{ 

    if (player1===player2){ 
    alert("its a tie"); 
    } 
    else 
    { 
     if (player1+player2==="4") 
     { 
     if(player1==="1"){ 
     alert("Rock beats Scissors, Player one wins"); 
     }else { 
     alert("Rock beats Scissors, Player Two wins"); 
     } 

     } 
     if (player1+player2==="3") 
     { 
     if (player1==="1"){ 
      alert("paper beats rock, player One wins"); 
     }else { 
      alert ("paper beats rock, player Two wins"); 
      } 
     } 
     if (player1+player2==="5") 
     { 
      if (player1==="3"){ 
      alert("scissors beats paper, Player One wins"); 
      }else{ 
      alert("scissors beats papaer, player Two wins"); 
      } 

     } 
    } 
}; 
game(player1,player2); 
+0

读了你的“纸拍摇滚”块v请小心! - 它需要切换 –

+0

要查找的类型,只是使用功能的typeof,像这样的警告(ty​​peof运算(PLAYER1)) – Davorin

回答

2

你连接字符串,不添加数字,所以你player1+player2==="3"实际上会产生1221。您想先将字符串转换为数字。

将此代码放在领带检查的else块的顶部。

player1 = parseInt(player1); 
player2 = parseInt(player2); 

作为扩展,你将要消毒玩家的输入,以确保它仅包含数字,仿佛以外的任何其他数字字符串传递这种方法将失败。

+0

多谢多谢。真的很感谢 – invis1985

1

你在做你的player1..2变量的字符串连接而不是整数。

请注意,您还需要更改比较,而不仅仅是将字符串解析为整数。

尝试:

var player1= parseInt(prompt("Player ONE, choose (1) for rock, (2) for paper, (3) for scissors")); 
var player2 = parseInt(prompt("Player TWO, choose (1) for rock, (2) for paper, (3) for scissors")); 

function game (player1,player2) 
{ 

    if (player1===player2){ 
    alert("its a tie you mofos"); 
    } 
    else 
    { 
    if (player1+player2===4) 
    { 
     if(player1===1){ 
     alert("Rock beats Scissors, Player one wins"); 
     }else { 
     alert("Rock beats Scissors, Player Two wins"); 
     } 
    } 
    if (player1+player2===3) 
    { 
     if (player1===1){ 
     alert("paper beats rock, player One wins"); 
     }else { 
     alert("paper beats rock, player Two wins"); 
     } 
    } 
    if (player1+player2===5) 
    { 
     if (player1===3){ 
     alert("scissors beats paper, Player One wins"); 
     }else{ 
     alert("scissors beats papaer, player Two wins"); 
     } 
    } 
    } 
}; 
game(player1,player2); 
+0

谢谢。真的很感谢 – invis1985

0

除了什么Ruirize写的,也有另一种错误:

player1+player2==="4"您尝试添加两个整数,然后比较它们串线。

===运算符不仅仅比较值,还会比较类型。当你将一个整数与一个字符串进行比较时,它将返回false。

要么使用== comperator,其中4号等于字符串 “4”,或者比较数字4:

player1+player2 === 4 
+0

谢谢。非常感谢 – invis1985

0

试试这个!希望能帮助到你!

--javascript code for rock, paper, scissors...-- 

var userChoice = prompt("Please type in your choice : 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 (userChoice, computerChioce) 
{ 
    if (userChoice === computerChioce) 
    { 
     return "The result is a tie!"; 
    } 
    else if (userChoice === "rock") 
    { 
     if(computerChioce === "scissors") 
     { 
      return "rock wins" + " ," + "rock breaks scissors"; 
     } 
     else if (computerChioce === "paper") 
     { 
      return "paper wins" + " ," + "paper captures rock"; 
     } 
    } 
    else if (userChoice === "paper") 
    { 
     if (computerChioce === "rock") 
     { 
      return "paper wins" + ", " + "paper captures rock"; 
     } 
     else if (computerChioce === "scissors") 
     { 
      return "scissors win" + ", " + "scissors cuts paper"; 
     } 
    } 
    else if (userChoice === "scissors") 
    { 
     if (computerChioce === "rock") 
     { 
      return "rock wins" + " " + "rock breaks scissors"; 
     } 
     else if (computerChioce === "paper") 
     { 
      return "scissors win" + ", " + "scissors cuts paper"; 
     } 
    } 
}; 

console.log("You chose" + " " + userChoice + "."); 
console.log("Computer chose" + " " + computerChoice + "."); 
compare (userChoice, computerChoice);