Making my classes communicate

Started by
9 comments, last by Glak 16 years, 8 months ago
Hello, I am currently writing a tic-tac-toe console program for practice and have run into a slight problem. I have made classes for the game board, the menu system, and the player. I am wondering how I can get these elements of the game to communicate together efficiently. For example, the problem I have run into right now is when a player chooses a square to put their mark (X or O) in I don't know how to ask the board if that square is available. Currently the method of choosing a square is in the player class. I have written a method under the board class to check if the space is available but how can I get the player to call a method from the board? I hope I didn't confuse you, thanks in advance.
Advertisement
If I understand your question correctly, make the method on the board class public. Or, if the player class is derived from the board class make it protected.
"If I were a philosopher, I might argue that an array name's type is not equivalent to its equivalent. But I'm not a philosopher, so when I suggest something like that, I'm not a philosopher; I'm only being equivalent to a philosopher.""Saturn ascends, choose one or ten. hang on or be humbled again." - Tool
I have the Board and Player in separate files. Since there is only one board to access (which is declared in the main.cpp file) I can't seem to make a call to it's public methods in the Player.cpp file.

I think I'm confusing myself by trying to make this too organized.

EDIT: Maybe I need to make the board object a global variable? Isn't this bad practice?
Quote:Original post by Kchaa
I have the Board and Player in separate files. Since there is only one board to access (which is declared in the main.cpp file) I can't seem to make a call to it's public methods in the Player.cpp file.

I think I'm confusing myself by trying to make this too organized.

EDIT: Maybe I need to make the board object a global variable? Isn't this bad practice?


Inside the header for the board, place a declaration for an instance of a board object:

Gameboard.h:
class GameBoard{
public:
...
};
extern GameBoard gb; // place a declaration here so that player can see it.

main.cpp:
#include "player.h"
#include "Board.h"

GameBoard gb; // define the board here

int main(){
...
}


I appreciate the response from both of you. Is using the 'extern' keyword good practice or would structuring my program better have prevented using this? I am really more concerned about bettering my ability to make simple games than making this one work.

So I value any suggestions you may have.

EDIT: Also, is there a way to do this without putting the game board in global scope?
The structure would depend on the game your making. When I'm working on a game, I would use inheritance. I would have a base class with all of the member functions that the derived classes would all need. So, if you have 2 classes that both need to access the same member function, why not make those classes derive from a base class that contains the member functions needed by both of the derived classes?

BTW: If you would do this, then the member functions in the base class that would be used by the derived classes would be protected.

Ughh, that sounds ugly LOL.

I'm sort of out of it, so sorry if this doesn't make much sense.
"If I were a philosopher, I might argue that an array name's type is not equivalent to its equivalent. But I'm not a philosopher, so when I suggest something like that, I'm not a philosopher; I'm only being equivalent to a philosopher.""Saturn ascends, choose one or ten. hang on or be humbled again." - Tool
I haven't gotten to inheritance yet in my learning but I will definitely revisit this problem when I get there using your suggestion. I appreciate it.

I am working with the board as a global variable right now, though I am not sure if this is good practice or not. Any feedback appreciated.
All I can suggest is, use the knowledge that you have and make it work. :p

If you wish to learn more about inheritance, I suggest you read these tutorials:

Inheritance Overview
Inheritance Syntax

Those should get you started. Good luck on your game!
"If I were a philosopher, I might argue that an array name's type is not equivalent to its equivalent. But I'm not a philosopher, so when I suggest something like that, I'm not a philosopher; I'm only being equivalent to a philosopher.""Saturn ascends, choose one or ten. hang on or be humbled again." - Tool
I would keep your class's as is and make a manager for the game and in this file have the other class's as external reference. this way you could add other class easy
I would say the previous replies (except the one with the global instance) is over complicating the problem. The Player is aware of the Board, so why not have a pointer (or reference) to the board in the player class?

class Player {public:    Player( Board* board ) : mBoard( board ) {}    void PlaceMark( int x, int y ) {        if( mBoard->IsSquareAvailable( x, y ) ) {            /* do stuff */        }    }private:    Board* mBoard;};class Board {public:    bool IsSquareAvailable( int x, int y );};


So, when creating our players you make them aware of the board, and keep all the board manipulation inside the board class.

This topic is closed to new replies.

Advertisement