Functions in a Text Based Adventure

In my class we have started programming a Text Based Adventure and the instructor has not yet taught functions and i am impatient, I need to know how to jump around in my code so that i would be able to go from room A to room B then have the choice of going to room C, D, or back to A without have a ridiculous amount of code. From what i have been told in order to do this i would have to use functions, what would be the easiest way of going about doing this?
Last edited on
Without knowing what you have been taught and the parameters for the adventure game, it is hard to recommend a strategy to you. But you are much safer asking these sorts of questions in the beginner forum, where the members are more sensitive to these sorts of things. The answers in the general forum tend to assume a more broad-based knowledge of the language.
Well, you need to sit down and design how you want things to be in your text based adventure.

The simplest implementation I would think would have to use classes to describe the room, with each room having up to four exits (north, south, east, west) which are pointers to other rooms. The current room is held as a reference by the main loop, which will get the list of possible exits (possibly stored as a vector), display the options to the user and then take appropriate action (change the "current" object).
*cough*cough*

http://cplusplus.com/forum/articles/28558/

Text based adventure games are harder to design well than you may think. Start with an easier game like a simple vertical shooter or something. And don't use the console.
Last edited on
Disch is correct of course. Text based adventures aren't really as simple as some would expect. I'm assuming you're meant to make something more MUD (sans mobs) than nethack?
Last edited on
I have made a text based adventure before in python and it wasnt very tough, we're using dev-C++, think of a game more along the lines of zork

I know randomization using rand() and srand(), loops using while, if statements, etc.
Last edited on
Was it good though? Creating something to allow you to move through a series of "rooms" with simple commands is indeed fairly simple, creating an "adventure" is another matter entirely.

More on point: yes, functions are pretty much the basic ingredient of anything C++. The rand, srand, while, if you mention are all functions built by other people.

http://www.cplusplus.com/doc/tutorial/functions/ will give you the basics of the syntax, it isn't all that different to the function you probably used in Python, apart from having the C++ type identifiers for the input/output.
Okay so in python we had something like

def rooma();
}
if (choice == left)
then roomb();
}

def roomb();
}
if (choice == right)
then rooma();
if (choice == left)
then roomc();

}



and so on, and that allowed us to jump around the code quite a bit, im not sure if i was told the wrong thing to use to allow me to do this in C++ but from what ive been told you would use functions to hop around your code like that.






Hmm, yes. You've confused your syntax quite a bit there. Python tends to avoid semicolons with something approaching pathological fear.

That's a very inefficient way to do what you're wanting, as I'm assuming you're taking user input in each room? It's almost a recursive approach to text-based game navigation. :) Your call stack just keeps on growing and growing until the program ends, which is generally considered a bad thing if there's not some definite end in sight.

As I said before, seperating the rooms and the user navigation function would make for a much cleaner design.
Sorry, meant to start with: When you use the def keyword in Python you're defining a function so you already understand the concept.
Okay, so now lets say i want to do that in C++ how would i go about jumping back to previous pieces of code?
If that's the way you want to do it...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void roomA()
{
  // Get User Choice
  if ( choice == "roomB" )
  {
    roomB();
  }
}
void roomB()
{
  // Get User Choice
  if ( choice == "roomA" )
  {
    roomA();
  }
}
I apologize if this is a stupid question but i havent done much coding in 2 years but would i use those in int main(int argc, char *argv[]) or would those be completely off on their own?
When i try that i get this error "expected unqualified-id before '{' token "
What I've posted won't work without change from you (ie, putting in the code to get and validate input from the user).

Yes, you could start it off with a call to roomA() in your main function.
So i know this isnt right but am i on the right track?

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
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

string choice;

void roomA()
{
 cout << "You are in room A" << endl;
 cin >> choice;
  if ( choice == "roomB" )
  {
    roomB();
  }
}
void roomB()
{
 cout << "You are in room B" << endl;
 cin >> choice;
  if ( choice == "roomA" )
  {
    roomA();
  }
}

int main(int argc, char *argv[])
{
    roomA()
    system("PAUSE");
    return EXIT_SUCCESS;
}
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
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

string choice;
void roomB();
void roomA();

int main(int argc, char *argv[])
{
    
void roomA();
{
 cout << "You are in room A" << endl;
 cin >> choice;
  if ( choice == "roomB" )
  {
    roomB();
  }
}
void roomB();
{
 cout << "You are in room B" << endl;
 cin >> choice;
  if ( choice == "roomA" )
  {
    roomA();
  }
}

With this ^^
im getting :
1
2
  [Linker error] undefined reference to `roomA()' 
  [Linker error] undefined reference to `roomB()' 
Last edited on
You need to close the main() function. And put something in there.

So, around line 12 you should have something similar to what you had on line 30-33 in your previous post.
Topic archived. No new replies allowed.