【问题标题】:Rock Paper Scissors Javascript Function石头剪刀布Javascript函数
【发布时间】:2013-12-19 22:27:58
【问题描述】:

我现在有我的石头剪刀布 JS 游戏的代码。在我的比较功能中,我尝试对其进行编程,以便在未输入“Rock”、“Paper”或“Scissors”时显示警告消息。但它不起作用,当我输入与 3 个有效选项不同的字符串时,我没有得到任何响应。

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

有什么原因吗?

【问题讨论】:

  • 大声笑...你能说答案很明显吗...我正在观看更多答案...呵呵
  • 现在试试 Rock-paper-scissors-lizard-Spock
  • 如果你使用这个,你可以稍微清理一下代码:var computerChoice = Math.floor((Math.random() * 3)); 然后computerChoice是0、1或2,你可以使用switch语句。

标签: javascript


【解决方案1】:

computerChoice === "Rock" || "Paper" || "Scissors" 始终为真,因为它解析为:

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

"Paper" 是一个真实值。

另外,您似乎在比较 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))

【讨论】:

    【解决方案2】:
    if (computerChoice === "Rock" || computerChoice === "Paper" || computerChoice === "Scissors")
    

    TADA!.... "Paper" 作为字符串解析为 true :D

    【讨论】:

      【解决方案3】:

      你的第一个 if 在比较中有一个逻辑错误:

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

      应该是:

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

      编辑:为什么你首先有这个 if 语句对我来说没有意义。您想比较choice1 和choice2。

      【讨论】:

        【解决方案4】:

        你不能做这样的比较:

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

        您需要分别检查每个,例如:

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

        【讨论】:

          【解决方案5】:

          答案已经给出,但您可以进一步优化您的代码。这就是我要写的:

          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!
          

          由于机会是三分之一,您只需创建 3 个静态结果(赢、输或平),并为其生成消息。

          【讨论】:

            【解决方案6】:

            我认为您需要像 userChoice 这样的比较

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2022-11-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-01-10
              • 2018-04-06
              • 1970-01-01
              相关资源
              最近更新 更多