【问题标题】:Game does not finish when I get to 100 points for computer当我电脑达到 100 分时游戏没有结束
【发布时间】:2011-12-07 09:49:07
【问题描述】:

很奇怪,我在这里用 C++ 编写了我的猪游戏,一旦用户或计算机达到 100,他就赢了。但问题是,如果我继续滚动并且超过 100,我不会赢,除非我在到达那里后保持住。如果计算机在 80 到 100 之间接近 100,它只会说它赢了。我不明白这里发生了什么,问题是什么?我做的一切都是正确的!还是我错过了什么?

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

【问题讨论】:

  • 我做的一切都是对的! - 是的,你很快就会学会不信任这种态度。

标签: c++ function while-loop


【解决方案1】:

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

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

应该是

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 所以它在每次移动后都会调用它?
    【解决方案3】:

    试试这个方法:

                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 &lt; 100) &amp;&amp; (computerTotalScore &lt; 100)) 已移至 do-while 循环中。因为每次有人玩时都必须更新它。
    • 函数int computerTurn(int&amp; computerTotalScore)改了,有些地方用computerScore代替computerTotalScore

    【讨论】:

    • 电脑还有能力赢69分,我不明白?
    • 计算机只有在达到 100 或更多时才能获胜。但他能在69岁时获胜吗?如果你想看,我可以粘贴这个游戏的输出。
    • 此外,玩家仍然可以超过 100,除非我持有它,否则我赢了。如果我继续滚动,它就会继续前进。
    • 哇,好用,我看到你做了什么!但另一个问题是,如果人类达到 100,除非我按住,否则他只会继续前进,这是我一直坚持的另一件事。让我在上面的代码中更新我的修复。
    • 这只是我最后一个问题,人类不会自动转到 You Win!除非我在达到 100 时坚持持有...
    【解决方案4】:

    而不是

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多