0

こんにちは。テキスト ベースの RPG ゲームの作成方法を学んでいるのですが、エラーが発生しました。最初に私のコードは次のとおりです。

#include <iostream>
using namespace std;

void newGameFunc();
void titleFunc();
int userInput = 0;
int playerInfo[2];
int playerLocation = 0;


bool running = 1;

int main() {
    while (running) {
        titleFunc();
        if (playerLocation == 1) {
            cout << "You are standing in the middle of a forest. A path veers off to the East and to the West.\n";
            cout << " 1: Go East\n 2: Go West\n";
            cin >> userInput;

            if (userInput == 1) playerLocation = 2; //East
            else if (userInput == 2) playerLocation = 3; //West 
        }
        if (playerLocation == 2) {
            cout << "You are in the Eastern edge of the forest. It's heavilly forested and it's almost imposible to navigate through. You do find 2 flags though.\n";
            cout << " 1: Turn Back\n 2: Pick the FLAG.\n";
            cin >> userInput;

            if (userInput == 1) playerLocation = 1; //Start
            if (userInput == 2) running = 0;
        }
        if (playerLocation == 3) {
            cout << "There is a passage way that leads to a town in the seemingly distant town. There are two guards with shining metal chainmail which scales look as magistic as reptilian scales. Their logo resembles a black dragon spewing a string of fire. They tell you that in order to pass you must give them their lost flags.\n";
            cout << " 1: Give the flags to both guards.\n 2: Turn around.\n 3: Bribe them--NOT AVAILABLE.)\n";
        }
    }
    return 0;
}

void titleFunc() {
    cout << "\t\t\t\t---Fantasee---\n\n\n";
    cout << "\t\t\t\t   1: Play\n";
    cin >> userInput;

    if (userInput == 1) {
        newGameFunc();
    }
    else {
        running = 0;
    }
    return;
}

void newGameFunc() {
    cout << "Welcome to Fantasee, a world of adventure and danger.\n";
    cout << "Since you are a new hero, why don't you tell me a little about yourself?\n";
    cout << "For starters, are you a boy or a girl?\n 1: Boy\n 2: Girl\n";
    cin >> userInput;
    playerInfo[0] = userInput;

    cout << "And what kind of person are you?\n 1: Warrior\n 2: Archer\n 3: All-rounder\n";
    cin >> userInput;
    playerInfo[1] = userInput;
    playerLocation = 1;
    system("cls");
    return;
}

したがって、問題は、私が行ってそれplayerLocation 2に戻りたいときに、ステートメントではなくplayerLocation 1関数を開始するとしましょう。titleFunc();if (playerLocation == 1)

4

1 に答える 1

2

titleFunc()おそらく、while ループの前に入れたいと思うでしょう:

int main() {
    while (running) {
        titleFunc();
        ...

の中へ:

int main() {
    titleFunc();
    while (running) {
        ...

titleFunc()現在行っている方法では、ループのすべての繰り返しで実行を続け、それでゲームをリセットします。

于 2013-10-27T22:33:40.593 に答える