试图弄清楚如何正确地打破这些嵌套循环

时间:2018-09-13 00:21:46

标签: c++

这是一个简单的游戏,其中有23个木棍,您和计算机可以删除1、2或3,而删除最后一个的人将丢失。正如您可能想象的那样,这是一个课堂项目。

完整代码为here

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    char playAgain = 'Y';
    int totalSticks = 23;
    int humanStickChoice = 1;

    cout<<"****************************   RULES: Remove 1, 2, or 3 sticks per turn.\n";
    cout<<"*Welcome to our Game of 23!*   The player who removes the very last stick\n";
    cout<<"****************************   loses. Good luck!\n";
    cout<<endl;

    while (totalSticks > 0 && playAgain == 'Y' || playAgain == 'y')
    {
        cout<<"There are "<<totalSticks<<" sticks left. How many sticks would you like to remove?:\n";
        cin>>humanStickChoice;
        cout<<endl;

        if(totalSticks >= 3){
            if(humanStickChoice != 1 && humanStickChoice != 2 && humanStickChoice != 3){
                while(humanStickChoice != 1 && humanStickChoice != 2 && humanStickChoice != 3){
                    cout<<"Invalid choice; you can remove 1, 2, or 3 sticks. Choose again:\n";
                    cin>>humanStickChoice;
                }
            }
            totalSticks=totalSticks-humanStickChoice;
            cout<<"Player removes "<<humanStickChoice<<" sticks.\n";
        }
        else if(totalSticks == 3){
            if(humanStickChoice != 1 && humanStickChoice != 2){
                while(humanStickChoice != 1 && humanStickChoice != 2){
                    cout<<"Invalid choice; you can remove 1 or 2 sticks. Choose again:\n";
                    cin>>humanStickChoice;
                }
            }  
        }
        else if(totalSticks == 2){
            if(humanStickChoice != 1){
                while(humanStickChoice != 1){
                    cout<<"Invalid choice; as there are 2 sticks remaining, you can remove only one.\n";
                    cin>>humanStickChoice;
                }
            }
            totalSticks=totalSticks-humanStickChoice; 
            cout<<"Player removes "<<humanStickChoice<<" sticks.\n";
        }
        else if(totalSticks == 1){
            if(humanStickChoice != 1){
                while(humanStickChoice != 1){
                    cout<<"Invalid choice; as there is 1 stick remaining, you can remove only one.\n";
                    cin>>humanStickChoice;
                } 
            }
            int humanStickChoice = 1;
            cout<<"Player removes "<<humanStickChoice<<" stick.\n";
            totalSticks=totalSticks-humanStickChoice;  
            cout<<"*************************************************\n";
            cout<<"Player removed the last stick! Computer wins!";
            if(totalSticks == 0){
                cout<<endl<<"Would you like to play again? If so, input Y and hit return.\n";
                cin>>playAgain;
                totalSticks = 23;
            }
        }
        //This block above is where the logic error is occurring; instead of going back to the
        //beginning of the "main" while loop and starting over with the human player starting, 
        //the totalSticks count is merely replenished and the program continues to go on with the
        //computer making the first move; the human is supposed to make the first move every game.
        if(totalSticks > 4){
            int computerStickChoice = (4-humanStickChoice);
            cout<<"Computer removes "<<computerStickChoice<<" sticks.\n";
            totalSticks=totalSticks-computerStickChoice; 
        }
        else if(totalSticks == 4){
            int computerStickChoice = 3;
            cout<<"Computer removes "<<computerStickChoice<<" sticks.\n";
            totalSticks=totalSticks-computerStickChoice;
        }
        else if(totalSticks == 3){
            int computerStickChoice = 2; 
            cout<<"Computer removes "<<computerStickChoice<<" sticks.\n";
            totalSticks=totalSticks-computerStickChoice;  
        }
        else if(totalSticks == 2){
            int computerStickChoice = 1;
            cout<<"Computer removes "<<computerStickChoice<<" stick.\n";
            totalSticks=totalSticks-computerStickChoice;  
        }
        else if(totalSticks == 1){
            int computerStickChoice = 1;
            cout<<"Computer removes "<<computerStickChoice<<" stick.\n";
            totalSticks=totalSticks-computerStickChoice;  
            cout<<"*************************************************\n";
            cout<<"Computer removed the last stick! You win!";
            if(totalSticks == 0){
                cout<<endl<<"Would you like to play again? If so, input Y and hit return.\n";
                cin>>playAgain;
                totalSticks = 23;
            }
        }
    }




    return 0;
}

导致错误的部分:

else if(totalSticks == 1){
    if(humanStickChoice != 1){
        while(humanStickChoice != 1){
            cout<<"Invalid choice; as there is 1 stick remaining, you can "; 
            "remove only one.\n";
            cin>>humanStickChoice;
        } 
    }
    int humanStickChoice = 1;
    cout<<"Player removes "<<humanStickChoice<<" stick.\n";
    totalSticks=totalSticks-humanStickChoice;  
    cout<<"*************************************************\n";
    cout<<"Player removed the last stick! Computer wins!";

    if(totalSticks == 0){
        cout<<endl<<"Would you like to play again? If so, input Y and hit 
    return.\n";
        cin>>playAgain;
        totalSticks = 23;
    }
}

我调试了每种情况(我认为),这些嵌套循环可能会中断,除了我不知道的情况。

我已经在其上方添加了注释行以进行解释,但我也会在此处进行解释;当玩家在最后一个else-if区块中输了球时,将补充摇杆的数量,并提供cin输入以询问用户是否要玩其他游戏。应该重新开始,让他们扮演第一步,但是,只是继续编写代码,让计算机迈出第一步。

我如何让人类再次迈出第一步?

我尝试使用break;并弄乱了“主要”的while循环要求,但我做对了。

2 个答案:

答案 0 :(得分:0)

使用continue跳过循环主体的其余部分,并立即进行下一次迭代。

使用break完全退出循环。所以最终的测试应该是:

if(totalSticks == 0){
    cout<<endl<<"Would you like to play again? If so, input Y and hit 
return.\n";
    cin>>playAgain;
    if (playAgain == 'y' || playAgain == 'Y') {
        totalSticks = 23;
        continue;
    } else {
        break;
    }
}

有了此更改,也无需在playAgain条件下测试while。这里的测试可以完全处理它。

答案 1 :(得分:0)

时间紧迫,所以不是最理想的方法,但这是代码:

    humanFirstMove = false;
    cout << "There are " << totalSticks << " sticks left. How many sticks would you like to remove?:\n";
    cin >> humanStickChoice;
    cout << endl;

            if (totalSticks == 0) {
            cout << endl << "Would you like to play again? If 
            so, input Y and hit return.\n";
            cin >> playAgain;
            totalSticks = 23;
            humanFirstMove = true;

        }

最后

   //This block above is where the logic error is 
   occurring;instead of going back to the
    //beginning of the "main" while loop and starting over with the human player starting,
    //the totalSticks count is merely replenished and the program continues to go on with the
    //computer making the first move; the human is supposed to make the first move every game.
    if (!humanFirstMove) {
        if (totalSticks > 4) {
            int computerStickChoice = (4 - humanStickChoice);
            cout << "Computer removes " << computerStickChoice << " sticks.\n";
            totalSticks = totalSticks - computerStickChoice;
        }

几乎刚刚添加了一个布尔值