Help with Texted Based game.

Hi! I'm new to C++ and I'm in the process of making a texted based game and I've come up with a problem. How do you make the storyline change based on your actions that you make in the game? Would I make one big if else statement with many if else statements inside? I've tried making one big switch statement and that didn't work out. I would appreciate any help.
I think it would be best to use a graph-like structure to hold all scenes. However that is somewhat complex to make. A switch should work fine. Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool play = true;
int state = 0;
string input;
while (play) {
   switch (state) {
      case 0:
         cout << "go left or go elevator? ";
         getline (cin, input);
         if (input == "go left") state = 1;
         else if (input == "go elevator") state = 0;
         else cout << "invalid input!\n";
         break;
      case 1:
         cout << "you wen't left and were eaten by wild rabbits!\n";
         play = false;
         break;
      case 2:
        cout << "you wen't elevator and escaped the rabbits!\n";
        play = false;
        break;
   }
}
(not tested)

Also, obligatory link, http://cplusplus.com/articles/G13hAqkS/
Last edited on
Thanks hamsterman. I haven't tried the code out yet because I've took the concentration of making a 2D game because of the link you posted. If I where to make a 2D game would I still be able to use the code you gave me and use it in the 2D game storyline?
Even though some people on this forum seems to hate console games, they're pretty fun to make (and play) with libraries like NCurses/PDCurses etc. At least I think so.
Last edited on
Topic archived. No new replies allowed.