I am trying to make an IF text-based adventure. It takes place in a palace with multiple rooms which I can navigate through using NORTH/SOUTH/EAST/WEST. Does anyone know how to include the use of ITEMS and an INVENTORY? For example I would be able to get a key and use it to unlock a door.
i'l ask a few questions:
-do you have a full design with what you want in it on paper or in mind?
-what is your knowledge of work with classes?
Does anyone know how to include the use of ITEMS and an INVENTORY? For example I would be able to get a key and use it to unlock a door.
i think an elegant idea would be to use a program struture like this:
1 2 3 4 5 6 7
class inventory{
//inventory code here
};
class character : public inventory {
//character code here
}
why i think inheritance is suitable ?
Well you can say that in games an inventory is a "part of" the player.
Whenever you'l need to open a door you can check the inventory for the required item.
how complex is this? It could be as simple as (does he have it, boolean) and (if has it, allowed to use it). Then just a lookup table of the items in the game, if not too huge, ... the entire thing could be done with an enum and a vector<bool> in your class.
If the items are consumable, vector<unsigned char> would let you have 0-255 of an item and increase as find, decrease as used.
If it gets more complex than this, you will soon need that inventory class, but figure out what you need first.....
For example I would be able to get a key and use it to unlock a door.
Lets say the command is "Go East"
You would get the room east.
If room is locked and player has key then response is "OK" else
response "The room is locked"
If Inventory is a class on its own it can also be embedded in a room class.
same can be done with every example of composition you can even implement an inventory inside of the inventory but why would you ? Besides why not embed an inventory inside of a class room ? it can serve as the items laying in the room (which most adventure games naturally have)