石头剪刀布游戏功能在Javascript

Rock Paper Scissors Game Function in Javascript

本文关键字:Javascript 功能 游戏 石头剪刀布      更新时间:2023-09-26

我有一个简单的石头剪刀布游戏的代码,但是当我运行它时,它只是要求我输入石头,纸或剪刀,然后就没有其他了。 毕竟没有警报。

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

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

您在这里的问题是您混合了情况。 您将computerChoice设置为 "rock""paper""scissors",但在函数内部,您正在执行与"Rock""Paper""Scissors"的比较。

只需始终使用小写,并在prompt()调用后添加userChoice = userChoice.toLowerCase()

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

仅当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))];
为了

清楚起见,您可以将这个从数组中选择随机元素的东西隐藏在单独的函数中。

有一个好的编码!

相关文章: