Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C + + i need help fixing my health system and save health for the next enemy.. I know it deals with my logic and

C++ i need help fixing my health system and save health for the next enemy..
I know it deals with my logic and how im calling the variables.
The player weapon isn't registering to deal damage.
For example, boss_health =150 and player_health =100
The Boss weapon does 26 damage, and the player Sword weapon does 30 damage. But when the player attacks it does nothing. The Boss weapon does damage to itself and player, so the output for the boss health is now 124 and player health is 74... So, what am i doing wrong? why isn't the player weapon being returned?
character.hpp
----------------------------
class Character {
private:
int atk;
int def;
int hp;
public:
virtual void equip(Equip* equipment)=0;
virtual void attack(Character* target){};
virtual void special()=0;
void set_attack(int new_atk){ atk = new_atk; }
int get_attack(){ return atk; }
void set_defense(int new_def){ def = new_def; }
int get_defense(){ return def; }
void set_hp(int new_hp){ hp = new_hp; }
int get_hp(){ return hp; }
};
class Player : public Character{
private:
Equip* currentEquipment;
public:
void equip(Equip* equipment) override{
currentEquipment = equipment;
set_attack(currentEquipment->get_attack_bonus());
set_defense(currentEquipment->get_defense_bonus());
}
void attack(Character* target) override{
bool enemy; // logic to determine if target is enemy
int updateHealth;
if(enemy){
updateHealth = target->get_hp()- target->get_attack();
// apply damage to target
target->set_hp(updateHealth);
}
}
void special() override {
std::cout << "Defualt Implementation
";
}
};
class Boss : public Character{
private:
Equip* currentEquipment;
public:
void equip(Equip* equipment) override{
currentEquipment = equipment;
set_attack(currentEquipment->get_attack_bonus());
set_defense(currentEquipment->get_defense_bonus());
}
//overloading function
// equip 'sythe' weapon to boss
void equip(){
Equip* sythe = new Sythe();
equip(sythe);
delete sythe;
}
void attack(Character* target) override{
bool enemy;
int updateHealth;
equip();
if(enemy){
updateHealth = target->get_hp()- get_attack();
target->set_hp(updateHealth);
}
}
void special() override{
//special attacks go here
std::cout << "Defualt Implementation
";
}
}
----------------------------
game.cpp
-----------------------------
constexpr int PLAYER_HEALTH =100;
constexpr int BOSS_HEALTH =200;
void fight(){
// when enemy is found, player can fight or run away...
Player player;
Boss boss;
// setting the health to player and enemy
player.set_hp(PLAYER_HEALTH);
boss.set_hp(BOSS_HEALTH);
string response;
int hit;
do{
boss.attack(&player);
player.attack(&boss);
cout <<"Do you want to fight or run away?
";
std::cin >> response;
if(response == "fight"){
cout <<"################"<< endl;
cout << "Player Health: "<< player.get_hp()<< endl;
cout << "Boss Health: "<< boss.get_hp()<< endl;
cout <<"################"<< endl;
}
else if(response == "run"){
srand(time(NULL));
hit = rand()%2+1;
if (hit ==1){
cout <<"
-> Damage took when escaping: "<< player.get_hp()<< endl;
}
else{ cout <<"
-> Took no damage when escaping." << endl; }
}
}while(player.get_hp()>0 && player.get_hp()>0 && response != "run");
if(player.get_hp()<=0){ cout << "Game Over! You Died!" << endl; main();}
else if(boss.get_hp()<=0){ cout << "Congrats! You Defeated the Boss!" << endl;}
}
-----------------------
Thank you in advanced!!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Refer to Problem 10-1. What is the projects PI?

Answered: 1 week ago

Question

Describe how to train managers to coach employees. page 404

Answered: 1 week ago

Question

Discuss the steps in the development planning process. page 381

Answered: 1 week ago