岩纸剪刀Javascript功能

时间:2013-12-19 22:27:58

标签: javascript

我的Rock Paper Scissors 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);

有什么原因吗?

6 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

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

TADA!....“Paper”作为一个字符串解析为true:D

答案 2 :(得分:0)

如果在比较中,您的第一个出现逻辑错误:

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

应该是:

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

编辑:为什么你有这个if语句首先对我没有意义。您想要将choice1与choice2进行比较。

答案 3 :(得分:0)

你不能做这样的比较:

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

您需要单独检查每个,例如:

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

答案 4 :(得分:0)

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

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个静态结果(赢,输或画),并为其生成信息。

答案 5 :(得分:-1)

我认为您需要比较为userChoice,如

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