如何获得JavaScript来跟踪我的分数增量?

发布于 2025-01-27 03:12:00 字数 2927 浏览 4 评论 0原文

我正在制作基本的岩石剪刀游戏,并认为我正确地编码了分数增量,但是该游戏将分数保持在每轮0。

我尝试在函数和全球范围内初始化变量。我尝试在变量增量的前面添加返回。我尝试使用或不使用返回分数语句显示在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(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>

I am making a basic Rock Paper Scissors game and thought I was coding the score increments correctly, but the game keeps the scores at 0 each round.

I tried initializing the variables within the function as well as globally. I tried adding return in front of the variable increments. I tried with and without the return score statements shown at the end of the playRound() function. I understand that the game either isn't modifying the variable and/or keeps deferring to the initial given value, I just can't figure out why or what I need to do to get the variables to maintain the increments.

<!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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲世九天 2025-02-03 03:12:00

您可以通过多种方法来改进代码。我删除了您的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>

There are many ways in which you can improve your code. I removed the arguments of your playRound() function, removed the return statements at the end of that function and added the game() call at the end of your script to make it work.

The arguments in playRound() forced the function to work with undefined local values each time.

You are not checking at all, whether the input given by the user is a valid one.

Maybe you can check out this alternative way of doing the game: rock,paper,scissors?

<!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>

最冷一天 2025-02-03 03:12:00

您的游戏现在正在运行,只知道您没有处理三次以外的球员进入某些东西的情况

            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()

Your game is working now, just know that you didn't handle situation where player enter something outside the three times

            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()

眼前雾蒙蒙 2025-02-03 03:12:00

我不知道。

但是我可能会解决您的问题,即停止使用全局变量。

JavaScript执行返回语句后,它将退出该过程,因此PlayRound的第二个返回永远不会被调用。

因此,请尝试一下...

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

  2. 根据计算机获胜,领带或播放器获胜,请更改PlayRound以返回-1、0或1。

  3. game中,声明您的两个分数变量,并根据循环在循环中递增它们,请playRound函数返回。

  4. 将最终分数传递给获胜者

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

I don't know.

But I might have an answer to your problems, which is to stop using global variables.

Once JavaScript executes a return statement, it exits the procedure, so playround's second return will never be called.

So try this...

  1. Remove your global score variables.

  2. Instead of changing the score, change playround to return -1, 0, or 1 based on if computer wins, a tie, or player wins.

  3. In game, declare your two score variables, and increment them within your for loop based on what your playround function returns.

  4. Pass the final score to winner.

This makes more sense as well, because the score is within the scope of a game.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文