无法理解剪刀石头布程序的流程

问题描述

我很抱歉问另一个剪刀石头布的问题。这是我第一次学习编码,我宁愿理解为什么我的代码流程不起作用,然后复制并粘贴正确的答案。

我看到的方式是我正在调用game();然后调用playerSelection(),computerPlay(),最后调用playRound()。然后将这些变量放入playRound()中,该播放器确定if / else语句的输出基础。然后将Game()再循环四次,直到结束为止,并将最终总数记录到控制台。

到目前为止,每次我运行游戏时,总是以五次平局结束,玩家和计算机的得分均为0。我已经摆弄了一段时间,似乎我所做的每件事都“破坏”了程序。非常感谢任何见识!

let playerChoice;
let computerChoice;
let i = 0;
       
function game(){
    
    for (i; i < 5; i++) {
        function playerSelection() {
            let playerChoice = prompt("Rock paper scissors?");
            return playerChoice;
            }
            
    playerSelection();
        

           function computerPlay () {
               let computerChoice = ["rock","paper","scissors"];
                return computerChoice[Math.floor(Math.random()*computerChoice.length)];
            }

    computerPlay();
   
    playRound();
        
        function playRound(playerChoice,computerChoice) {
            let playerscore = 0;
            let compscore = 0;

            if (playerChoice == computerChoice) {
                console.log("Draw!")
            } else if (playerChoice == "rock" && computerChoice == "scissors"){
                console.log("You Win!")
                playerscore++;
            } else if (playerChoice == "paper" && computerChoice == "rock"){
                    console.log("You Win!")
                    playerscore++;
            } else if (playerChoice == "scissors" && computerChoice == "paper"){
                console.log("You Win!")
                playerscore++;
            } else {
                console.log("You Lose!")
                compscore++;
            }
            console.log(playerscore);
            console.log(compscore);
            return;
            }

    }
}


game();

if (playerscore > compscore) {
    console.log("Congratulations! You won the game!")
    } else {
    console.log("You lost the game!")
}

解决方法

总是看到 Draw!的原因是因为您没有使用两个函数playerSelection()computerPlay()的返回值来覆盖playerChoicecomputerChoice