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
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