Want criticism/advice/thoughts

I am writing a text-adventure and I am in the beginning stages, I would like to submit my code for review and give me a brief overview of how I am doing. I would like my organization, effeciency, and logic to be considered. Also, Can you foresee any problems down the road? Thank you very much.

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
#include "itemclass.h"
#include <iostream>
#include <vector>
#ifndef CLASSPLAYER_H
#define CLASSPLAYER_H

class classPlayer
{
	public:
		std::string returnName();
		void setName(std::string settingName);
		int returnDefense();
		int returnAttack();
		int returnMagic();
		
		std::vector <itemClass> inventory;
		
		/*Inventory management functions */
		void showInventory();
		void showHUD();
		void addItem(const itemClass& item);
		void removeItem(std::string itemName);
		void equipItem(std::string itemName);
		void unequipItem(std::string itemName);
	private:
		
		std::string playerName;
		int health = 10, baseDefense = 1, baseAttack = 1, baseMagic = 1;
		int level = 1, totalExperience = 0;
		
		bool weaponSlotTaken = false;
		std::string weaponSlotItem;
};

#endif 

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
#include "itemclass.h"
#include <iostream>
#include <vector>
#include "classplayer.h"

std::string classPlayer::returnName()
{
	return playerName;
}

int classPlayer::returnDefense()
{
	return baseDefense;
}

int classPlayer::returnAttack()
{
	return baseAttack;
}

int classPlayer::returnMagic()
{
	return baseMagic;
}

void classPlayer::setName(std::string settingName)
{
	playerName = settingName;
}

void classPlayer::showInventory()
{
	if ( inventory.empty() )
	{
		std::cout << "You currently have no items in your inventory, better get looting!" << std::endl;
	}
	else
	{
		std::cout << "You currently have " << inventory.size() << " items in your inventory. Huzzah!" << std::endl;
		for ( int a = 0; a < inventory.size(); a++ )
		{
			std::cout << inventory[a].showItemName();
			if (inventory[a].showItemName() == weaponSlotItem )
			{
				std::cout << " [Equipped]" << std::endl;
			}
			else
			{
				std::cout << std::endl;
			}
		}
	}
}

void classPlayer::showHUD()
{
	std::cout << "Attack power " << returnAttack() << " | Defensive power " << returnDefense() <<  " | Magic power " << returnMagic() << " | Health " << health << std::endl;
	std::cout << "You are level " << level << " with " << totalExperience << " experience." << std::endl;
}

void classPlayer::addItem(const itemClass& item)
{
	inventory.push_back(item);
}

void classPlayer::removeItem(std::string itemName)
{
	for ( int a = 0; a < inventory.size(); a++ )
	{
		if ( itemName == inventory[a].showItemName() )
		{
			if ( inventory[a].showItemName() == weaponSlotItem )
			{
				weaponSlotItem = "Empty";
				weaponSlotTaken = false;
				baseAttack = baseAttack - inventory[a].returnDamage();
				baseDefense = baseDefense - inventory[a].returnArmor();
				baseMagic = baseMagic - inventory[a].returnMagic();
			}
			inventory.erase(inventory.begin() + a);
		}
	}
}

void classPlayer::equipItem(std::string itemName)
{
	if ( weaponSlotTaken )
	{
		for ( int a = 0; a < inventory.size(); a++ )
		{
			if ( inventory[a].showItemName() == weaponSlotItem )
			{
				baseAttack = baseAttack - inventory[a].returnDamage();
				baseDefense = baseDefense - inventory[a].returnArmor();	
				baseMagic = baseMagic - inventory[a].returnMagic();
				weaponSlotTaken = false;
				weaponSlotItem = "Empty";
			}
		}
	}
	
	for ( int a = 0; a < inventory.size(); a++ )
	{
		if ( inventory[a].showItemName() == itemName )
		{
			baseAttack = baseAttack + inventory[a].returnDamage();
			baseDefense = baseDefense + inventory[a].returnArmor();
			baseMagic = baseMagic + inventory[a].returnMagic();
			weaponSlotTaken = true;
			weaponSlotItem = itemName;
		}
	}
}

void classPlayer::unequipItem(std::string itemName)
{
			for ( int a = 0; a < inventory.size(); a++ )
		{
			if ( inventory[a].showItemName() == itemName )
			{
				baseAttack = baseAttack - inventory[a].returnDamage();
				baseDefense = baseDefense - inventory[a].returnArmor();	
				baseMagic = baseMagic - inventory[a].returnMagic();
				weaponSlotTaken = false;
				weaponSlotItem = "Empty";
			}
		}		
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#ifndef ITEMCLASS_H
#define ITEMCLASS_H

class itemClass
{
	public:
		
		itemClass(std::string name, int setdamage, int setarmor, int setmagic);
		
		std::string showItemName();
		int returnDamage();
		int returnArmor();
		int returnMagic();
		
		
	private:
		std::string itemName;
		int damage, armor, magic;
};

#endif 

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
#include <iostream>
#include "itemclass.h"

itemClass::itemClass(std::string name, int setdamage, int setarmor, int setmagic)
{
	itemName = name;
	damage = setdamage;
	armor = setarmor;
	magic = setmagic;
}

std::string itemClass::showItemName()
{
	return itemName;
}

int itemClass::returnDamage()
{
	return damage;
}

int itemClass::returnArmor()
{
	return armor;
}

int itemClass::returnMagic()
{
	return magic;
}
Last edited on
Basically looking for feedback on my structure thus far, Is it acceptable?
Or any advice on building a text adventure
Any
The listing is just too big for me to go through all and find all problems.

A few of quick observations though; I like the you haven't used "using namespace std" that creates/can create a big problem in huge code base.

Second, you can use the new "range based for" in C++11 if you have GCC 4.8~, CLANG3.4~, visual studio 2012~ (basically C++11/C++14 capable compilers).
Something like for ( auto &a: inventory) instead of for ( int a = 0; a < inventory.size(); a++ )
Here "a" becomes reference to the itemClass object inside the vector

The "accessors" should return const as far as possible they should be const int returnDamage() const; or at least int returnDamage() const;
Topic archived. No new replies allowed.