Click to See Complete Forum and Search --> : Totally stuck - addition of member variables of different classes


Cro-Mag
December 29th, 2007, 01:12 PM
Hi I'll try to be concise, I'm new to C++ and am writing a basic game for practice (and it's fun).

My "player character" has a weapon and some armor, the weapon deals damage, the armor absorbs it.

In addition to this, my "player character" has a certain amount of physical "strength" to provide a damage bonus, he also has a "defense skill" to provide a defense bonus.

Strength to hit harder with a weapon.
Defense skill to maximise armor protection.

I cannot fathom why I am getting such odd armor bonus numbers when I run the code below...please forgive me as I cannot find the "code" tags mentioned in the "before you post" guidelines.

class hero //the player character
{
public:
int get_hero_strength();
void set_hero_strength(int);
int get_hero_defense_skill();
void set_hero_defense_skill(int);
int hero_strength;
int hero_defense_skill;
};

class weapon //the weapon equipped
{
private:
int weapon_rating;
public:
void set_weapon_rating(int);
int get_weapon_rating_with_bonus(); //taking hero_strength into account
hero player;
};

class armor //the armor being worn
{ private:
int armor_rating;
public:
void set_armor_rating(int);
int get_armor_rating_with_bonus(); //taking hero_defense_skill into account
hero player;
};
//functions
void armor::set_armor_rating(int ar)
{
armor_rating = ar;
}

int armor::get_armor_rating_with_bonus()
{
return armor_rating + player.hero_defense_skill; //defense skill bonus added here
}

void weapon::set_weapon_rating(int wr)
{
weapon_rating = wr;
}

int weapon::get_weapon_rating_with_bonus()
{
return weapon_rating + player.hero_strength; //strength bonus added here
}

void hero::set_hero_strength(int str)
{
hero_strength = str;
}

int hero::get_hero_strength()
{
return hero_strength;
}

void hero::set_hero_defense_skill(int def)
{
hero_defense_skill = def;
}

int hero::get_hero_defense_skill()
{
return hero_defense_skill;
}

//main

#include <iostream>
#include <classes.h>
#include <functions.h>
using namespace std;

int main()
{
weapon mace; //declare class instances
armor breastplate;
hero player;
int ar; //declare variables to assign to member variables
int wr; //as am not confident with constructors at the moment
int str;
int def;
cout << "enter armor rating (ar) ";
cin >> ar;
cout << "enter wep rating (wr) ";
cin >> wr;
cout << "enter strength (str) ";
cin >> str;
cout << "enter defense skill (def) ";
cin >> def;
breastplate.set_armor_rating(ar); //entered variables passed
mace.set_weapon_rating(wr);
player.set_hero_strength(str);
player.set_hero_defense_skill(def);
cout << "you hit yourself in the chest... for some reason" << endl;
cout << "you take " << mace.get_weapon_rating_with_bonus()-breastplate.get_armor_rating_with_bonus() << " damage" << endl;
cout << "ar entered as " << breastplate.get_armor_rating_with_bonus() << endl;
cout << "wr entered as " << mace.get_weapon_rating_with_bonus() << endl;
cout << "str entered as " << player.get_hero_strength() << endl;
cout << "def entered as " << player.get_hero_defense_skill() << endl;

int please_ignore_me; //in order to stop dos window so I can read the output
cin >> please_ignore_me; //in order to stop dos window so I can read the output
}
---------------------------------------------------------------------------------------
The output for this is below:

enter armor rating (ar) 20
enter wep rating (wr) 21
enter strength (str) 50
enter defense skill (def) 51
you hit yourself in the chest... for some reason
you take -5409055 damage
ar entered as 5409076
wr entered as 21
str entered as 50
def entered as 51
----------------------------------------------------------------------------------------

any suggestions welcome thx and apologies in advanced for unknowingly breaking any forum rules.. :S

bovinedragon
December 29th, 2007, 02:18 PM
Welcome to the forum, for future reference, please use code tags when posting code (see my sig)

Having variables with the same name, but in different places, does NOT make then have the same value.

I see you put "hero player;" in both your weapon class, and your armor class, so that you could use them in you damage calculations. However, just by naming it the same name does not make it the same instance in the hero class.

An easy way to fix this is to just but your hero instatnce in the global scope, so you will not have to deal with copying it to the other classes just to reference it.


class hero //the player character
{
public:
int get_hero_strength();
void set_hero_strength(int);
int get_hero_defense_skill();
void set_hero_defense_skill(int);
int hero_strength;
int hero_defense_skill;
} player; //this will put player in the global scope


so take it out of you other classes then.

And the reason that it is giving you such wierd numbers is that it is creating a difference "hero player" in each of your weapon class and armor class. And since you didnt initialize those values, with a constructor, they contain garbage numbers.

spoon!
December 29th, 2007, 04:54 PM
better, you can have weapon and armor classes have a pointer or reference to a hero; and then, when you construct the weapon or armor class, pass in a pointer to the applicable hero; the methods in weapon and armor will then need to use that pointer or reference

Graham
December 29th, 2007, 05:26 PM
I would have thought that it made more sense for the player class to "own" the weapon and armour. The potential damage inflicited then becomes a function of the player's strength and the damage potential of the weapon, and the protection is a function of the player's defensive capability and armour class

class weapon
{
public:
int rating() const { return rating_; }
private:
int rating_;
};

class armour
{
public:
int rating() const { return rating_; }
private:
int rating_;
};

class player
{
public:
int attack_damage() const { return strength_ + weapon_.rating(); }
int protection() const { return skill_ + armour_.rating(); }
int defend_attack(int damage)
{
hit_points_ -= damage - protection();
return hit_points_;
}

private:
int hit_points_;
int strength_;
int skill_;
weapon weaon_;
armour armour_;
};

int main()
{
player p1, p2;

p2.defend_attack(p1.attack_damage());
}

The above ignores a lot of things (such as intialising all the values), but it should give the outline of what I mean.

Cro-Mag
December 30th, 2007, 07:40 AM
I intially got it working by making the player instance global, thanks bovinedragon and have now expanded the player class to be the source for damage/defense calculations as that does make more sense. As well as making future additions like weapon enchantments, and usage-based skill gains for the player easier to add.

I will delve into pointers and references properly as they seem to be the best way to implement an inventory of the player "backpack" - items aquired by the player on his adventures - maybe using an array of pointers?

a pointer is an address of a variable a pointer is an address of a variable
*chants over and over* :D