The Player object that is passed into the dAttack method is not updating its hp on dAttack(player). The dragon takes damage perfectly fine but not the player. I know it's probably not best practice in C++ to define classes in your main function but when I tried to make them into their own .cpp files I was getting an error stating the "Player" and "Dragon" objects were uninitialized.
I have attempted to rename the hit variable in dAttack() to dHit instead, haven't had any other ideas.
#include <iostream>
#include <string>
int main()
{
class Player {
public:
float hMul;
float dMul;
float aMul;
float hp;
void setHMul(float h) { hMul = h; };
void setDMul(float d) { dMul = d; };
void setAMul(float a) { aMul = a; };
void setHP() { hp = hMul * 100; };
};
class Dragon {
public:
float dHp;
float dAt;
void dAttack(Player player) {
float dhit = dAt * 2;
player.hp -= dhit;
std::cout << "The dragon has hit you for: " << dhit << std::endl;
std::cout << "You have " << player.hp << " hp left!" << std::endl;
}
void pAttack(Player player) {
float hit = player.dMul * 2 + 3;
dHp -= hit;
std::cout << "You have hit the dragon for: " << hit << std::endl;
std::cout << "The dragon has " << dHp << " hp left!" << std::endl;
}
};
bool playing = true;
std::cout << "Hello, please pick a character, (W)arrior, (Wi)zard or (R)ogue: ";
std::string choice;
std::cin >> choice;
float hMul;
float dMul;
float aMul;
if (choice == "W" || choice == "w" || choice == "Warrior" || choice == "warrior" || choice == "WARRIOR") {
hMul = 10.0;
dMul = 0.5;
aMul = 3.0;
}
else if (choice == "Wi" || choice == "wi" || choice == "WI" || choice == "Wizard" || choice == "wizard" || choice == "WIZARD") {
hMul = 5.0;
dMul = 5.0;
aMul = 1.0;
}
else if (choice == "R" || choice == "r" || choice == "Rogue" || choice == "rogue" || choice == "ROGUE") {
hMul = 3.0;
dMul = 10.0;
aMul = 1.5;
}
Player player;
player.setHMul(hMul);
player.setDMul(dMul);
player.setAMul(aMul);
player.setHP();
Dragon dragon;
dragon.dAt = 100.0;
dragon.dHp = 10000.0;
while (playing) {
if (dragon.dHp > 0 && player.hp > 0) {
dragon.pAttack(player);
if (dragon.dHp > 0) {
dragon.dAttack(player);
}
else {
std::string ch;
std::cout << "The dragon has been defeated, would you like to play again? (y/n): ";
std::cin >> ch;
}
}
else if (dragon.dHp <= 0) {
std::string ch;
std::cout << "The dragon has been defeated, would you like to play again? (y/n): ";
std::cin >> ch;
}
else if (player.hp <= 0) {
std::string ch;
std::cout << "You have been defeated, would you like to play again? (y/n): ";
std::cin >> ch;
}
}
}
example output:
You have hit the dragon for: 4
The dragon has 20 left!
The dragon has hit you for: 200
You have 800 hp left!
You have hit the dragon for: 4
The dragon has 16 left!
The dragon has hit you for: 200
You have 800 hp left!
You have hit the dragon for: 4
The dragon has 12 left!
The dragon has hit you for: 200
You have 800 hp left!
You have hit the dragon for: 4
The dragon has 8 left!
The dragon has hit you for: 200
You have 800 hp left!
You have hit the dragon for: 4
The dragon has 4 left!
The dragon has hit you for: 200
You have 800 hp left!
You have hit the dragon for: 4
The dragon has 0 left!
The dragon has been defeated, would you like to play again? (y/n):