Interactive Fiction

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.
@globaltourist,
Well you can say that in games an inventory is a "part of" the player.

Yes, but then it should be part of the player. Inheritance doesn't make any sense then.

@OP,
here are plenty of post about this topic already.
http://www.cplusplus.com/search.do?q=text+based+game
Tutorial with source code
https://www.youtube.com/watch?v=oqvNEI6Wf2A
@Thomas1965,
Yes, but then it should be part of the player.

I did make it a part of the playes, class character is the player

Inheritance doesn't make any sense then.

In another thought i do agree with you, i wrote this in a rush before going to college i think a "has a" design would be much better:

1
2
3
4
5
6
7
8
class inventory{
//inventory code here
};

class character {
  inventory playerInv;

}
If Inventory is a class on its own it can also be embedded in a room class.
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.....
Last edited on
IF you want an object-oriented solution you might consider the following structure - just a plan, not working yet:
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>
#include <vector>
#include <map>

using namespace std;

struct Item
{
  string name;
  string description;
  int weight;
};

class Inventory
{
public:
  void AddItem(Item item);
  void RemoveItem(Item item);
  bool HasItem(Item item);
private:
  vector<Item> items;
};

class Room
{
private:
  map<string, Room> exits;
  string name;
  string description;
  Inventory inventory;
};

class Player
{
private:
  Inventory inventory;
};

struct Action
{
  string command;
  string param;
};

class CommandParser
{
public:
  Action Parse(const string& txt);
private:
  vector<string> commandWords;
};

class Game
{
public:
  Game();
  void Run();
private:
  Room location;
  vector<Room> rooms;
};

int main()
{
  try
  {
    Game game;
    game.Run();
  }
  catch (const exception& ex)
  {
    cerr << "Error: " << ex.what();
    return -1;
  }
  catch (...)
  {
    cerr << "Unknown error: ";
    return -2;
  }
  return 0;
}


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)
This is the beginning of my code. How would I do that using enum nouns as the items and storing them in an inventory?

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
 5  #include <iostream>                                                                
  6  #include <string>                                                                  
  7  #include <vector>                                                                  
  8                                                                                     
  9  using namespace std;                                                               
 10                                                                                     
 11  enum en_DIRS {NORTH, EAST, SOUTH, WEST};                                           
 12  enum en_ROOMS {ENTRANCE, SMALL, KITCHEN, MAIN, LIBRARY, DINING, BACK, BED, LOCKED};
 13  enum en_VERBS {GET, DROP, USE, EXAMINE, INVENTORY, LOOK};                          
 14  enum en_NOUNS {BACK_DOOR, KEY};                                                    
 15                                                                                     
 16  const int NONE = -1;                                                               
 17  const int DIRS = 4;                                                                
 18  const int ROOMS = 9;                                                               
 19  const int VERBS = 6;                                                               
 20  const int NOUNS = 2;                                                               
 21                                                                                     
 22  struct word {                                                                      
 23     string word;                                                                    
 24     int code;                                                                       
 25  };                                                                                 
 26                                                                                     
 27  struct room {                                                                      
 28     string description;                                                             
 29     string look_around;                                                             
 30     int exits_to_room[DIRS];                                                        
 31  };          
 33  struct noun {                                                                      
 34     string word;                                                                    
 35     string description;                                                             
 36     int code;                                                                       
 37     int location;                                                                   
 38     bool can_carry;                                                                 
 39  };                                                                                 
 
Topic archived. No new replies allowed.