I dont know if this is possible, but i need some help

My class is writing a text adventure game, but we haven't learned all of what we need to do it. so we were told to use case switches to go from room to room. I can do that just fine(its basically switch case within switch case). but I'm trying to figure out a way to go BACK through the rooms and i cant understand how to do that.

im sorry if im being vague, i dont know what im talking about
When you are told to go from room to room with a switch case, do you mean something like this?

1
2
3
4
5
6
7
8
9
switch(Direction)
{
     case 'N':
     case 'n':
             // move north PlayerY++;
     // Rest of the directions/actions
}

std::cout << Room[PlayerY][PlayerX];


That's all that is springing to mind right now.
Sounds like you need to work out a data structure to contain the data for each location in the game. This data would include valid directions the player could exit from and pointers to the locations that traveling in that direction would take them. Once these location objects are all made, the code to actually move the player from node to node should be fairly straighforward.

quick pseudocode class example:
1
2
3
4
5
6
7
8
9
class location
{
	string locName;
	string description;
	string north;
	string south;
	string east;
	string west;
}


an object of this class could contain null strings for invalid directions and the locName of other instances of the object representing other valid locations that can be reached from the current location.
the code to do the actuall movement would just use a switch with a case for each direction. each of those cases would check if the corresponding direction string were empty, if so, you don't move, otherwise move the player to the destination.

You could just have a vector containing all the locations and movement between them handled by just searching through the vector until you find the destination locName. Keep in mind, this is just a very rough and inelegant example to show you how it could be done. If the project were large, I'd polish it up a lot and probably come up with a pointer scheme to hold the actual addresses of destination objects, but honestly, you're not writing Crysis 4. You could optimize the hell out of it and nobody would ever notice any difference. Keep it simple.
Last edited on
Sounds like you need to work out a data structure to contain the data for each location in the game. This data would include valid directions the player could exit from and pointers to the locations that traveling in that direction would take them.


Well that definitely looks like what i want. but i have no idea how to go about doing that
Hmm... have you covered vectors and structures/classes in your class yet? If not, then you might need to go with a more primitive option.

http://www.cplusplus.com/doc/tutorial/structures/
http://www.cplusplus.com/reference/vector/vector/
Last edited on
we havent touched vectors yet.
ok.. vectors are just fancy templated arrays which can change size dynamically. And you don't really need to use them for this.

as long as you understand how to make a structure, you can make one something like my example above. Then define an array of those structures. The size of the array, is the number of locations you want in the game world.
I really appreciate the help, but class structures are out of the scope of what i know
Last edited on
Ok. Well, here's a really quick intro to structures:

1
2
3
4
5
6
7
8
9
10
struct location{
	string locName;
	string description;
	string north;
	string south;
	string east;
	string west;
	string up;
	string down;
};


This defines a structure named 'location'. This does not itself create a variable or instance of location, it is just the definition of the type.

You create an instance of that type like this:location loc1;
this creates a location variable named loc1, the same as "int i" creates an integer variable named i.

to reference each member of loc1, use the notation loc1.locName or loc1.east.
1
2
3
location loc1;
loc1.locName = "Bedroom";
loc1.west = "Hallway";


You can create an array of the structures just like an array of any other type:location gameWorld[20];
and refer to the members of any individual element like this:
1
2
gameWorld[5].locName = "Bedroom";
gameWorld[5].west = "Hallway";


Is this enough to get you started?
Last edited on
I think i can get it from there. thank you for the help
Topic archived. No new replies allowed.