Public Group

# A question about calling members functions.

This topic is 3921 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi. I'm programming a tic-tac-toe in c++ using classes. I have two classes: Board and Player. In my game loop function I call Player.get_player_move(). Then I need to know if the value returned by get_player_move (a number between 1 and 9) is a legal move. I mean, I have to look if there is an empty place in my array char board[9]. Does get_player_move need to return a value? What's the best design? This:

// inside Main.cpp
int j = player.get_player_move();
board.islegal(j);


Or this:

//Inside player class
Player::get_player_move(board&)
{
//.....
board.islegal(i);
}


Or doesn't matter? Thanks in advance! [Edited by - Sade_129 on September 25, 2007 9:46:23 PM]

##### Share on other sites
they're both correct. It depends on the architecture you want.

The first option (where the calling code asks the player for a move, and then asks the board if it is valid) is probably better IMO, because the board and the player are "decoupled" - i.e. they don't directly communicate, which allows for some more flexibility when you change things.

##### Share on other sites
Both looks correct, but as Hodgman said the first sample allows the classes to work independent of each other.

##### Share on other sites
Thanks! I'll use the first option then :)

##### Share on other sites
The argument against the second option ignores the fact that the player (at least an AI player) has to be given the state of the board in order to determine its move. Since you have to pass a board to the player, you might as well check the legality inside the player. I think it works better anyway if get_player_move() always returns a valid move -- then you only have to call it once.

##### Share on other sites
Damn!, I didn't think in that. You are right JohnBolton. Player.get_pc_move() needs a copy of the board, board.islegal(), and also board.check_winner, for check if the PC or the Player can win in the next move. I was searching a tic-tac-toe example using classes and I found one that pass a board to the player class. So I think that I'll do the same, it's more clean, although maybe it's not the best design possible.

• 16
• 11
• 11
• 9
• 49
• ### Forum Statistics

• Total Topics
631394
• Total Posts
2999749
×