Class to Class Access

I'm developing a basic attack structure in a text-based game to experiment with class structures. I have three classes; a UI class, an enemy class, and a player class. I developed the code successfully without the UI class first but it seemed bulky and real messy to be honest.

The class below seems more scalable, and much cleaner. The problem I'm having is that the attackState() and defenseState() functions would need access to my two other classes of types cEnemy and cPlayer. Is there a way in which I can access public functions of cEnemy and cPlayer classes within the UI class?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*In this example, Player is an instance of cPlayer, and Skeleton is an instance
of cEnemy. Both contain the values you might expect in most games.*/
class UI
{
  bool gameOver;
  bool exit; 
  int currentState; 
  string input; 
  public:
    UI(){currentState = 0; gameOver = false; exit = false; }
    ~UI(){}
    void stateSelector(int stateNum) {
      switch(stateNum)
      {
        case 0: {introState(); break;  }
        case 1: {attackState(); break; } 
        case 2: {defenseState(); break; } 
        case 3: {exitState(); }
      }
    }
    void updateUI() {
           cout << string(50,'\n'); //Don't really like this refresh method. 
           cout << Player.name << "\t\t\t" << Skeleton.name << "\n"
           << Player.getHP() << "\t\t\t" << Skeleton.getHP() << "\n"
           << Player.getMP() << "\t\t\t" << Skeleton.getMP() << "\n\n\n";
           cout << "1. Attack" << "\t\t" << "2. Block" << "\t\t" << "3. Exit\n"
           << ">: "; }
    void introState();
    void attackState();
    void defenseState(); 
    void exitState() { exit = true; }
    bool getExitState() { return exitState; }
    bool getGameOver(){return gameOver;}
    void gatherInput(){ getline(cin,input); }
};
my guess would be to use friend class cEnemy and friend class cPlayer
Isn't the friend keyword used to access private members of the given classes? I'm only trying to access public functions of these classes before their instances are declared.

I figured there might be a way to use pointers within the class UI to access say cEnemy or cPlayer? I've tried variations on the code below, but I'm both not sure if this is even possible.. or if it uses correct pointer syntax in the first place.

1
2
3
4
5
//Take pointers to two classes, and do various things with them. 
void attackState(cEnemy *Skel, cPlayer *Play) 
{
  Skel->reduceHP(Play->getAttackHP() );
}
That looks like correct syntax. Just pass in &Skeleton and &Player when you call attackState().

Are Skeleton and Player members of the UI class? If they are, you don't even need to have them as parameters to the attackState function - it can just use them directly.

The reduceHP() and getAttackHP() methods just need to be public in cSkeleton and cPlayer.

Jim
Oooohh.... This is one of those 'duh' moments I think lol. I totally spaced on making these into nested classes. Thanks Jim! Things should run a lot smoother now that I know the easiest way to do this.
No worries!
Topic archived. No new replies allowed.