gpt4 book ai didi

javascript - 在 javascript Rock, Paper, Scissors Game 中记分有问题

转载 作者:行者123 更新时间:2023-11-30 07:49:06 26 4
gpt4 key购买 nike

一段时间以来,我一直在努力解决这个问题,但没有成功。我有一个名为 gameScore() 的函数,它应该跟踪玩家和计算机的得分以及平局。我将分数定义为全局变量,因此从函数内部更新它们应该没有问题。

我的问题是,即使我在控制台中看到谁赢了比赛,但分数没有更新,比赛会无限期地进行下去。非常感谢任何帮助,我的代码在下面。

let playerScore = 0;
let computerScore = 0;
let draws = 0;

//Computer choice
function computerPlay() {
let random = Math.random();
if (random <= 0.3333) {
return "paper";
} else if (random >= 0.6666) {
return "rock";
} else {
return "scissors";
}
}

//Plays one round of RPS
function playRound(playerChoice, computerSelection) {
if (playerChoice === computerSelection) {
return draw;
} else if (playerChoice === "rock" && computerSelection === "scissors") {
return playerWinRound;

} else if (playerChoice === "paper" && computerSelection === "rock") {
return playerWinRound;

} else if (playerChoice === "scissors" && computerSelection === "paper") {
return playerWinRound;

} else {
return computerWinRound;

}
}

//Specifies round win/game win messages
let playerWinRound = "Player wins this round!"
let computerWinRound = "Computer wins this round!"
let draw = "Draw!"
let playerWin = "Player wins the game! Congratulations!"
let computerWin = "Computer wins the game! Congratulations!"


//For loop that plays multiple rounds
for (let i = 0; i < 1000; i++) {
let playerChoice = prompt("Rock, paper, or scissors?").toLowerCase();
const computerSelection = computerPlay();
let roundResult = playRound(playerChoice, computerSelection);
console.log(roundResult);
gameScore(roundResult);
console.log("Your score is " + playerScore);
console.log("The computer's score is " + computerScore);

if (playerScore === 5 || computerScore === 5) {
break;
}
}


//Keeps score and prints out correct messages based on score
function gameScore() {
let result = playRound()

if (result === playerWinRound) {
playerScore++;
} else if (result === draw) {
draws++;
} else {
computerScore++;
}


if (playerScore === 5) {
console.log(playerWin);
return;
}
if (computerScore === 5) {
console.log(computerWin);
return;
}
}

最佳答案

您已经有了该回合的结果,并使用 gameScore(roundResult); 将其传递给 gameScore,但是,您实际上并没有使用它争论,但你创造了另一轮:

 function gameScore() {
let result = playRound()

相反,只接受结果通过:

function gameScore(result) {

关于javascript - 在 javascript Rock, Paper, Scissors Game 中记分有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57397000/

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