Jump to content
  • Advertisement
Sign in to follow this  
Sade_129

A question about calling members functions.

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

If you intended to correct an error in the post then please contact us.

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 this post


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


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!