When I run the code, everything sort of works besides the health system.
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?
In my main.cpp the user can choose a weapon to equip to the player
<code>void characterClass(){
Staff* staff = new Staff();
Book_Spells* book_spells = new Book_Spells();
Sword* sword = new Sword();
Shield* shield = new Shield();
Menu book_menu(book_spells);
Menu shield_menu(shield);
Equip* chosenWeapon = nullptr;
LOG("--------------------------------------------")
LOG("| Choose Your Class |")
LOG("| 1-Staff 2-Book 3-Sword 4-Shield |")
LOG("--------------------------------------------")
cout << "Staff (DPS:" << staff_menu.item_bonus() << " DEF:" << staff_menu.item_defense() << ")" << endl;
cout << "Book of Spells (DPS:" << book_menu.item_bonus() << " DEF:" << book_menu.item_defense() << ")" << endl;
chosenWeapon = book_spells;
cout << "Sword (DPS:" << sword_menu.item_bonus() << " DEF:" << sword_menu.item_defense() << ")" << endl;
cout << "Shield (DPS:" << shield_menu.item_bonus() << " DEF:" << shield_menu.item_defense() << ")" << endl;
LOG("Do you want to pick a differnt class?(Y/N)")
}while(changeInput == 'Y' || changeInput == 'y');
//equips weapon to player in class
player.equip(chosenWeapon);
<code>void characterClass(){
Player player;
int response;
char changeInput;
Staff* staff = new Staff();
Book_Spells* book_spells = new Book_Spells();
Sword* sword = new Sword();
Shield* shield = new Shield();
Menu staff_menu(staff);
Menu book_menu(book_spells);
Menu sword_menu(sword);
Menu shield_menu(shield);
Equip* chosenWeapon = nullptr;
do{
LOG("--------------------------------------------")
LOG("| Choose Your Class |")
LOG("| 1-Staff 2-Book 3-Sword 4-Shield |")
LOG("--------------------------------------------")
std::cin >> response;
switch(response){
case 1:
cout << "Staff (DPS:" << staff_menu.item_bonus() << " DEF:" << staff_menu.item_defense() << ")" << endl;
chosenWeapon = staff;
break;
case 2:
cout << "Book of Spells (DPS:" << book_menu.item_bonus() << " DEF:" << book_menu.item_defense() << ")" << endl;
chosenWeapon = book_spells;
break;
case 3:
cout << "Sword (DPS:" << sword_menu.item_bonus() << " DEF:" << sword_menu.item_defense() << ")" << endl;
chosenWeapon = sword;
break;
case 4:
cout << "Shield (DPS:" << shield_menu.item_bonus() << " DEF:" << shield_menu.item_defense() << ")" << endl;
chosenWeapon = shield;
break;
case 5:
default:
LOG("Invalid Input")
break;
}
LOG("Do you want to pick a differnt class?(Y/N)")
std::cin >> changeInput;
}while(changeInput == 'Y' || changeInput == 'y');
//equips weapon to player in class
player.equip(chosenWeapon);
</code>
void characterClass(){
Player player;
int response;
char changeInput;
Staff* staff = new Staff();
Book_Spells* book_spells = new Book_Spells();
Sword* sword = new Sword();
Shield* shield = new Shield();
Menu staff_menu(staff);
Menu book_menu(book_spells);
Menu sword_menu(sword);
Menu shield_menu(shield);
Equip* chosenWeapon = nullptr;
do{
LOG("--------------------------------------------")
LOG("| Choose Your Class |")
LOG("| 1-Staff 2-Book 3-Sword 4-Shield |")
LOG("--------------------------------------------")
std::cin >> response;
switch(response){
case 1:
cout << "Staff (DPS:" << staff_menu.item_bonus() << " DEF:" << staff_menu.item_defense() << ")" << endl;
chosenWeapon = staff;
break;
case 2:
cout << "Book of Spells (DPS:" << book_menu.item_bonus() << " DEF:" << book_menu.item_defense() << ")" << endl;
chosenWeapon = book_spells;
break;
case 3:
cout << "Sword (DPS:" << sword_menu.item_bonus() << " DEF:" << sword_menu.item_defense() << ")" << endl;
chosenWeapon = sword;
break;
case 4:
cout << "Shield (DPS:" << shield_menu.item_bonus() << " DEF:" << shield_menu.item_defense() << ")" << endl;
chosenWeapon = shield;
break;
case 5:
default:
LOG("Invalid Input")
break;
}
LOG("Do you want to pick a differnt class?(Y/N)")
std::cin >> changeInput;
}while(changeInput == 'Y' || changeInput == 'y');
//equips weapon to player in class
player.equip(chosenWeapon);
Character.hpp
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{
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
updateHealth = target->get_hp() - target->get_attack();
// apply damage to target
target->set_hp(updateHealth);
void special() override {
std::cout << "Defualt Implementationn";
class Boss : public Character{
void equip(Equip* equipment) override{
currentEquipment = equipment;
set_attack(currentEquipment->get_attack_bonus());
set_defense(currentEquipment->get_defense_bonus());
// equip 'sythe' weapon to boss
Equip* sythe = new Sythe();
void attack(Character* target) override{
updateHealth = target->get_hp() - get_attack();
target->set_hp(updateHealth);
//special attacks go here
std::cout << "Defualt Implementationn";
<code>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 Implementationn";
}
};
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 Implementationn";
}
};
</code>
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 Implementationn";
}
};
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 Implementationn";
}
};
game.cpp
<code>constexpr int PLAYER_HEALTH = 100;
constexpr int BOSS_HEALTH = 200;
// when enemy is found, player can fight or run away...
// setting the health to player and enemy
player.set_hp(PLAYER_HEALTH);
boss.set_hp(BOSS_HEALTH);
cout << "Do you want to fight or run away?n";
cout << "################" << endl;
cout << "Player Health: " << player.get_hp() << endl;
cout << "Boss Health: " << boss.get_hp() << endl;
cout << "################" << endl;
else if(response == "run"){
cout << "n-> Damage took when escaping: " << player.get_hp() << endl;
else{ cout << "n-> 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;}
<code>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?n";
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 << "n-> Damage took when escaping: " << player.get_hp() << endl;
}
else{ cout << "n-> 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;}
}
</code>
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?n";
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 << "n-> Damage took when escaping: " << player.get_hp() << endl;
}
else{ cout << "n-> 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;}
}
This is my first post, I hope i did it correctly.
I tried setting the health in private within each class, and i even tried setting the boss weapon equip in the main.cpp.