Getter and Setter Help
Nov 2, 2012 at 5:13am UTC
I'm working on a very simple, very beginner level combat system.
Right now I'm trying to set up a Getter and a Setter functions in my Boss class. I want the Boss' health to be randomly generated between two set number, so that anytime you play and get to the Boss, his health and attack power will be different.
When I run what I have now, The program displays the location (it's like a -89102...some number) of the variable rather than the randomly generated number.
Any help in figuring out why and how I can get this to work would be greatly appreciated!
Boss.h
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
#ifndef rpg_Boss_h
#define rpg_Boss_h
#include <iostream>
#include "Player.h"
#include <string>
#include <cstdlib>
#include "Monster.h"
#include "RPGGameConfig.h"
using namespace std;
class Boss : public Monster
{
public :
Boss();
~Boss();
//string GetMonsterName();
void virtual DisplayMonsterStats();
//void SetMonsterName();
void SetMonsterHealth();
void SetMonsterAttack(int iAttack);
int virtual GetMonsterHealth(); // method that randomly generates the monster's starting health
int virtual GetMonsterAttack(); // randomly generates the monster's attack strength
int damage;
void TakeDamage(int damage);
int BossHealth();
int BossStrength();
protected :
int iBossHealth;
int iBossStrength;
};
#endif
Boss.cpp
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
#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <string>
#include "Monster.h"
#include "Boss.h"
#include "Player.h"
#include "RPGGameConfig.h"
using namespace std;
Boss::Boss(){
}
void Boss::TakeDamage(int damage){
_iMonsterHealth -= damage;
}
void Boss::DisplayMonsterStats(){
cout << "Big Boss Gollem Stats:" << endl;
cout << "Attack Power: " << GetMonsterAttack() << endl;
cout << "Health: " << GetMonsterHealth() << endl;
}
///////////////// Set and Get Boss Health ////////////////////////////
void Boss::SetMonsterHealth()
{
srand((int ) time( NULL ) );
int iHealthPoints = rand() % MAX_BOSS_HEALTH + MIN_BOSS_HEALTH;
_iMonsterHealth = iHealthPoints;
}
int Boss::GetMonsterHealth(){
return _iMonsterHealth;
}
//////////////////// Set and Get Boss Name /////////////////////////////
/*
void SetMonsterName(){
string _sMonsterName = "Gollem";
}
string Boss::GetMonsterName(){
return _sMonsterName;
}
*/
/////////////////// Set and Get Boss Attack Strength /////////////////////
void Boss::SetMonsterAttack(int iAttack){
//int iAttack = rand() % MAX_BOSS_POWER + MIN_BOSS_POWER;
_iMonsterStrength = iAttack;
}
int Boss::GetMonsterAttack(){
return _iMonsterStrength;
}
//////////////////////////////////////////////////////////////////////////
Boss::~Boss(){
}
Nov 2, 2012 at 5:30am UTC
Where is the code that is using the class?
Nov 2, 2012 at 3:20pm UTC
I have the Boss Battle part commented out right now as I was working on another aspect.
Combat.cpp
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
#include "Player.h"
#include "Monster.h"
#include "Combat.h"
#include "Boss.h"
#include "RPGGameConfig.h"
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
Combat::Combat(){
}
Combat::~Combat(){
}
Monster* currentMonster;
Boss* theBoss;
void Combat::CreateMonster(){
goblin = new Monster("Goblin" , 10, 100);
currentMonster = goblin;
// gollem = new Boss();
// currentMonster = gollem;
}
void Combat::CreateBoss(){
//SetMonsterHealth();
gollem = new Boss();
theBoss = gollem;
}
/*
void Combat::BossBattle(Player* player){
string viewStats;
string attack;
Boss boss;
boss.SetMonsterAttack(rand() % MAX_BOSS_POWER + MIN_BOSS_POWER);
cout << "You've encountered: Big Boss Gollem" << endl;
cout << "Would you like to view Big Boss Gollem's stats? Y/N" << endl;
cin >> viewStats;
if(viewStats == "Y")
{
theBoss->DisplayMonsterStats();
// player->DisplayStats();
}
else
{
cout << "Good luck!" << endl;
}
cout << "FIGHT!" << endl;
while(player->GetHealth() >= 1)
{
cout << "(A)ttack, (U)se Potion, or (R)un: " << endl;
cin >> sMove;
if(sMove == "A")
{
//player attacks
cout << "\n" << player->GetName() << "attacks and deals " << player->GetAttackPower() << " damage!" << endl;
currentMonster->TakeDamage(player->GetAttackPower());
cout << "Current Stats: " << endl;
cout << player->GetName() << ": " << player->GetHealth() << " health points." << endl;
cout << "Big Boss Gollem: " << theBoss->GetMonsterHealth() << " health points." << endl;
// monster attacks
cout << "Big Boss Gollem attacks and deals you " << theBoss->GetMonsterAttack() << " damage!" << endl;
player->TakeDamage(theBoss->GetMonsterAttack());
cout << "Current Stats: " << endl;
cout << player->GetName() << ": " << player->GetHealth() << " health points." << endl;
cout << "Big Boss Gollem: " << theBoss->GetMonsterHealth() << " health points." << endl;
}
else if(sMove=="U")
{
// display potions in inventory
cout << "Currently empty." << endl;
}
else if(sMove == "R")
{
// player receives damage when trying to run away
cout << "You attempted to run away." << endl;
cout << "As you turned to run, you stepped on your cloak and faceplanted into the ground, hard." << endl;
cout << "You have received 10 damage!" << endl;
player->TakeDamage(10);
cout << "Current Stats: " << endl;
cout << player->GetName() << ": " << player->GetHealth() << " health points." << endl;
cout << "Big Boss Gollem: " << theBoss->GetMonsterHealth() << " health points." << endl;
// monster attacks
cout << "Big Boss Gollem attacks and deals you " << theBoss->GetMonsterAttack() << " damage!" << endl;
player->TakeDamage(theBoss->GetMonsterAttack());
cout << "Current Stats: " << endl;
cout << player->GetName() << ": " << player->GetHealth() << " health points." << endl;
cout << "Big Boss Gollem: " << theBoss->GetMonsterHealth() << " health points." << endl;
}
if(theBoss->GetMonsterHealth() <= 0)
{
//Monster died
cout << "You have defeated Big Boss Gollem!" << endl;
sWin = "Y";
}
else if(player->GetHealth() <= 0)
{
// player has died
cout << "You have been defeated." << endl;
cout << "Game Over." << endl;
system("pause");
exit(1);
}
}
}
*/
void Combat::battle(Player* player){
string viewStats;
string attack;
cout << "You've encountered: " + currentMonster->GetMonsterName() << endl;
cout << "Would you like to view " + currentMonster->GetMonsterName() + "'s stats? Y/N" << endl;
cin >> viewStats;
if (viewStats == "Y" )
{
currentMonster->DisplayMonsterStats();
// player->DisplayStats();
}
else
{
cout << "Good luck!" << endl;
}
cout << "FIGHT!" << endl;
while (player->GetHealth() >= 1)
{
cout << "(A)ttack, (U)se Potion, or (R)un: " << endl;
cin >> sMove;
if (sMove == "A" )
{
//player attacks
cout << "\n" << player->GetName() << " attacks and deals " << player->GetAttackPower() << " damage!" << endl;
currentMonster->TakeDamage(player->GetAttackPower());
cout << "Current Stats: " << endl;
cout << player->GetName() << ": " << player->GetHealth() << " health points." << endl;
cout << currentMonster->GetMonsterName() << ": " << currentMonster->GetMonsterHealth() << " health points." << endl;
// monster attacks
cout << "\n" << currentMonster->GetMonsterName() + " attacks and deals you " << currentMonster->GetMonsterAttack() << " damage!" << endl;
player->TakeDamage(currentMonster->GetMonsterAttack());
cout << "Current Stats: " << endl;
cout << player->GetName() << ": " << player->GetHealth() << " health points." << endl;
cout << currentMonster->GetMonsterName() << ": " << currentMonster->GetMonsterHealth() << " health points." << endl;
}
else if (sMove=="U" )
{
// display potions in inventory
cout << "Currently empty." << endl;
}
else if (sMove == "R" )
{
// player receives damage when trying to run away
cout << "You attempted to run away." << endl;
cout << "As you turned to run, you stepped on your cloak and faceplanted into the ground, hard." << endl;
cout << "You have received 10 damage!" << endl;
player->TakeDamage(10);
cout << "Current Stats: " << endl;
cout << player->GetName() << ": " << player->GetHealth() << " health points." << endl;
cout << currentMonster->GetMonsterName() << ": " << currentMonster->GetMonsterHealth() << " health points." << endl;
// monster attacks
cout << "\n" + currentMonster->GetMonsterName() << " attacks and deals you " << currentMonster->GetMonsterAttack() << " damage!" << endl;
player->TakeDamage(currentMonster->GetMonsterAttack());
cout << "Current Stats: " << endl;
cout << player->GetName() << ": " << player->GetHealth() << " health points." << endl;
cout << currentMonster->GetMonsterName() << ": " << currentMonster->GetMonsterHealth() << " health points." << endl;
}
if (currentMonster->GetMonsterHealth() <= 0)
{
//Monster died
cout << "You have defeated " << currentMonster->GetMonsterName() << "!" << endl;
sWin = "Y" ;
break ;
}
else if (player->GetHealth() <= 0)
{
// player has died
cout << "You have been defeated." << endl;
cout << "Game Over." << endl;
system("pause" );
exit(1);
}
}
}
Nov 5, 2012 at 12:11am UTC
I guess I'll just bump this in case someone wants to help.
Nov 5, 2012 at 12:14am UTC
Are you ever calling SetHealth() or whatever before you actually try to use it? The constructor you have given will not actually set any data members so if you don't set them yourself they will be garbage like you are seeing.
I think you should probably make the constructor set the HP/stats for the monster; that way you don't have to remember to set them before using it.
Nov 5, 2012 at 12:18am UTC
I'm not sure what your problem is, I do know I'll never try to run away from anything while playing your game though!
Topic archived. No new replies allowed.