Class question (C++)

Started by
7 comments, last by Ekim_Gram 20 years, 7 months ago
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]
Advertisement
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]
Youll have to add another function:
void Damage(int damage){   E_Hp -= damage   //Then chack here if enemy reaches 0 if so call another function to kill it} 


Jeff D
Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton
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]
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]
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.
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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]
[teamonkey] [blog] [tinyminions]
After reading teamonkey''s post and a few of the others, it''s given me a lot to think about.


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

This topic is closed to new replies.

Advertisement