我试图使用javascript和jquery制作一个rpsls游戏。我有大部分功能,但我似乎无法在5次投掷结束时将变量重置为0。我使用if语句将圆形变量重置为0,但我似乎无法弄清楚如何在5轮后将两个分数重置为0。
$(document).ready(function() {
var round = 0
var yourScore = 0
var compScore = 0
$(".shoot").on("click", function() {
var choiceRPS = ['rock', 'paper', 'scissor','lizard','spock'];
var ranNum = Math.floor(Math.random() * choiceRPS.length);
var compChoice = choiceRPS[ranNum];
var userChoice = this.id;
round++;
$("#round").html(round);
var compChoice = choiceRPS[ranNum];
console.log(userChoice);
console.log(compChoice);
if (compChoice == userChoice) {
};
if (userChoice === "rock") {
if (compChoice === "lizard" || compChoice === "scissor") {
yourScore++;
$("#yourScore").html(yourScore);
} else {
if (compChoice === "paper" || compChoice === "spock") {
compScore++;
$("#computerScore").html(compScore);
}
}
};
if (userChoice === "paper") {
if (compChoice === "rock" || compChoice === "spock") {
yourScore++;
$("#yourScore").html(yourScore);
} else {
if (compChoice === "scissor" || compChoice === "lizard") {
compScore++;
$("#computerScore").html(compScore);
}
}
};
if (userChoice === "scissor") {
if (compChoice === "paper" || compChoice === "lizard") {
yourScore++;
$("#yourScore").html(yourScore);
} else {
if (compChoice === "rock" || compChoice === "spock") {
compScore++;
$("#computerScore").html(compScore);
}
}
};
if (userChoice === "lizard") {
if (compChoice === "spock" || compChoice === "paper") {
yourScore++;
$("#yourScore").html(yourScore);
} else {
if (compChoice === "rock" || compChoice === "scissor") {
compScore++;
$("#computerScore").html(compScore);
}
}
};
if (userChoice === "spock") {
if (compChoice === "rock" || compChoice === "scissor") {
yourScore++;
$("#yourScore").html(yourScore);
} else {
if (compChoice === "lizard" || compChoice === "paper") {
compScore++;
$("#computerScore").html(compScore);
}
}
};
if (round === 5) {
round -=5;
yourScore -= yourScore;
compScore -= compScore;
if (yourScore>compScore) {
$('#win').modal({
keyboard: false
});
} else if (yourScore<compScore) {
$('#lose').modal({
keyboard: false
});
} else if (yourScore==compScore) {
$('#tie').modal({
keyboard: false
});
};
};
});
});
答案 0 :(得分:0)
我做了一个plnkr并意识到你的问题是你只是在相应元素获胜时更新#yourScore
和#computerScore
元素。您应该将元素更新移动到if语句之外。这也简化了你的代码。
if (userChoice === "rock") {
if (compChoice === "lizard" || compChoice === "scissor") {
yourScore++;
} else {
if (compChoice === "paper" || compChoice === "spock") {
compScore++;
}
}
};
if (userChoice === "paper") {
if (compChoice === "rock" || compChoice === "spock") {
yourScore++;
} else {
if (compChoice === "scissor" || compChoice === "lizard") {
compScore++;
}
}
};
if (userChoice === "scissor") {
if (compChoice === "paper" || compChoice === "lizard") {
yourScore++;
} else {
if (compChoice === "rock" || compChoice === "spock") {
compScore++;
}
}
};
if (userChoice === "lizard") {
if (compChoice === "spock" || compChoice === "paper") {
yourScore++;
} else {
if (compChoice === "rock" || compChoice === "scissor") {
compScore++;
}
}
};
if (userChoice === "spock") {
if (compChoice === "rock" || compChoice === "scissor") {
yourScore++;
} else {
if (compChoice === "lizard" || compChoice === "paper") {
compScore++;
}
}
};
$("#yourScore").html(yourScore);
$("#computerScore").html(compScore);
http://plnkr.co/edit/FofGShFtTMED8IY4fP1b?p=preview
另外,在你的胜利检查中,你应该在检查谁赢了之后重置变量,否则它将永远是平局,因为你的分数和compScore都是0。
此外,这看起来像是一些很好的练习,所以我将稍微优化你的解决方案:)
答案 1 :(得分:-1)
您是否尝试过在文档之外移动声明。你仍然可以在里面初始化它们。
i.e.
var round = 0;
var yourScore = 0;
var compScore = 0;
$(document).ready(function() {
...
我刚试过你的解决方案,似乎工作正常。它重置为0,然后在下一个按钮点击它再次增加到1。