Class Structure/Style Question

I'm designing a basic text-based game and implementing a player/enemy attack structure to start things off. My question is, is it better to design functions in the player class which directly modify values in the enemy class? Or, is it better to design the attack structure so that the enemy HP/MP/etc is modified outside of the player class function and within the game loop?

Apart from that fact, my attackMelee() and attackMagic() funtions don't work in the first place. My compiler throws me an interesting "variable or field attackMelee() declared as void" error, though I'm not returning anything. o.O I've been trying to figure out how class pointers might work in this instance but I'm having no luck there in the first place.

1
2
3
4
5
6
7
8
9
10
11
12
13
class cPlayer
{
  string name;
  int age, HP, MP, mAttack;
 public:
   cPlayer(){ HP = 100; MP = 100; mAttack = 10; }
   ~cPlayer(){}
   void setName(string pName){ pName = name; }
   void setAge(int pAge){pAge = age; }
   void levelPlayer(){HP*=3; HP*=2; mAttack*=2;}
   void attackMelee(cEnemy c_pEnemy, int aHP){ c_pEnemy.HP -= aHP; }
   void attackMagic(cEnemy c_pEnemy, int aMP){ c_pEnemy.HP -= (aMP*mAttack); }
};

1
2
3
4
5
6
7
8
9
10
11
12
class cEnemy
{
  string name;
  int HP, MP;
 public:
   cEnemy(){ HP = 50; MP = 20; name = "Skeleton Warrior"; }
   ~cEnemy(){}
   void attackHP(){}
   void attackMP(){}
   void reduceHP(int eHP){ HP-=eHP;}
   void reduceMP(int eMP){ MP-=eMP;}
};
closed account (S6k9GNh0)
Here's a realistic answer:

It doesn't matter what design you take on, as long as it doesn't:
1) Hinder the flexibility of your program.
2) Confuse you (to some extent).
3) Prevent you from accomplishing your intended functionality.

Design is something you probably need to learn on your own since I've found that each programmer has his own "style" of design, including myself. You'll probably find no right or wrong answer.

Try something, if it doesn't follow the 3 requirements, move on.

I also cannot help you with your error unless I know the full error.
Last edited on
I agree with computerquip.

But, either way. I build classes around and engine class.

i.e.
At the moment, I'm building Tetris. So far, I've built a class for shapes, a class for GUI, and an engine class. And I'm yet to build a file reading/writing class and a player class.

I 'link' all my classes to the engine class.

I include all the classes in the engine.cpp and create all objects of classes there. Everything to be done is within the corresponding class.

So I create an object in the engine class, to be run here and the object will take care of anything to be done.
Topic archived. No new replies allowed.