如何将所有东西放在一个游戏函数中来运行石头、纸、剪刀游戏



我正在完成我的石头,纸,剪刀游戏,我需要一些关于如何执行我已经创建的所有功能并将它们存储到一个主game()函数中的指导。我要完成的是:

  1. 玩5轮。
  2. 跟踪他们的积分。
  3. 检查谁赢得了比赛。
  4. 仅运行用户提示一次(而不是两次(。

以下是完整的代码:https://repl.it/@jl88s/rps

我尝试创建一个for loop以跟踪轮数。此外,检查humancomputer是否达到 5 分的if statement。对我来说,主要问题之一是提示只执行一次,但我不确定如何将humanPlay()函数放置在游戏函数中,这样它就不会运行两次。

function game() {
    for (let rounds = 1 ; rounds <= 5 ; rounds++) {
        let playerSelection = humanPlay();
        let computerSelection = computerPlay();
        console.log(playRound(playerSelection, computerSelection))
        console.log(`Round ${rounds} [ Your score: ${playerScore} | Computer's score: ${computerScore} ]`);
    }
    if (playerScore === 5) {
        return "CONGRATULATIONS - YOU WON!";
    } else if (computerScore === 5) {
        return "THE COMPUTER HAS DEFEATED YOU! YOU LOSE!";
    } else {
        return "It's a draw!";
    }
}

我希望game()函数能够prompt用户一次,并将每轮的结果存储给给定的获胜者,并让游戏达到 5 分之最佳。

您的提示正在执行两次,因为您在game()中调用humanPlay(),然后在playRound()中立即调用,然后执行console.log()

相关内容

最新更新