This probably isn't what you want to hear... but here's my advice:
If you're hardcoding everything (like you are -- putting every possible condition in separate if statements like that), then you're probably doing it the wrong way. As you can imagine, writing a full game like this would become overwhelming.
Notice how you have to duplicate a lot of code for each location (ie: check to see if the player typed a certain command, like "look", etc). Not only is this tedious and repetitive, but it's also error prone. What if you forget to specify "look" for one area? Or mistype it as "looj" or something? This will confuse the player... they'll wonder why "look" works for some areas but not for others.
A better way to structure this would be to make classes to represent each area. Maybe even derive them from a common 'Location' class or something:
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
|
struct Action
{
// stuff to specify different actions to take, such as move to a new location
// quit the game, restart, etc... whatever
};
// the generic "Location" abstract base class
class Location
{
public:
virtual ~Location() { }
// have functions for various commands the user can input
virtual Action Look() = 0;
virtual Action DoSomethingElse() = 0;
};
// then specialize individual locations:
class Location_OutsideDarkCave
{
public:
Action Look()
{
cout << "You are outside the dark cave. Blah blah blah";
return no_action;
}
Action DoSomethingElse()
{
// do whatever else
}
};
|
Game flow would all run through the same function, then. Instead of being repeated for each location. Something like this:
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
|
int main()
{
//...
Location* currentlocation = new Location_OutsideDarkCave; // start outside the cave
Action nextaction = no_action;
string userinput;
while(nextaction.type != quit)
{
// get the user input
getline(cin,userinput);
// figure out what they want to do based on that input
if( /* user wants to look */ )
nextaction = currentlocation->Look();
else if( /* user wants to do something else */ )
nextaction = currentlocation->DoSomethingElse();
// determine how that action changes game flow
if(nextaction.type == move_to_new_area)
{
delete currentlocation;
currentlocation = nextaction.newlocation;
}
}
}
|
Of course you might want to get clever with how you decipher string input. It looks like you will at least want "<verb> <noun>" semantics (as in "attack rat"), in which case you'd have to separate out each word, and determine whether or not they're a verb or a noun.
If you want to get really fancy, you can go "<verb> <noun> <preposition> <noun>", as in "attack rat with sword" or "use bandage on self".
Sound complicated?
IT IS
This is the thing newbies don't tend to realize. Text based adventure games are actually
much, much harder than other types of simple games. If you're interested in making games, you're much better off starting with something simpler, like a Space Invaders or Snake clone.
See this article for more:
http://cplusplus.com/forum/articles/28558/