0

好的,所以我正在尝试制作一个非常简单的文字角色扮演游戏,但我遇到了问题。我不知道我可以使用什么样的循环来循环这段代码:

getline(cin, response);
if(response == "Pick up the gun")
{
    cout << "You pick up the gun.\n";
    cout << "Knock down the door with it? (Y/N)\n";
    getline(cin, response);
    if(response == "Y")
    {
        cout << "The door opens.\n";
        cout << "You see a zombie.\n";
        cout << "You see an open window.\n";
        cout << "What do you do?\n";
        getline(cin, response);
        if(response == "Shoot the zombie")
        {
            cout << "The zombie dies and it attracts other zombies.\n";
            cout << "GAME OVER!\n";
            cin.get();
            return 0;
        }
        else if(response == "Jump out the window")
        {
            cout << "The zombie does not hear you and you safely make it out!\n";
            cout << "VICTORY!\n";
            cin.get();
            return 0;
        }
    }
    else if(response == "N")
    {
    }
}
else if(response == "Open the door")
{
    cout << "It appears to be locked.\n";
}

因此,如果他们弄错了(例如,他们选择“开门”,它只会说它已锁定,并且由于某种原因它会结束程序。我希望用户能够选择不同的选择,而不是拥有它只是空白或损坏或结束等)它只会循环,但是当我尝试它时,它要么不会结束程序,只是让它我可以输入并且无论如何都不会发生任何事情,或者它绝对是垃圾邮件100次和我不能做任何事情等等。它很奇怪。那我怎么能这样做呢?谢谢!

4

2 回答 2

2

如果你做了很多这样的事情,我建议你让读者更容易......一种选择路径的事情,而不是用户不知道他们应该输入什么。

int GetChoice( vector<string> & choices )
{
    cout << "Do you wish to:" << endl;

    // Output choices 1..N
    for( int i = 0; i < choices.size(); i++ ) {
        cout << "  " << i+1 << ". " << choices[i] << endl;
    }

    // Ask user for their choice.
    for(;;) {
        // This is pretty basic.  You could use getline instead...
        cout << "Enter your choice: ";
        cout.flush();
        int n;
        cin >> n;

        if( n < 1 || n > choices.size() ) {
            cout << "Invalid choice.\n";
            continue;
        }

        // Output the choice and clear the choices vector (so it can be used again)
        cout << "You " << choices[n-1] << endl;
        choices.clear();
        return n;
    }
 }

然后:

 vector<string> opts;
 int choice;

 cout << "You see a door and a gun.\n";
 do {
     opts.push_back( "try the door" );
     opts.push_back( "pick up the gun" );
     choice = GetChoice(opts);
     if( choice == 1 ) cout << "The door appears to be locked.\n";
 } while( choice == 1 );
 if( choice != 2 ) GameQuit();

 cout << "You are armed and dangerous.  Now what?\n";
 opts.push_back( "assault the door with the gun" );
 opts.push_back( "look down the barrel and pull the trigger" );
 choice = GetChoice(opts);

 if( choice == 1 ) {
    cout << "The door crashes from its hinges.\n";
    cout << "You see a zombie and an open window.\n";
    opts.push_back( "shoot the zombie" );
    opts.push_back( "jump out the window" );
    choice = GetChoice(opts);

    if( choice == 1 ) {
        cout << "The zombie explodes and it attracts hundreds more zombies.\n";
        GameOver();
    } else if( choice == 2 ) {
        cout << "You plummet 17 stories to a grisly death.\n";
        GameOver();
    } else {
        GameQuit();
    }
 } else if( choice == 2 ) {
    cout << "Your head explodes.\n";
    GameOver();
 } else {
    GameQuit();
 }

并不是说它很漂亮,但是这种格式允许您随时使用其他选项(例如 'q' 退出,它可能返回零)。

显然,您会看到以这种方式进行游戏会导致大量嵌套块if......最好将游戏安排为状态机,正如之前在评论中所建议的那样。最终,您可以定义一个包含整个游戏的简单文本文件。

于 2012-11-18T22:25:47.377 回答
0

创建一个布尔变量预设为 FALSE。如果用户选择“跳出窗口”,则将布尔变量更改为 TRUE。然后将整个代码放入一个循环中。

while (myBooleanVariable == FALSE) {

  //insert game code

}
于 2012-11-18T21:32:29.573 回答