I feel stupid, but I need help with one simple concept. I've though of a few ways to do it, but I feel like they all are bad methods. I have two classes, Player, and Enemy both in different .cpp files. I have a private member in my Player class that is the players health. I have a function titled TakeDamaga(int th) which I can use to make the player take damage whenever I call the function. However that means ->I<- have to pass in the value every time, I want it to depend on how it corresponds with the enemy objects attack function. Is there a way I can take the attack function from my Enemy class and have it in my TakeDamage function from my player class?
It's probably better to have the TakeDamage in the player class and then do a top level Battle function. Then you could do this...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void Battle(Player p1, Player p2)
{
while (!p1.IsDead() && !p2.IsDead())
{
p2.TakeDamage(p1.Attack());
p1.TakeDamage(p2.Attack());
}
if (!p1.IsDead() && p2.IsDead())
cout << "Player 1 is victorious"!;
elseif (! p2.IsDead() && p1.IsDead())
cout << "Player 2 is victorious!";
else
cout << "They were both slain!";
}
You should not let them Attack if they are dead though. That would make more sense.
If you haven't already, you probably want to make Attack and AttackRoll that uses a random die generator.
// Player.cpp
#include "Player.h"
#include "Enemy.h"
#include<iostream>
int Player::TakeDamage(int h)
{
health = health - h;
return 0;
}
void Player::printHealth()
{
std::cout << health << std::endl;
return;
}
void Player::setHealth(int a)
{
health = a;
return;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// main.cpp
#include<iostream>
#include"Enemy.h"
#include"Player.h"
int main()
{
Player po;
Enemy eo;
po.setHealth(100);
po.printHealth();
po.TakeDamage(eo.attack(50));
po.printHealth();
system("PAUSE");
return 0;
}
I'm pretty sure that I could be passing by reference in some of these but I'm not really proficient with pointers yet. Feel free to point out my errors.