我正在使用 javascript 开发剪刀石头布游戏,玩家将玩 3 次,每次的结果都会显示在屏幕上。每次玩都存储在一个不同的函数中,该函数返回一个值。最终结果将根据这三个值的总和给出。
我无法弄清楚在同一个按钮中播放 3 次的逻辑。
第一次按下按钮播放后,如何才能再次按下按钮,但播放第二个功能?我应该只使用 1 个函数,但如何存储 3 个不同的结果?
function playGame(choice) {
confirm('Are you sure?');
document.getElementById("userChoice1").style.backgroundImage = "url('./img/rock2.png')";
document.getElementById("userChoice2").style.backgroundImage = "url('./img/scissors2.png')";
document.getElementById("userChoice3").style.backgroundImage = "url('./img/paper2.png')";
if(choice == 'A')
{
var userChoice = "rock";
/* animation */
document.getElementById("userChoiceDis").style.backgroundImage = "url('./img/rock.png')";
}
else if(choice == 'B')
{
var userChoice = "scissors";
/* animation */
document.getElementById("userChoiceDis").style.backgroundImage = "url('./img/scissors.png')";
} else if (choice == 'C'){
var userChoice = "paper";
/* animation */
document.getElementById("userChoiceDis").style.backgroundImage = "url('./img/paper.png')";
}
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
/* animation */
document.getElementById("aiChoiceDis").style.backgroundImage = "url('./img/rock.png')";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
/* animation */
document.getElementById("aiChoiceDis").style.backgroundImage = "url('./img/paper.png')";
} else {
computerChoice = "scissors";
/* animation */
document.getElementById("aiChoiceDis").style.backgroundImage = "url('./img/scissors.png')";
}
var compare1 = function(choice1, choice2) {
if(choice1 === choice2) {
document.getElementById("result").style.backgroundImage = "url('./img/tie.png')";
var pResult = "tie";
document.getElementById("replay").style.display = "block";
}else if(choice1 ==="rock")
{
if(choice2 === "scissors")
{
/* animation */
document.getElementById("result").style.backgroundImage = "url('./img/win.png')";
/* animation */
document.getElementById("replay").style.display = "block";
var pResult = true;
}else
{
/* animation */
document.getElementById("result").style.backgroundImage = "url('./img/loose.png')";
/* animation */
document.getElementById("replay").style.display = "block";
var pResult = false;
}
}else if(choice1 ==="paper")
{
if(choice2 === "rock")
{
/* animation */
document.getElementById("result").style.backgroundImage = "url('./img/win.png')";
var pResult = true;
/* animation */
document.getElementById("replay").style.display = "block";
}else
{
/* animation */
document.getElementById("result").style.backgroundImage = "url('./img/loose.png')";
var pResult = false;
/* animation */
document.getElementById("replay").style.display = "block";
}
}else if(choice1 ==="scissors")
{
if(choice2 === "rock")
{
/* animation */
document.getElementById("result").style.backgroundImage = "url('./img/loose.png')";
var pResult = false;
/* animation */
document.getElementById("replay").style.display = "block";
}else
{
/* animation */
document.getElementById("result").style.backgroundImage = "url('./img/win.png')";
var pResult = true;
/* animation */
document.getElementById("replay").style.display = "block";
}
}else
{
return "incorrect input";
}
switch(pResult) {
case true:
document.getElementById("gameResult1").style.backgroundImage = "url('./img/win.png')";
break;
case "tie":
document.getElementById("gameResult1").style.backgroundImage = "url('./img/win.png')";
break;
default:
document.getElementById("gameResult1").style.backgroundImage = "url('./img/loose.png')";
}
};
compare1(userChoice,computerChoice);
最佳答案
使用数组 - 每次使用类似的东西:
var allResults = []; //declare the array
allResults.push(results);
然后您将得到一个包含所有结果的漂亮数组。您可以使用 console.log(allResults)
检查数组的内容。要访问单个结果,请使用索引号,例如 allResults[0]
将返回数组中的第一个值。
关于javascript - 如何在同一个函数中存储 3 个不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31237968/