当我的电脑积分达到 100 分时游戏还没有结束

发布于 2024-12-10 06:16:50 字数 275 浏览 1 评论 0原文

很奇怪,我在这里用 C++ 编写了我的小猪游戏,一旦用户或计算机达到 100,他就赢了。但问题是,如果我继续滚动并且超过 100,我就不会获胜,除非我在到达那里后坚持下去。如果计算机在 80 到 100 左右接近 100,它就会说它赢了。我不明白这里发生了什么以及问题是什么?我做的一切都是对的!或者我错过了什么?

我该如何解决这个问题,一旦我达到 100 滚动或在用户端保持,它就会获胜,并且不会超过 100,所以如果我保持在 100+,它会告诉我我赢了。我怎样才能让计算机只有在实际达到或自动超过 100 时才获胜?

It is strange, I coded my pig game here in c++ which once the user or computer reaches 100 he wins. But the problem is if I keep rolling and I go past 100 I don't win unless I hold after I get there. And the computer if it gets close to 100 around 80 to 100 it will just say it wins. I don't understand whats going on here and what the problem is? I did everything right! Or am I missing something?

How can I fix this issue where once I reach 100 rolling or holding on the users end that it just wins and doesn't go past 100 so if I hold at 100+ it tells me I win. How can I go about making it so the computer only wins when it actually gets to 100 or past it automatically?

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

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

发布评论

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

评论(4

最美的太阳 2024-12-17 06:16:50

您继续玩的测试应该是 AND 而不是 OR。

if((humanTotalScore < 100) || (computerTotalScore < 100))

应该是

if((humanTotalScore < 100) && (computerTotalScore < 100))

否则两者都需要超过 100 才会停止,而不仅仅是其中之一。

我没有寻找可能存在的任何其他错误,当我发现那个错误时我就停了下来。

Your test to continue play should be AND not OR.

if((humanTotalScore < 100) || (computerTotalScore < 100))

should be

if((humanTotalScore < 100) && (computerTotalScore < 100))

Otherwise both need to be over 100 before it will stop, not just one of them.

I didn't look for any other bugs which may be there, I stopped when I found that one.

坏尐絯℡ 2024-12-17 06:16:50

您是否注意到分配给 continuePlay 的逻辑位于播放循环之外?

Did you notice that your logic that assigns to continuePlay is outside the play loop?

惯饮孤独 2024-12-17 06:16:50

尝试这样:

            do
            {
               if((humanTotalScore < 100) && (computerTotalScore < 100))
               continuePlay = true;
               else
               continuePlay = false;

                {

                    humanTurn(humanTotalScore);
                    computerTurn(computerTotalScore);
                }


                if(humanTotalScore >= 100)
                {
                    cout << "You won!";
                    return 0;
                }
                else if (computerTotalScore >= 100)   
                {

                    cout << "You lost!";
                    return 0;
                } 
           }
             while(continuePlay == true);

int computerTurn(int& computerTotalScore)
    {
            int currentScore = 0;
            int randomDiceRoll;
            cout << " " << endl;
            cout << "The computer will now make its move" << endl;
            cout << " " << endl;
            while ((currentScore < 20) && (computerScore != 1)&&computerTotalScore<100)
            {
                    randomDiceRoll = diceRollFunction();
                    if (randomDiceRoll == 1)
                    {
                            cout << "The computer rolled: ";
                            cout << randomDiceRoll;
                            cout << " " << endl;
                            cout << "The computers total score is: " << computerTotalScore;
                            cout << " " << endl;
                            cout << " " << endl;
                            cout << "The computer has rolled a one, it is now your turn." << endl;
                            cout << " " << endl;

                            break;
                    }
                    else
                    {
                            currentScore += randomDiceRoll;
                            computerTotalScore += randomDiceRoll;
                            cout << "The computer rolled: ";
                            cout << randomDiceRoll;
                            cout << " " << endl;
                            cout << "The computers total score is: " << computerTotalScore;
                            cout << " " << endl;
                            cout << " " << endl;
                    }
            }
            if(currentScore >= 20)
            {
                    computerTotalScore += computerScore;
                    cout << "The computer has reached a max of 20 points for their turn" << endl;
                    cout << " " << endl;
            }

            return computerTotalScore;
    }

进行更改:

  • 由于逻辑缺陷,if 条件已更改。
  • If 条件 if(( humanTotalScore < 100) && (computerTotalScore < 100)) 已移至 do-while 循环。因为每次有人玩时都必须更新。
  • 函数 int computerTurn(int&computerTotalScore) 被更改,在某些地方使用 computerScore 代替 computerTotalScore

Try it this way :

            do
            {
               if((humanTotalScore < 100) && (computerTotalScore < 100))
               continuePlay = true;
               else
               continuePlay = false;

                {

                    humanTurn(humanTotalScore);
                    computerTurn(computerTotalScore);
                }


                if(humanTotalScore >= 100)
                {
                    cout << "You won!";
                    return 0;
                }
                else if (computerTotalScore >= 100)   
                {

                    cout << "You lost!";
                    return 0;
                } 
           }
             while(continuePlay == true);

and

int computerTurn(int& computerTotalScore)
    {
            int currentScore = 0;
            int randomDiceRoll;
            cout << " " << endl;
            cout << "The computer will now make its move" << endl;
            cout << " " << endl;
            while ((currentScore < 20) && (computerScore != 1)&&computerTotalScore<100)
            {
                    randomDiceRoll = diceRollFunction();
                    if (randomDiceRoll == 1)
                    {
                            cout << "The computer rolled: ";
                            cout << randomDiceRoll;
                            cout << " " << endl;
                            cout << "The computers total score is: " << computerTotalScore;
                            cout << " " << endl;
                            cout << " " << endl;
                            cout << "The computer has rolled a one, it is now your turn." << endl;
                            cout << " " << endl;

                            break;
                    }
                    else
                    {
                            currentScore += randomDiceRoll;
                            computerTotalScore += randomDiceRoll;
                            cout << "The computer rolled: ";
                            cout << randomDiceRoll;
                            cout << " " << endl;
                            cout << "The computers total score is: " << computerTotalScore;
                            cout << " " << endl;
                            cout << " " << endl;
                    }
            }
            if(currentScore >= 20)
            {
                    computerTotalScore += computerScore;
                    cout << "The computer has reached a max of 20 points for their turn" << endl;
                    cout << " " << endl;
            }

            return computerTotalScore;
    }

changes made :

  • The if condition was altered due to flaw in logic.
  • The If condition,if((humanTotalScore < 100) && (computerTotalScore < 100)) was moved into the do-while loop. Since it have to be updated each time anyone plays.
  • The function int computerTurn(int& computerTotalScore) was altered,In some places computerScore was used instead of computerTotalScore
傲性难收 2024-12-17 06:16:50

而不是

if((humanTotalScore < 100) || (computerTotalScore < 100))

if((humanTotalScore <= 100) && (computerTotalScore <= 100))

instead of

if((humanTotalScore < 100) || (computerTotalScore < 100))

have

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