繁体   English   中英

Javascript中的石头剪刀布游戏功能

[英]Rock Paper Scissors Game Function in Javascript

我有一个简单的 Rock Paper Scissors 游戏的代码,但是当我运行它时,它只要求我输入 Rock、Paper 或 Scissors 的输入,然后就没有其他内容了。 之后就没有警报了。

var userChoice = prompt("Do you choose 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 (choice1, choice2)
{
    if (choice1 === choice2)
    {
        return alert("The result is a tie!");
    }

    if (choice1 === "Rock")
    {
        if (choice2 === "Scissors")
        {
            return alert("Rock wins!");
        }
        else if (choice2 === "Paper")
        {
            return alert("Paper wins!");
        }
    }
    else if (choice1 === "Scissors")
    {
        if (choice2 === "Rock")
        {
            return alert("Rock wins!");
        }
        else if (choice2 === "Paper")
        {
            return alert("Schissors wins!");
        }
    }
};

compare(userChoice, computerChoice);

顺便说一句,我正在使用在线编辑器。

jsFiddle

不需要return

var userChoice = prompt("Do you choose rock, paper or scissors?");

var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if (computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

function compare(choice1, choice2) {
    if (choice1 === choice2) {
        alert("The result is a tie!");
    }

    if (choice1 === "Rock") {
        if (choice2 === "Scissors") {
            alert("Rock wins!");
        } else if (choice2 === "Paper") {
            alert("Paper wins!");
        }
    } else if (choice1 === "Scissors") {
        if (choice2 === "Rock") {
            alert("Rock wins!");
        } else if (choice2 === "Paper") {
            alert("Schissors wins!");
        }
    }
};

compare(userChoice, computerChoice);

您的问题是您正在混合案例。 您将computerChoice<\/code>设置为"rock"<\/code> 、 "paper"<\/code>或"scissors"<\/code>但随后在函数内部执行与"Rock"<\/code> 、 "Paper"<\/code>或"Scissors"<\/code> 。

只需使用小写字母,并在prompt()<\/code>调用后添加userChoice = userChoice.toLowerCase()<\/code> 。

仅当choice1"Rock""Scissors"完全匹配时,才会显示任何内容。

你漏掉了"Paper" 更重要的是,您排除了用户拼写错误的可能性。

你应该有一个“其他一切”分支。 这至少会给你一些反馈。

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

    if (choice1 === "Rock")
    {
        if (choice2 === "Scissors")
        {
            return alert("Rock wins!");
        }
        else if (choice2 === "Paper")
        {
            return alert("Paper wins!");
        }
    }
    else if (choice1 === "Scissors")
    {
        if (choice2 === "Rock")
        {
            return alert("Rock wins!");
        }
        else if (choice2 === "Paper")
        {
            return alert("Schissors wins!");
        }
    }
    alert('Unhandled combination! choice1 = ' + choice1 + ', choice2 = ' + choice2);
};

javascript中的字符串比较区分大小写:

"rock" != "Rock"

明显的错误是计算机的选择都是小写的,而compare功能使用石头、纸和剪刀的标题。

我还将建议对您的代码进行一些改进。 首先, compare函数现在正在做两件事:计算游戏结果并将其发送给用户。 更好的方法是单独进行。 函数应该做一件事情:

function compare(choice1, choice2) {
    ...
        return "It's a tie";
    ...
        return "Rock wins";
    ...
}

alert(compare(...))

第二,小写用户输入。 这将使其格式与计算机选择相同,并且可能使您免于用户问题。

第三,检查用户输入是否是可用变体之一:

var userChoice = prompt("Do you ...?").toLowerCase();

var variants = ['rock', 'paper', 'scissors'];
if (variants.indexOf(userChoice) === -1) {
    alert("Don't cheat! Choose one of ...")
}

第四,这个游戏只有六个可能的选择对,所以compare功能不应该那么长。 使其更简单的一种方法是明确列出所有可能的选择对:

var choicePairs = {
    'rock.rock': "It's a tie",
    'rock.paper': "Paper wins",
    'rock.scissors': "Rock wins",
    'paper.rock': "Paper wins",
    ...
}

function compare(choice1, choice2) {
    return choicePairs[choice1 + '.' + choice2];
} 

最后,您可以重用variants数组来选择计算机选项:

var computerChoice = variants[Math.floor(Math.random() * variants.length))];

为了清楚起见,您可以将这个 select-random-element-from-array 存储在单独的函数中。

好好编码!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM