First, you are absolutely corrected about how this won't scale very well. At all.
The best way I can think of to do this is to prefer thinking of code in terms of data. So, you might have an array of data, containing messages for what to do and a number saying what way allows you to survive. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#include <iostream>
#include <string>
// A data type for a single 'page' in your story.
struct StoryPage {
// the flavour text at the start of your choices.
std::string message;
// list of options - this can be as many as you like. I'll assume that
// there are always only two options to choose from.
std::string option1;
std::string option2;
// message for when you lose.
std::string loss;
// flag to determine which one is correct.
int choice;
};
int main() {
const int numPages = 2; // change this when you add more pages
StoryPage story[2] = {
{
"You start walking and you finally make it out of the woods.\n"
"You are now on a road. To the right you see a sign that says\n"
"'Bubba's Truck Stop.' To the left you see nothing but road.",
"Follow the road that leads to the truck stop.",
"Follow the road that leads to nothing.",
"The road you took is too big for you to reach civilisation, you die of\n"
"hunger and dehydration.",
1
},
{
"(Second Page story background)",
"(Second page option 1)",
"(Second page option 2)",
"(Second page death message)",
2 // lets just say option 2 is correct
}
};
for (int i = 0; i < numPages; ++i) {
std::cout << story[i].message << std::endl;
std::cout << "What do you do?\n" << std::endl;
std::cout << "1. " << story[i].option1 << std::endl;
std::cout << "2. " << story[i].option2 << std::endl;
int choice;
std::cin >> choice;
// if you choose wrong
if (choice != story[i].choice) {
std::cout << story[i].loss << std::endl;
i -= 1; // go back one step and repeat this
}
// otherwise go on to the next difficulty.
}
std::cout << "You won!" << std::endl;
return 0;
}
|
As you can see, you can just keep on adding to that massive array at the start of your program, and everything should fit in neatly. If you like, you can even add more choices, without much difficulty (maybe see if a certain choice message exists and don't print it if it doesn't). You could even store all of this in a file and load it when you run your program (what I'd do, personally), though that would be a bit harder. Using
std::vector instead of a C-style array would make that a lot easier, though.
It also gets a bit more complicated if your story stops being linear - rather than storing which option doesn't kill you, you should store what choice will go to what 'page', and change 'i' to based on that. Hope you enjoy looking through this, and that it gives you an idea of what to do - I spent far too much time typing of all of this up...
EDIT:
Like this, its also really easy to restart the game. Say, check if they enter -1 or 0 or something, and then just set i to -1. That way it will loop back to 0 and you get to start the game from the beginning again!
Also, if you are wondering what I did with the strings,
"Hello" "World"
becomes just
"HelloWorld"
. Its a useful trick for breaking up long strings onto multiple lines. Make sure that you have the comma at the end of each entry, though.