如何让 Javascript 跟踪我的分数增量?

How do I get Javascript to track my score increments?

我正在制作一个基本的剪刀石头布游戏,我以为我正确地编码了分数增量,但游戏将每轮的分数保持为 0。

我尝试在函数内以及全局初始化变量。我尝试在变量增量前添加 return 。我尝试使用和不使用 playRound() 函数末尾显示的 return 分数语句。我知道游戏要么没有修改变量 and/or 一直推迟到初始给定值,我只是想不通为什么或我需要做什么才能让变量保持增量。

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Rock Paper Scissors</title>
</head>

<body>
    <script>
        const choices = ['Rock', 'Paper', 'Scissors'];
        let playerScore = 0;
        let compScore = 0;

        function computerPlay() {
            return choices[Math.floor(Math.random() * choices.length)];
        }

        function playRound(playerSelection, computerSelection, playerScore, compScore) {  
            computerSelection = computerPlay(); 
            playerSelection = prompt("Rock, Paper, or Scissors? ");

            if (playerSelection === 'Rock' && computerSelection === 'Scissors') {
                playerScore += 1;
                return "You win! Rock beats Scissors!";
            } else if (playerSelection === 'Scissors' && computerSelection === 'Paper') {
                playerScore += 1;
                return "You win! Scissors beats Paper!";
            } else if (playerSelection === 'Paper' && computerSelection === 'Rock') {
                playerScore += 1;
                return "You win! Paper beats Rock!";
            } else if (playerSelection === 'Scissors' && computerSelection === 'Rock') {
                compScore += 1;
                return "You lose! Rock beats Scissors!";
            } else if (playerSelection === 'Paper' && computerSelection === 'Scissors') {
                compScore += 1;
                return "You lose! Scissors beats Paper!";
            } else if (playerSelection === 'Rock' && computerSelection === 'Paper') {
                compScore += 1;
                return "You lose! Paper beats Rock!";
            } else {
                playerScore += 1;
                compScore += 1;
                return "Tie!"
            }
            return playerScore;
            return compScore;
        }

        function game() {
            for (let i = 0; i < 5; i++) {
                console.log(playRound());
                console.log(`Your score: ${playerScore}`);
                console.log(`Computer score: ${compScore}`);
            }
            winner();
        }

        function winner() {
            if (compScore > playerScore) {
                console.log("\nThe computer dominated your ass! Better luck next time!")
            } else if (compScore < playerScore) {
                console.log("\nWay to crush it! You win!")
            } else {
                console.log("\nHoly shizzers! It's a tie!")
            }
        }
    </script>
</body>
</html>

您可以通过多种方式改进代码。我删除了您的 playRound() 函数的参数,删除了该函数末尾的 return 语句,并在脚本末尾添加了 game() 调用以使其工作。

playRound() 中的参数强制函数每次都使用未定义的局部值。

你根本没有检查用户给出的输入是否有效。

也许您可以看看这种玩游戏的替代方法:?

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Rock Paper Scissors</title>
</head>

<body>
    <script>
        const choices = ['Rock', 'Paper', 'Scissors'];
        let playerScore = 0;
        let compScore = 0;

        function computerPlay() {
            return choices[Math.floor(Math.random() * choices.length)];
        }

        function playRound() {  
            computerSelection = computerPlay(); 
            playerSelection = prompt("Rock, Paper, or Scissors? ");

            if (playerSelection === 'Rock' && computerSelection === 'Scissors') {
                playerScore += 1;
                return "You win! Rock beats Scissors!";
            } else if (playerSelection === 'Scissors' && computerSelection === 'Paper') {
                playerScore += 1;
                return "You win! Scissors beats Paper!";
            } else if (playerSelection === 'Paper' && computerSelection === 'Rock') {
                playerScore += 1;
                return "You win! Paper beats Rock!";
            } else if (playerSelection === 'Scissors' && computerSelection === 'Rock') {
                compScore += 1;
                return "You lose! Rock beats Scissors!";
            } else if (playerSelection === 'Paper' && computerSelection === 'Scissors') {
                compScore += 1;
                return "You lose! Scissors beats Paper!";
            } else if (playerSelection === 'Rock' && computerSelection === 'Paper') {
                compScore += 1;
                return "You lose! Paper beats Rock!";
            } else {
                playerScore += 1;
                compScore += 1;
                return "Tie!"
            }
        }

        function game() {
            for (let i = 0; i < 5; i++) {
                console.log(playRound());
                console.log(`Your score: ${playerScore}`);
                console.log(`Computer score: ${compScore}`);
            }
            winner();
        }

        function winner() {
            if (compScore > playerScore) {
                console.log("\nThe computer dominated your ass! Better luck next time!")
            } else if (compScore < playerScore) {
                console.log("\nWay to crush it! You win!")
            } else {
                console.log("\nHoly shizzers! It's a tie!")
            }
        }
        game();
    </script>
</body>
</html>

我不知道。

但是对于你的问题我可能有一个答案,那就是停止使用全局变量。

一旦 JavaScript 执行 return 语句,它就会退出过程,因此 playround 的第二个 return 将永远不会被调用。

所以试试这个...

  1. 删除您的全局得分变量。

  2. 不更改分数,而是根据计算机获胜、平局或玩家获胜,将 playround 更改为 return -1、0 或 1。

  3. game 中,声明两个分数变量,并根据 playround 函数 returns 在 for 循环中递增它们.

  4. 将最终得分传给winner

这也更有意义,因为比分在游戏的范围内。

你的游戏现在可以运行了,只知道你没有处理玩家输入 3 次以外的东西的情况

            const choices = ['Rock', 'Paper', 'Scissors'];
            let playerScore = 0;
            let compScore = 0;

            function computerPlay() {
                return choices[Math.floor(Math.random() * choices.length)];
            }

            function playRound(playerSelection, computerSelection) {   //2. you decare computer and playerscore twice,
                computerSelection = computerPlay(); 
                playerSelection = prompt("Rock, Paper, or Scissors? ");

                if (playerSelection === 'Rock' && computerSelection === 'Scissors') {
                    playerScore += 1;
                    return "You win! Rock beats Scissors!";
                } else if (playerSelection === 'Scissors' && computerSelection === 'Paper') {
                    playerScore += 1;
                    return "You win! Scissors beats Paper!";
                } else if (playerSelection === 'Paper' && computerSelection === 'Rock') {
                    playerScore += 1;
                    return "You win! Paper beats Rock!";
                } else if (playerSelection === 'Scissors' && computerSelection === 'Rock') {
                    compScore += 1;
                    return "You lose! Rock beats Scissors!";
                } else if (playerSelection === 'Paper' && computerSelection === 'Scissors') {
                    compScore += 1;
                    return "You lose! Scissors beats Paper!";
                } else if (playerSelection === 'Rock' && computerSelection === 'Paper') {
                    compScore += 1;
                    return "You lose! Paper beats Rock!";
                } else {
                    playerScore += 1;
                    compScore += 1;
                    return "Tie!"
                }
                return playerScore;
                return compScore;
            }

            function game() { 
                for (let i = 0; i < 5; i++) {
                    playRound(); //remove console here.
                    console.log(`Computer score: ${compScore}`);
                    console.log(`Your score: ${playerScore}`);
                   }
                winner();
            }

            function winner() {
                if (compScore > playerScore) {
                    console.log("\nThe computer dominated your ass! Better luck next time!")
                } else if (compScore < playerScore) {
                    console.log("\nWay to crush it! You win!")
                } else {
                    console.log("\nHoly shizzers! It's a tie!")
                }
            }

         game()// 1. You didn't call game()