我不明白为什么游戏不记录玩家的分数,它应该在你赢得 5 轮后提醒一条消息,但它没有提醒任何事情,这让我觉得它只是不记录分数运行 game() 函数时。
<script>
const rty = Math.floor(Math.random())
let option1 = "rock"
let option2 = "paper"
let option3 = "scissors"
let playerScore = 0
let computerScore = 0
function getComputerChoice() {
if (rty <= 0.33) {
return option1
}
else if (rty <= 0.66) {
return option2
}
else if (rty <= 0.99) {
return option3
}
}
function play(x, enter) {
x = prompt("Enter your choice").toLowerCase()
enter = getComputerChoice()
if (x === "paper" && enter === "rock") {
return alert("You Win! Paper beats Rock");
playerScore++;
}
else if (x === "rock" && enter === "paper") {
return alert("You Lose! Paper beats Rock");
computerScore++;
}
else if (x === "scissors" && enter === "rock") {
return alert("You Lose! Rock beats Scissors");
computerScore++;
}
else if (x === "rock" && enter === "scissors") {
return alert("You Win! Rock beats Scissors");
playerScore++;
}
else if (x === "scissors" && enter === "paper") {
return alert("You Win! Scissors beats Paper");
playerScore++;
}
else if (x === "paper" && enter === "scissors") {
return alert("You Lose! Scissor beats Paper");
computerScore++;
}
else if (x === enter) {
return alert("It's a tie")
}
else {
return alert("Enter a valid input plox")
}
}
function game() {
let win = "Winner"
let lose = "Loser"
for (let i = 0; i < 5; i++) {
play()
if (playerScore === 5) {
return alert(win)
}
}
}
</script>
我将变量放在全局范围内,这样就不会出现问题了。
您在
return
ing 之后增加分数,这意味着这些行不会被执行。您应该在返回之前增加分数。例如:
if (x === "paper" && enter === "rock") {
// First increment the score
playerScore++;
// Then return
return alert("You Win! Paper beats Rock");
}