C++ Text Adventure - Inventory Class?

I'm working on a text based RPG for our last big project for school. I'm still developing all of the Classes and such, but I'm trying to just work on small chunks at a time so I can be constantly checking to make sure it all runs.

The big issue I'm running into (ant after not finding any specific help by Googling), is creating the Inventory. I was thinking of doing it in a class, but I'm getting hung up on how to store all of the info on each item.

I have
Weapons
- Axe
- Sword
- Fireball

Armor
- Shield
- Helmet
- Forcefield

Potions
- Health Potion
- Increase Attack Power
- Increase Defense

Right now, I have the inventory system set up as a function, but I don't think this is the cleanest way to code. Also, I'm not sure how to access the items of the inventory, such as in battle. Here's the inventory function right now:

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <vector>
/*
#include "Armor.h"*/
#include "Monster.h" 
#include "Player.h"
#include "Inventory.h"
//#include "Potion.h"
//#include "Weapon.h"
#include "RPGGameConfig.h"

using namespace std;
/*
void Welcome(){

	//Player myPlayer;

	cout << "Welcome to this RPG!" << endl;
	cout << "Let's start with some basics." << endl;
	cout << "What is your name? " << endl;
	cin >> myPlayer._sPlayerName;

	cout << "Hello, " << myPlayer._sPlayerName << "!" << endl;
	cout << "Your starting stats are as follows: " << endl;
	cout << "Health: " << myPlayer._iPlayerHealth << endl;
	cout << "Defense: " << myPlayer._iPlayerDefense << endl;
	cout << "Attack Power: " << myPlayer._iPlayerStrength << endl;
	cout << "Gold: " << myPlayer._iStartingGold << endl;

	//cout << "Welcome to the world of 
}

//void Item::YourInventory(){

	//playerItemCount = 0;

//} */

void Storefront(){

	vector<string> inventory;
	vector<string>::iterator myIterator;
	vector<string>::const_iterator iter;

	Player myPlayer;
	bool bExitOrStay = true;
	int iChoice;
	int iWeaponChoice;
	int iArmorChoice;
	int iPotionChoice;
	int iExit;
	int iWeaponCost = 200;

	cout << "Welcome to the storefront!" << endl;

	do{
		if(myPlayer._iStartingGold > 0)
		{
			cout << "\nType in the number of whatever item you'd like to purchase." << endl;
			cout << "1 - Weapons, 2 - Armor, 3 - Potions, 4 - Exit: " << endl;
			cin >> iChoice;		

			switch(iChoice)
			{
			case 1:
				cout << "Choose a Weapon!" << endl;
				cout << " 1 - Axe (35 damage, +50 health) : cost 200G" << endl;
				cout << " 2 - Sword (55 damage, +25 health) : cost 300G" << endl;
				cout << " 3 - Fireball (60 damage, +10 health) : cost 400G" << endl;
				cin >> iWeaponChoice;
				if(iWeaponChoice == 1)
				{
					cout << "You bought an Axe. \nIt has been added to your inventory." << endl;
					inventory.insert(inventory.begin(), "Axe");
					iWeaponCost = 200;
					myPlayer._iPlayerHealth = myPlayer._iPlayerHealth + 50;
					myPlayer._iPlayerStrength = myPlayer._iPlayerStrength + 35;
					myPlayer._iStartingGold = myPlayer._iStartingGold - iWeaponCost;
				}
				else if(iWeaponChoice == 2)
				{
					inventory.insert(inventory.begin(), "Sword");
					cout << "You have bought a Sword." << endl;
					iWeaponCost = 300;
					myPlayer._iPlayerHealth = myPlayer._iPlayerHealth + 25;
					myPlayer._iPlayerStrength = myPlayer._iPlayerStrength + 55;
					myPlayer._iStartingGold = myPlayer._iStartingGold - iWeaponCost;
				}
				else if(iWeaponChoice == 3)
				{
					inventory.insert(inventory.begin(), "Fireball");
					cout << "You have bought a Fireball." << endl;
					iWeaponCost = 400;
					myPlayer._iPlayerHealth = myPlayer._iPlayerHealth + 10;
					myPlayer._iPlayerStrength = myPlayer._iPlayerStrength + 60;
					myPlayer._iStartingGold = myPlayer._iStartingGold - iWeaponCost;
				}
				cout << "You have this in remaining gold: " << myPlayer._iStartingGold;
				break;
			case 2:
				cout << "Choose your armor." << endl;
				cout << " 1 - Shield (+20 defense) : cost 100G" << endl;
				cout << " 2 - Helmet (+10 defense) : cost 50G" << endl;
				cout << " 3 - Forcefield (+30 defense) : cost 200G" << endl;
				cin >> iArmorChoice;
				if(iArmorChoice == 1)
				{
					inventory.insert(inventory.begin(), "Shield");
					cout << "You have bought a Shield." << endl;
					myPlayer._iPlayerDefense = myPlayer._iPlayerDefense + 20;
					myPlayer._iStartingGold = myPlayer._iStartingGold - 100;
				}
				else if(iArmorChoice == 2)
				{
					inventory.insert(inventory.begin(), "Helmet");
					cout << "You have bought a Helmet." << endl;
					myPlayer._iPlayerDefense = myPlayer._iPlayerDefense + 10;
					myPlayer._iStartingGold = myPlayer._iStartingGold - 50;
				}
				else if(iArmorChoice == 3)
				{
					inventory.insert(inventory.begin(), "Forcefield");
					cout << "You have bought a Forcefield." << endl;
					myPlayer._iPlayerDefense = myPlayer._iPlayerDefense + 30;
					myPlayer._iStartingGold = myPlayer._iStartingGold - 200;
				}
				cout << "You have this in remaining gold: " << myPlayer._iStartingGold << endl;
				break;
			case 3:
				cout << "Choose a potion!" << endl;
				cout << " 1 - Health Potion (+20 health) : cost 75G" << endl;
				cout << " 2 - Increase Attack Power (+20 damage) : cost 125G" << endl;
				cout << " 3 - Increase Defense (+50 health) : cost 200G" << endl;
				cin >> iPotionChoice;
				if(iPotionChoice == 1)
				{
					inventory.insert(inventory.begin(), "Health Potion");
					myPlayer._iStartingGold = myPlayer._iStartingGold - 75;
					cout << "You have bought a Health Potion." << endl;
					cout << "You have this in remaining gold: " << myPlayer._iStartingGold << endl;
				}
				else if(iPotionChoice == 2)
				{
					inventory.insert(inventory.begin(), "Increase Attack Power");
					myPlayer._iStartingGold = myPlayer._iStartingGold - 125;
					cout << "You have bought an Increase Attack Power Potion." << endl;
					cout << "You have this in remaining gold: " << myPlayer._iStartingGold << endl;
				}
				else if(iPotionChoice == 3)
				{
					inventory.insert(inventory.begin(), "Increase Defense");
					myPlayer._iStartingGold = myPlayer._iStartingGold - 200;
					cout << "You have bought an Increase Defense Potion." << endl;
					cout << "You have this in remaining gold: " << myPlayer._iStartingGold << endl;
				}
				break;
			case 4:
				cout << "You just chose to leave the store." << endl;
				cout << "Have a great day!" << endl;
				bExitOrStay = false;
				break;
			}
		}
		else
		{
			cout << "You are out of money!" << endl;
			bExitOrStay = false;
		}
		
		
	}while(bExitOrStay != false);

	cout << "Your inventory: " << endl;
	for(iter = inventory.begin(); iter != inventory.end(); ++iter)
		cout << *iter << endl;

	cout << "Your stats:" << endl;
	cout << "Health: " << myPlayer._iPlayerHealth << endl;
	cout << "Attack Power: " << myPlayer._iPlayerStrength << endl;
	cout << "Defensive Strength: " << myPlayer._iPlayerDefense << endl;
	system("pause");


}



My Request: Can someone help me in creating a class that stores the inventory items and the attributes of each item so that I can call upon one of those items in battle or any time while playing the game?
How do you want to store the inventory? as integers representing items, strings,etc...etc...

I would make a class with the functions:
GetEquipped() - return equipped weapon
SetEquipped(int ItemID) - set current equipped weapon, checks if in inventory
AddItem(int ItemID) - add item into backpack, displays message if inventory is full
RemoveItem() - remove item, displays message if item is not found
EmptyBackpack() - clear entire backpack

there could be more, im not quite sure how in depth you want your inventory class to be(handle gold,player values...). These would be some basic functions you want.
I would love to see how this works, if you would share your .h files or zip everything and send me a email.

----This is more concept planning than programming----

I've thought about doing something like this for fun.
Sooner or later you'll want many types of weapons and armor.
You may want a short sword and a long sword, then you may also want wood, iron, steel, and magic ones :)

So I would create Item table for each type game item
Armor, Weapons, Magic, Spells, Potions, MISC
One you can see a number and know exactly what it is
Based on the 1st number, you can say if it will work in slot 0-9
Two you can randomly create a new item on a vendor or a monster.
three you can update one Wpns list easier than one List with everything
and I would add one entry in your table for description.
You could make item # 1000 one that has not been identified
If you want, one number could be the bonus
You might also use letters for more options, or add more numbers.

Item# Name value damage bonus
1000 unidentified wpn
1101 wood sword 10 gp 1-6 0
1502 Magic W Sword 10 gp 2-7 +1
1303 Long sword 50 gp 2-8 0
1504 Magic L sword 50 gp 4-10 +2

Change the First Item # for armor
Item# Name value Protection bonus
2000 unidentified armor
2101 Wood sheild 10 10 0
2202 Leather sheild 10 10 5
2303 Iron sheild 50 30 0
2504 Magic sheild 50 30 10

To use, buy, or sell, you would call the item #

To random make a item
1 could be wpns
2 armor or shields

The second number would be the type
1 wood
2 leather(not used with swords) maybe replace leather with stone.
3 iron
4 steal
5 magic

Then you could have 99 types of each item or add a number for 999 types.
Your inventory is then just a list of numbers and quantities.

Anyway, wish you the best of luck :)
You need to think a lot about how you want your inventory to work. How many items is the player allowed to carry? Can they carry multiples of the same item. If I were doing this I would have an inventory class which controls all the items the player has eg stores them, manipulates them, drops them/uses them. Then I would have another class for items in which specify the unique abilities of each item. Then the inventory would control when they are used (based on player input) and the item would control eactly what the result.

1
2
3
4
5
6
class Inventory
    item items[];
    items[0].type = Healthpotion

void useItem()
    items[0].Use();


Then you could give both the player and the shop an inventory object and pass items (and money) between the two.
Also to use polymorphism:
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
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Item
{
public:
  string name;
  int weight;
  int value;

  Item(string n, int w, int v) : name(n), value(v), weight(w) {}

  virtual void print(void) {}
};

class Weapon : public Item
{
public:
  int damage;

  Weapon(int d, string n, int w, int v) :damage(d), Item(n, w, v) {}

  void print(void) { std::cout << name << ": " << weight << " " << value << " " << damage << endl; }
};

class Armor : public Item
{
public:
  int block;
  
  Armor(int b, string n, int w, int v) :block(b), Item(n,w,v) {};
  
  void print(void) { std::cout << name << ": " << weight << " " << value << " " << block << endl; }
 };

class Inventory
{
public:
  vector<Item*> items;
};

int main(void)
{
  Inventory myInventory;
  Item *myFirstWeapon = new Weapon(6, "Sword", 5, 10);
  Item *myFirstArmor = new Armor(3, "Shield", 4, 8);
  myInventory.items.push_back(myFirstWeapon);
  myInventory.items.push_back(myFirstArmor);
  

  for (int i = 0; i < myInventory.items.size(); i++)
     myInventory.items[i]->print();

  for (int i = 0; i < myInventory.items.size(); i++)
    delete myInventory.items[i];

  return 0;
}
Last edited on
Topic archived. No new replies allowed.