Text based games are actually quite difficult to program efficiently because they are so heavily event-driven.
You will either need to spend a good deal of time designing a storage mechanism for your events, or you will need to have one or more tremendously long "else if" chains.
As Athar says, I don't really recommend a text based game, especially not in the console. A simple Galaga-style shooter with a graphics lib would actually be much easier and would do a better job of introducing you to how program flows in a video game.
If you want to start with a graphic lib, there are many available. SFML and SDL are both very popular (google them). SFML is generally better, faster, and easier to use once it's installed, but is more difficult to install, whereas SDL is easier to install, but a little more difficult to use and not as fast.
Or... if you want to stick with a text adventure game... sit down and buckle up...
Beginners like to hardcode things. You probably have something like a function for every room which prints a description and whatnot. And in that function you watch user input and move to another room or trigger some kind of action depending on what the user inputs.
That is a poor design. One reason it's a poor design is because of what you are trying to do now. You want to have a universal 'I' command that checks your inventory, but you don't want to put that command in every single room in your code. The problem is your underlying design is flawed so there's no way around it unless you redesign.
What you want to do is have some kind of data structure which represents the room. You then write code
that works for ALL rooms, and simply use different
data for each room. This way all your code is in one spot.
For a basic example...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
struct RoomData
{
string name;
string description;
string exits[4]; // N,E,S,W
};
RoomData room1 = {
"Room 1",
"You are in a dark, cold room. There is an exit to the south.",
"","","Room 2",""
};
RoomData room2 = {
"Room 2",
"This room is blazing hot! There is an exit to the north.",
"Room 1","","",""
};
|
As you can see, the south exit in the room1 data leads you to room2, and the north exit in room2 data leads you to room1. Now in your code, you would simply print the description of whatever the current room is, rather than printing static text.
Once you get this foundation set up, adding new rooms requires 0 code changes. All you have to do is add/modify the existing room data.
The reason why text adventure games are so difficult is because room and event data tend to be
significantly more complciated to store in a generic structure like this, since a specific item will often only work in a specific room.
Also -- in the above example I put the room data in code as an illustration, but in practice that isn't wise. You're better off storing that kind of information in an external file (like a text file) and reading it during game execution. That way adding new rooms doesn't even require you to rebuild.