Editing class object values

Hello everyone,
I am trying to do a text based rpg game. and now I am stuck.... I want to create a fighting system... But well no luck.Here is a part of my 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
class player
{
    public:
        /*player(int cls1, int lvl1, int  exp1, int maxh1, int health1, int maxm1, int mana1, int maxs1, int stamina1, int weapon1, int armor1, int dmg1, int def1, int loc1)
        {
            setcls(cls1);
            setlvl(lvl1);
            setexp(exp1);
            setmaxh(maxh1);
            setmaxm(maxm1);
            setmana(mana1);
            setmaxs(maxs1);
            setstamina(stamina1);
            setweapon(weapon1);
            setarmor(armor1);
            setdmg(dmg1);
            setdef(def1);
            setloc(loc1);

        }*/
        void setstats(int cls1, int lvl1, int  exp1, int maxh1, int health1, int maxm1, int mana1, int maxs1, int stamina1, int weapon1, int armor1, int dmg1, int def1, int loc1)
        {
            cls=cls1;
            lvl=lvl1;
            exp=exp1;
            maxh=maxh1;
            health=health1;
            maxm=maxm1;
            mana=mana1;
            maxs=maxs1;
            stamina=stamina1;
            weapon=weapon1;
            armor=armor1;
            dmg=dmg1;
            def=def1;
            loc=loc1;
        }
        int getcls()
        {
            return cls;
        }
        int getlvl()
        {
            return lvl;
        }
        int getexp()
        {
            return exp;
        }
        int getmaxh()
        {
            return maxh;
        }
        int gethealth()
        {
            return health;
        }
        int getmaxm()
        {
            return maxm;
        }
        int getmana()
        {
            return mana;
        }
        int getmaxs()
        {
            return maxs;
        }
        int getstamina()
        {
            return stamina;
        }
        int getweapon()
        {
            return weapon;
        }
        int getarmor()
        {
            return armor;
        }
        int getdmg()
        {
            return dmg;
        }
        int getdef()
        {
            return def;
        }
        int getloc()
        {
            return loc;
        }
        void setcls(int x)
        {
            cls=x;
        }
        void setlvl(int x)
        {
            lvl=x;
        }
        void setexp(int x)
        {
            exp=x;
        }
        void setmaxh(int x)
        {
            maxh=x;
        }
        void sethealth(int x)
        {
            health=x;
        }
        void setmaxm(int x)
        {
            maxm=x;
        }
        void setmana(int x)
        {
            mana=x;
        }
        void setmaxs(int x)
        {
            maxs=x;
        }
        void setstamina(int x)
        {
            stamina=x;
        }
        void setweapon(int x)
        {
            weapon=x;
        }
        void setarmor(int x)
        {
            armor=x;
        }
        void setdmg(int x)
        {
            dmg=x;
        }
        void setdef(int x)
        {
            def=x;
        }
        void setloc(int x)
        {
            loc=x;
        }
    private:
        int cls;
        int lvl;
        int exp;
        int maxh;
        int health;
        int maxm;
        int mana;
        int maxs;
        int stamina;
        int weapon;
        int armor;
        int dmg;
        int def;
        int loc;
    friend void fight(player &hero);

};


class SharathCity
{

};

//id, hp, damage, defense, expirience, level, aggresivnes.
class npc
{
    public:
        npc(int id1,int hp1, int dmg1, int def1, int exp1, int lvl1,int agro)
        {
            setid(id1);
            sethp(hp1);
            setdmg(dmg1);
            setdef(def1);
            setexp(exp1);
            setlvl(lvl1);
            setagression(agro);
        }
        int gethp()
        {
            return hp;
        }
        int getdmg()
        {
            return dmg;
        }
        int getexp()
        {
            return exp;
        }
        void setid(int a)
        {
            id=a;
        }
        void sethp(int a)
        {
            hp=a;
        }
        void setdmg(int a)
        {
            dmg=a;
        }
        void setdef(int a)
        {
            def=a;
        }
        void setexp(int a)
        {
            exp=a;
        }
        void setlvl(int a)
        {
            lvl=a;
        }
        void setagression (int a)
        {
            agro=a;
        }
    private:
        int id;
        int hp;
        int dmg;
        int def;
        int exp;
        int lvl;
        int agro;

};



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void fight(player& hero, npc& enemy)
{
    while (hero.gethealth() > 0 || enemy.gethp() > 0)
    {
        hero.sethealth(hero.gethealth()-enemy.getdmg());
        cout << "You were hit for " << enemy.getdmg() << " damage" << endl;
        enemy.sethp(enemy.gethp()-hero.getdmg());
        cout << "You hit the enemy for" << hero.getdmg() << " damage" << endl;
    }
    if ( enemy.gethp()==0)
    {
        hero.setexp(hero.getexp()+enemy.getexp());
        cout << "You have earned " << enemy.getexp() << " expirience." << endl;
    }
}

You have a fundamental design problem.

Consider all your getters and setters. Maybe it makes sense to be able to get some of the values, but to set them to random arbitrary things does not make sense. For one, it makes the getters and setters useless (you could just reference the real variable names), and for another, it breaks Object oriented Design.

Consider you're walking through the woods and bump into a tree. Does the tree come in and magically set your health to a new value? Does the tree need to know about your armor, defense, etc? Does it need to kill you itself when your health runs out? This doesn't make sense.
Well thanks for replying.
I only tried today to program with classes, and it seems I didn't get the idea what could they be used for. My main idea was,that they are like tables (well that's what i thought it is for), like for example the table has id mana health.
And you could always read it and edit it.And it should have worked like this.
I have a loop, where while you in the Rat cave, the Rat monsters are created and then you have to fight. When you leave the Rat Cave, your location sets to another variable like 1. And the rat object disapears.It's like every location spawns monsters, or npc that are intended to be there, and when you leave the place, it will destroy them.

And about the second part, I didn't really understand what did you want to tell me. Considering you hit the Tree hard with your head, you could set your health lower. And considering I wear protection from hits, that gives me defense from trees, I would get no damage, no wounds or something like that.And considering whether or not it's a living/walking tree with some intellect and agressive, it could inted to attack me.
But in case, you mean otherwise why would I use Object as a databank for npc/monsters and whatsoever, I thought it really like that.
Well guess maybe I should just create so variables like
1
2
3
4
int monstername = spider.getname();
int monsterdamage = spider.getdamage();
int monsterhp = spider.gethp();
int monsterexpirience = spider.getexp(); //how much expirience I get for killing it. 
Your idea of classes as tables is ...odd. I'm not sure where you got that idea from. Classes are a way to represent self-contained entities. The "self-contained" part is important, this is called encapsulation.

Your code currently just acts exactly the same as a basic struct"
1
2
3
4
struct Player
{
    int health, defense;
};
The struct just holds data, it doesn't manage itself or do anything on its own - everywhere you have the struct you have to write code to manage it. This results in you having to rewrite the same code over and over, and then have to change it everywhere when you want to make a change. This is the opposite of encapsulation.

It also doesn't make sense conceptually, either; you don't go up to an enemy, punch them, and say "your new health is 2". You just punch them, and they lose health. You also don't ask the enemy how much health they have, or how much defense they have, etc. because it doesn't make sense to - you don't need to know any of that to punch them correctly.

With classes, OO, and encapsulation, objects and instances of classes manage themselves. As a general rule of thumb, getters and setters are bad.

You never need to get information, not even to display it - you can ask an object to display itself. Nobody asks you what you look like to be able to see you, you just make yourself visible.

You never need to set information either; as said before, you don't just punch something and say "your health is now 2" - you punch it and that changes its health itself. You normally change an object's condition, you don't set an object's condition. (Think: do you look at a box and every second tell it that it is one foot to the left? Or do you push the box?)


Try rewriting your code without any getters or setters at all. An object only needs to know about itself, so there's no need for getters, and an object will only change over time, so there is no need for setters.

As a hint, you will probably need a function that deals damage to a player or enemy, and you will need a function to ask the enemy to display itself so the person playing the game knows what they are battling. Then you will need a function to see if the enemy is dead, or if the player is dead.
Last edited on
Topic archived. No new replies allowed.