Archived

This topic is now archived and is closed to further replies.

Ekim_Gram

Class question (C++)

Recommended Posts

Ok, I'm trying to make a simple RPG simulation using C++ and I'm having a bit of trouble. Say I have two classes; Player and Enemy. To make it simple, here are their declarations:
class Player
{
public:
  Player();
  int P_Attack(int P_damage);
  int P_hp;
};

class Enemy
{
public:
  Enemy();
  int E_Attack(int E_damage);
  int E_hp;
};
Now, say the Player class uses the Attack() function. How can I do it so that the amount of damage is subtracted from the Enemy's HP(E_hp) or vice versa?
R.I.P. Mark Osback Solo Pa Mi Gente [edited by - Ekim_Gram on August 31, 2003 8:21:00 PM] [edited by - Ekim_Gram on August 31, 2003 8:21:24 PM]

Share this post


Link to post
Share on other sites
Assuming that there's only one player, you have two simple options.

1) Keep a player variable as a global, and decrement its HP in the Enemy's methods. I'm sure you know how to do that.

2) The cleaner solution: Give the Enemy a pointer to the Player class as a member. Assign it in the constructor.

Since I assume there will be lots of enemies, you'll have to pass a pointer to the victimized enemy to the player's attack method.
From a design standpoint, it might be better not to pass the amount of damage to the attack methods. That should probably be determined internally in the class for modularity.

[edited by - twix on August 31, 2003 8:19:13 PM]

Share this post


Link to post
Share on other sites
So if I used global variables, it'd look like this?:


#include <iostream>
using namespace std;

int P_hp = 10;
int E_hp = 10;

class Player
{
public:
Player();
int Attack(int damage);
}

int Player::Attack(int damage)
{
damage = 5;
E_hp -= damage;
}

// Then the same for the Enemy class



That would be it right? If I'm wrong please correct me.




R.I.P. Mark Osback
Solo Pa Mi Gente

[edited by - Ekim_Gram on August 31, 2003 8:25:58 PM]

Share this post


Link to post
Share on other sites
No! I mean a global Player object. But really, I recommend the other solution. If you're going to use classes, you should go the distance and avoid OOP no-nos.

Also, wouldn't it be interesting to allow monsters to attack other monsters (summoning and stuff!). Maybe you should make players and monsters the same class, or at least inherit from a common base that can do basic stuff like attacking an arbitrary entity.

[edited by - twix on August 31, 2003 8:30:19 PM]

Share this post


Link to post
Share on other sites
The Enemy class represents an enemy. All aspects of the enemie should be a part of that class. So the enemies hitpoints should a part of the class.


class Enemy
{
public:
Enemy();
int Attack();
void Damage(int damage);

private:
int hitpoints;
};

void Enemy::Damage(int damage)
{
hitpoints = hitpoints - damage;

if(hitpoints <= 0)
{
// enemy just died, cart him away

}
}

...

int main()
{
Player player;
Enemy enemy;

//player attacks enemy

enemy.Damage(player.Attack());

// or a little more detailed

int amountOfDamagePlayerDeals = player.Attack();
// enemy takes that much damage

enemy.Damage(amountofDamagePlayerDeals);
}


You don''t need the E_ and P_ prefixes, since they are a part of the class you know who they belong to. I can''t call enemy''s Damage() method without an enemy object first, so it''s always something like enemy.Damage(), enemy.E_Damage() is redundant.

Share this post


Link to post
Share on other sites

class Player
{
public:
Player();
bool P_Attack(int P_damage, Enemy& target);
int P_hp;
};

bool Player::P_Attack(int P_damage, Enemy& target)
{
target.E_hp -= P_damage;
return target.E_hp <= 0;
}




[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
I would have thought that what you want to do is have two instances of one class, not two classes. The Enemy and Player classes are essentially the same at this stage, and if they have to differ later on you can always inherit from the common class.


class cGameCharacter {
public:
cGameCharacter(const int Init_hp) : hp(Init_hp) {};

void Attack(cGameCharacter &MyEnemy, const int amount) {
MyEnemy->Damage(amount);
}

void Damage(const int amount) {
hp -= amount;
if(hp <= 0) {
// Add code to kill off this character here
}
}

int hp;
};

int main() {
cGameCharacter(100) Player; // Player starts with 100 hit points
cGameCharacter(200) Enemy; // Enemy starts with 50 hit points

Player.Attack(Enemy, 20); // Sap 20 hit points from the enemy

return 0;
}


[teamonkey]

[edited by - teamonkey on August 31, 2003 8:54:28 PM]

Share this post


Link to post
Share on other sites