gpt4 book ai didi

javascript - 石头、布、剪刀、蜥蜴、Spock 在 JavaScript 中

转载 作者:数据小太阳 更新时间:2023-10-29 05:58:00 25 4
gpt4 key购买 nike

我是 JavaScript 的新手。我刚开始学习它,我决定制作一款“石头、剪刀、蜥蜴、史波克”游戏。这是代码:

var userChoice = prompt("Do you choose rock, paper, scissors, lizard, or spock?")
var computerChoice = Math.random();
if (computerChoice < 0.2) {
computerChoice = "rock";
} else if (computerChoice <= 0.4) {
computerChoice = "paper";
} else if (computerChoice <= 0.6) {
computerChoice = "scissors";
} else if (computerChoice <= 0.8) {
computerChoice = "lizard";
} else {
computerChoice = "spock";
}

alert("The computer chose " + computerChoice);

var compare = function(choice1, choice2){
if (choice1 === choice2) {
alert("And... It's a tie!");
}

//If the user chose rock...
else if (choice1 === "rock") {
if (choice2 === "scissors") {
alert("Rock wins!");
} else if (choice2 === "paper") {
alert("Paper wins!");
} else if (choice2 === "lizard") {
alert("Rock wins!");
} else {
alert("Spock wins!");
}
}

//If the user chose paper...
else if (choice1 === "paper") {
if (choice2 === "scissors") {
alert("Scissors wins!");
} else if (choice2 === "rock") {
alert("Paper wins!");
} else if (choice2 === "lizard") {
alert("Lizard wins!");
} else {
alert("Paper wins!");
}
}

//If the user chose scissors...
else if (choice1 === "scissors") {
if (choice2 === "paper") {
alert("Scissors wins!");
} else if (choice2 === "rock") {
alert("Rock wins!");
} else if (choice2 === "lizard") {
alert("Scissors wins!");
} else {
alert("Spock wins!");
}
}

//If the user chose lizard...
else if (choice1 === "lizard") {
if (choice2 === "scissors") {
alert("Scissors wins!");
} else if (choice2 === "rock") {
alert("Rock wins!");
} else if (choice2 === "paper") {
alert("Lizard wins!");
} else {
alert("Lizard wins!");
}
}

//If the user chose spock...
else if (choice1 === "spock") {
if (choice2 === "scissors") {
alert("Spock wins!");
} else if (choice2 === "rock") {
alert("Spock wins!");
} else if (choice2 === "lizard") {
alert("Lizard wins!");
} else {
alert("Paper wins!");
}
}
};
compare(userChoice, computerChoice);

我想在我的代码中添加 2 个主要内容,但我不知道如何添加:

  1. 现在,如果用户输入带有大写字母“R”的“Rock”,它不会被识别为五个有效输入之一(rock、paper、scissors、lizard 和斯波克)。有没有办法让它在用户输入有效的大写字母(或字母)时仍然有效?

  2. 我想添加一些东西,这样每当有人输入无效的东西(例如“sloth”)时,它就会提醒他们输入无效,并会再次要求他们输入石头、布、剪刀、蜥蜴,或斯波克。

最佳答案

用数学简化结果函数。 http://jsfiddle.net/afrievalt/qBbJn/

var options = ["paper", "rock", "lizard", "spock", "scissors"],
result = [" ties ", " beats ", " loses to "],
bigBang = function(choice1, choice2) {
var index1 = options.indexOf(choice1), //spock => 3
index2 = options.indexOf(choice2), //rock=> 1
dif = index2 - index1; // 1 - 3 => -2
if(dif < 0) { // -2 < 0 => truthy
dif += options.length; // -2 + 5 => 3
}
while(dif > 2) { //3 > 2 => truthy
dif -= 2; // 3 - 2 => 1
}
return choice1 + result[dif] + choice2; //spock beats rock
};

.

  bigBang("spock", "paper");  // spock losses to paper 

var i = Math.floor(Math.random() * 5),
randomChoice = options[i];
bigBang(randomChoice, userChoice);

此函数也适用于 options = ["cockroach", "nuke", "shoe"](来自 70 年代的节目)或任何奇数长度数组,如 options = ["water", "fire", "paper ”、“岩石”、“树”、“金属”、“泥土”]//todo: 如果任何索引 = -1 则抛出错误

关于javascript - 石头、布、剪刀、蜥蜴、Spock 在 JavaScript 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22623331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com