I’ve just completed the Triple X section of the course and decided to take on a project for myself. I’m trying to make a text adventure. How it works is it presents you with a scenario and a number of choices afterward. Each choice may lead to a another scenario. How I had it planned out looks something like this:
#include <iostream>
void PrintIntroduction()
{
std::cout << "---------------------------------------------------------------------------";
std::cout << std::endl;
std::cout << std::endl;
std::cout << "-- Welcome to my game!\n";
std::cout << "-- Through the course of this game you will be\n";
std::cout << " presented with a scenario and some options.\n";
std::cout << "-- When you have chosen your option, simply type\n";
std::cout << " the number of that option and press enter to continue.\n";
std::cout << "-- Enjoy the game!";
std::cout << std::endl;
std::cout << std::endl;
std::cout << "---------------------------------------------------------------------------";
}
void Sec1()
{
int PlayerChoice;
std::cout << std::endl;
std::cout << "*************************************************************************";
std::cout << std::endl;
std::cout << std::endl;
std::cout << "++ From outside of the void you hear a voice calling to you.\n";
std::cout << "++ \"Awaken, slave. Hear my words and obey!\"\n";
std::cout << "++ From everywhere you feel tendrils of will tugging at your essence.\n";
std::cout << "++ It feels like being drawn from a deep sleep.";
std::cout << std::endl;
std::cout << std::endl;
std::cout << "*************************************************************************";
std::cout << std::endl;
std::cout << std::endl;
std::cout << "What do you do?\n";
std::cout << std::endl;
std::cout << "1 . Try to resist\n";
std::cout << "2 . Surrender your will\n";
std::cin >> PlayerChoice;
if(PlayerChoice == 1)
{
Sec2();
if(PlayerChoice == 2)
{
Sec4();
}
}
}
This is one section. Sec2 and Sec4 would look very similar.
The idea is that the player chooses an option, types the number of that option, presses enter and will be taken to the next section, etc.
Then, to start the whole thing off:
int main ()
{
system("CLS");
PrintIntroduction();
std::cout << std::endl;
std::cout << std::endl;
system("pause");
system("CLS");
Sec1();
}
What this does is it clears the terminal, prints the introduction and then presents the player with a prompt to press any key to continue. When the player presses a key it it clears the terminal again and calls Sec1.
The problem must is probably obvious by now. I didn’t account for the fact that I can’t, for example, call Sec4 from inside of Sec1 because it hasn’t been defined yet (I think). I also don’t believe I can use a loop with something like section++ every time you complete a section because answer 2 in Sec1 will go to Sec4 and at one point, I believe, Sec99 loops back to Sec63, for example.
I’m sure the answer somehow involves parameters and variable scope somehow, but I just figured out how those work today, so I have no idea.
Thanks in advance.
EDIT
So I figured out that if I rearrange the sections so that Sec1 is defined last and then called in main it seems to work so far. However, I’d still like to hear from you guys because there are literally about 100 bits of dialogue, each one a section, and they get thrown out of order quick. So I’m worried that I’ll code them all neatly just to find out that, even if I order them from 100 down to 1, if a choice leads the player to loop back from Sec63 to Sec44 (which might be on a completely different dialogue branch), it might not work because Sec44 isn’t defined before Sec63.