早期的JS Rock纸剪刀游戏

时间:2012-11-21 08:56:51

标签: javascript

我正在尝试制作一个摇滚纸..游戏,但似乎代码只有在我有领带时才有效。我可能在下来的路上搞砸了。我想问你填写的提示窗口上的数字是字符串还是数字? 任何帮助表示赞赏。谢谢!

// 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);

4 个答案:

答案 0 :(得分:2)

您正在连接字符串,而不是添加数字,因此player1+player2==="3"实际上会生成1221。您希望首先将字符串转换为数字。

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

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

作为扩展,您需要清理播放器输入以确保它只包含数字,因为如果传递除数字字符串以外的任何内容,此方法将失败。

答案 1 :(得分: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);

答案 2 :(得分:0)

除了Ruirize写的内容外,还有另一个错误:

player1+player2==="4"行中,您尝试添加两个整数,然后将它们与字符串进行比较。

===运算符不只是比较值,还会比较类型。将整数与字符串进行比较时,它将返回false。

使用==编译器,其中数字4等于字符串“4”,或者与数字4进行比较:

player1+player2 === 4

答案 3 :(得分: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);