C++, help with coding chess

JOKER_JOKER

Limp Gawd
Joined
Nov 2, 2005
Messages
471
In my chess program I have a base class called Piece with 6 subclasses of each piece type (King, Queen, Rook, Knight, Bishop, Pawn), as well as a class called Game, which handles all of the gameplay. In my Game class header, I declare a two dimensional array Pieces (Piece* Board[8][8]). The problem I'm running into is that I can't figure out how to call that array while in a subclass of Piece. For instance, in my Queen class, I'm trying to access and alter the Board, but I keep getting errors. I know the problem is probably a simple one, but I can't seem to figure it out, any ideas?

Header of my Game class
Code:
class Game
{
public:

	Game();

	void StartGame();
	void DrawBoard();
	bool IsCheckMate();
	
	Piece* Board[8][8];

};

The Move function from my Queen class
Code:
void Queen::Move(int desx, int desy)
{
	Board[desy][desx] = *this;
	Board[LocY][LocX] = NULL;
}
 
Queen has no direct knowledge of the Game class's Board member, unless your design is funky and a piece owns a game or something bizarre.
 
my C++ is a little rusty (haven't used it since Borland Turbo C++ for DOS ;) )

But do you have a reference to Board in your subclass definitions?

Also, for future reference, please include all errors that you are getting.. it helps a lot to know what we're trying to fix :)
 
No I don't have a reference to Board in my subclass definitions, how would I go about doing that? I've never been very good with using pointers.
 
You can do it with
Code:
Piece*** Board;
then initialize the pointers when you create each piece. This is going to be a bit tough for you, since you're so inexperienced and don't fully understand using pointers, in particular. Since the array of pointers you've got declared in the Game class is two-dimensional, you have to declare an array of pointers to pointers to pointers. Otherwise, the compiler won't know how to resolve the address for both the rows and columns.

Even pursuing this, I think you've got a pretty bad design. You're probably better off making accessors on the Game object. You might also reconsider your model; perhaps each derived piece doesn't actually move itself on the board and instead asks the game to move it. That moves the Move() method to the Game() class and better encapsulates the data with the method. Actually removing and placing the piece isn't specific to the piece type; you can cover the logic within the piece-derived classes which implement rules and state, and then call the game to actually effect the move.
 
I'm fine with initializing the pieces, I just did it this way:
Code:
Rook* bRook1 = new Rook(0, 0, 2);
	Board[0][0] = bRook1;
and did that for each piece.

I might implement your idea of having the actual moving take place in the Game class, but nonetheless the Queen/King/etc classes still need access to the Board. I just don't know the syntax I would need to be able to give them access.
 
What part of your model would require the pieces to be able to access the board? That's the part that you may want to re-evaluate.
 
My pieces need to be able to know if the current move is valid or not (checking if a piece is in the new location, and if the piece is allowed to move that way), and the only way to do this would be for them to have access to the board.
 
Then you'll need to find a way to get them access to the board. There's lots of ways to solve that problem; a couple have been outlined in this thread.
 
You already have classes for Game and each piece, but not a class for the Board itself. Why not?

I would solve this by making the Board a class and then having it be a shared member of all the pieces and the game.
 
Geez... I wish I still had my code from my C++ classes back in the day. We wrote a pretty darn nice chess game along the same lines you're doing. If I think of how we tackled it, I'll let ya know, but my memory's pretty shot right now.

Either way, I found this particular project to be a lot of fun. :)
 
I guess it won't matter much at this point, but having a single shared board instance probably isn't the right way to go.
 
The reason I didn't make Board a class is because it doesn't do anything other than being a container, I didn't really see the point. Anyway someone from another forum helped me out and we got it working, thanks anyways guys!
 
Back
Top