0

学校のプロジェクト用に、C++ でテキスト ベースのアドベンチャー ゲームを作成しようとしています。私が抱えている問題は、gameover() 関数が begin() 関数に移動できる必要があることです。問題は、ゲームオーバー()関数の前にbeginを宣言して、begin()に移動できるようにする必要があることです.gameover()にアクセスする必要がある他の関数があるだけです。要するに、プログラムに伝える方法が必要です関数 gameover() または begin() に移動し、それが存在し、宣言されていることを確認します。Thanks, Simon

void begin() {
   int name;
   int choice1;
   system("cls");
   cout << "To start your adventure, please enter your name." << endl;
   cin >> name;
   cin.ignore();
   system("cls");
   cout << "Hello " << name << " Your adventure now begins.... Who knows what's in store for you!" << endl;
   system("pause");
   system("cls");
   cout << "You find yourself in a dark, cramp library. " << endl;
   cout << "You don't know how you got here, or where you are." << endl;
   cout << "Luckily there is a sword laying on the ground next to you \nand an open door in front.\n" << endl;
   cout << "What will you do?" << endl;
   cout << "1. Pick up the sword" << endl;
   cout << "2. Walk forward through the door" << endl;
   cout << "3. Use the sword to end your miserable existence!" << endl;
   cin >> choice1;
   cin.ignore();
   system("cls");
   if (choice1 == 1) {
      cout << "You quickly pick up the sword and run through the door." << endl;
      system("pause");
      road();
   }
   else if (choice1 == 2) {
      cout << "While you make you way to the door...." << endl;
      cout << "You somehow managed to trip on the sword." << endl;
      cout << "You fall on the hilt smashing your neck, and end your painfully short life. " << endl;
      system("pause");
      gameover();
   }
   else   {
      cout << "That wasn't an option....." << endl;
      cout << "You have now broken the game. Good day Sir!!!" << endl;
   }
 }


 void gameover() {
    int choice_b;
    cout << " Oops! You died.... Try Again." << endl;
    cout << "\n1. Start Again!" << endl;
    cout << "2. Exit" << endl; 

    cin >> choice_b;
    cin.ignore();
    system("cls"); 

    if (choice_b == 1) {
        begin();
    }
    else if (choice_b == 2) { std::exit; }
 }
4

5 に答える 5

0

gameover解決策は、関数と関数を切り離すことbeginです。このことを考慮:

bool continueGame = true;

void gameover()
{
 //your gameover code
 if (userChoosesToExit) continueGame = false;
}


void begin()
{
 //your begin code
 if (playerDies) gameover();
}

void controllerFunction()
{
 while (continueGame) 
 {
  begin();
 }//while

}//controller

このようにして、 の後gameover、プログラム制御はbegin関数を終了し、continueGameが依然として真の場合、while ループはループを継続し、begin再度呼び出されます。このようにして、いつでもbeginと関数を から呼び出すこともできます。それは単に論理構造の問題です。このヒントを使えば、私が投稿したよりも賢いロジックを思いつくことができるでしょう。gameovercontrollerFunction

于 2014-05-11T06:57:48.380 に答える