Text-based RPG: Inventory Question

Hello everyone! I have a snippet of code here that a fellow member of the forums posted as an example of how an inventory system might work for a text-based RPG. The way I have it set up right now, the weapons should display damage / strength required, the armor should display defense / strength required, and the inventory items should only display weight. The problem, however, is that they're all pulling attributes from each other's classes (i.e. inventory items have a strength requirement when they shouldn't). Here is the code:

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
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>

using namespace std;


class BaseItem {
private:
	string name;
public:
	// define all the types that could be implemented.
	// not the most dynamic, but good enough
	enum ItemType { 
		IT_NONE		 = 0,
		IT_INVENTORY     = 1,
		IT_WEAPON	 = 2,
		IT_ARMOR	 = 3
	};

	BaseItem(const string &n) : name(n) { }
	const string &getName() const { return name; }
	// pure virtual, you can't make an instance of this class
	// which is fine, this class is the common blue print
	virtual bool isItemType(ItemType t) const = 0; 
};

// implement the base methods and add some
class InventoryItem : public BaseItem {
protected:
	int strRequired;
	const int weight;
public:
	InventoryItem(const string &n, int w) : BaseItem(n), weight(w) { }
	virtual bool isItemType(ItemType t) const { return ((t & IT_INVENTORY) != 0); }
	int getWeight() const { return weight; }
};


// implement the base methods and add some
class Weapon : public BaseItem {
protected:
	int damage;
	int strRequired;
public:
	Weapon(const string &n, int d, int str) : 
		BaseItem(n), damage(d), strRequired(str) { }

	Weapon(const string &n, int d) : 
		BaseItem(n), damage(d), strRequired(0) { }

	virtual bool isItemType(ItemType t) const { return ((t & IT_WEAPON) != 0); }
	int getDamage() const { return damage; }
	int getStr() const { return strRequired; }
};

class Armor : public BaseItem
{
protected:
	int defense;
	int strRequired;
public:
	Armor(const string &n, int d, int str) :
		BaseItem(n), defense(d), strRequired(str) { }

	Armor(const string &n, int d) :
		BaseItem(n), defense(d), strRequired(0) { }

	virtual bool isItemType(ItemType t) const { return ((t & IT_ARMOR) != 0); }
	int getDefense() const { return defense; }
	int getStr() const { return strRequired; }
};


int main() {
	// a list of all times, uses a BaseItem pointer to reference
	vector<BaseItem *> items;
	
	// magically, we can add any item pointer with BaseItem as a parent.
	items.push_back(new InventoryItem("Blue Mushroom", 1));
	items.push_back(new InventoryItem("Worn Lantern", 5));
	items.push_back(new Weapon("Rusty Dagger", 4, 2));
	items.push_back(new Weapon("Knotted Staff", 7, 3));
	items.push_back(new InventoryItem("Torn Rope", 7));
	items.push_back(new Weapon("Rusty Sword", 12, 3));
	items.push_back(new Armor("Worn Glove", 5, 3));
	items.push_back(new Armor("Faded Helmet", 4));
	
	// loop through all
	for(int i=0; i<items.size(); i++) {
		// everything behaves here
		BaseItem *item = items[i];
		cout << item->getName();
		
		// check for special case
		if (item->isItemType(BaseItem::IT_INVENTORY)) {
			InventoryItem *inv = (InventoryItem *)item;
			cout << "\tWeight: " << inv->getWeight();
		}
		
		// check for special case
		if (item->isItemType(BaseItem::IT_WEAPON)) { 
			Weapon *wep = (Weapon *)item;
			cout << "\tDamage: " << wep->getDamage();
			cout << "\tStr Req: " << wep->getStr();
		}

		// check for special case
		if (item->isItemType(BaseItem::IT_ARMOR)) {
			Armor *arm = (Armor *)item;
			cout << "\tArmor: " << arm->getDefense();
			cout << "\tStr Req: " << arm->getStr();
		}
		cout << endl;
	}
	cin.get();
	return 0;
}
Last edited on
Hi there,

I find these lines strange:

virtual bool isItemType(ItemType t) const { return ((t & IT_INVENTORY) != 0); }

I would personally write:

virtual bool isItemType(ItemType t) const { return (t == ItemType::IT_INVENTORY); }

Try to adjust those lines in each derived class and see if that helps.

All the best,
NwN
Thank you! That did the trick. ;)
This is solved, but you have to free your allocated memory.
Topic archived. No new replies allowed.