Sign in to follow this  
Kchaa

Making my classes communicate

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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(){
...
}


Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
cemedias,

that is not at all what inheritance is for. You are greatly overusing inheritance. It should only be used when there is a clear is-a relationship, which is almost never.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this