Some problems with an abstract class
Hey guys, I am having a few problems with this abstract class I am working on.
We are building a chess game in class, and using inheritance for the different pieces.
So my base class is simply called PIECE, and there are six classes that inherit from it: PAWN, ROOK, KNIGHT, BISHOP, QUEEN, and KING.
Well, most of the functionality of all the pieces is the exact same, so most of the functionality lies in the PIECE class itself. However, I have made the destructor virtual, as well as a function called GetPossibleMoves and also WriteToXML.
GetPossibleMoves is virtual obviously because every type of piece has a different set of possible moves. WriteToXML is virtual because...well, for similar reasons.
Anyways, the problem is this:
For the most part in my program I refer to all the pieces as PIECE in general. Very very rarely do I actually refer to them by their child class names. My desire is that when I call the GetPossibleMoves function from the base class of PIECE, that it automatically calls the correct child version of it. For all intents and purposes it should be doing so...however...it's not...
I orginally had GetPossibleMoves defined as basically an empty function on the PIECE level, and then on its childrens' levels, it was defined for each piece to get the moves of that piece.
I noticed that whenever I called GetPossibleMoves, it was returning to me a set of 0's...which means that no moves are possible....well...the only way that is possible is if it is calling the parent GetPossibleMoves, and not the child version.
So I decided to change the parent function from virtually an empty function to this:
virtual bitset <64> GetPossibleMoves ( ) = 0;
This brings up a different problem: It makes it a pure abstract class. Pure abstract classes cannot be instantiated or worked with, except with pointers. Well, right now my program isn't using pointers, and it would take a great deal of work to make it use pointers.....although it is one option.
For reference, I will post the code of where the pieces are being created, and then where they are being used.
The piece creation function:
Code:
/* CreatePiece() - creates a piece and places it on the specified position.
This function returns the piece's assigned id number. */
int BOARD::CreatePiece ( int type, int owner, int col, int row )
{
/* The tasks of this function include:
1. Create a PIECE object for the new piece. Insert it
into the chessPieces map with a new ID #.
2. Place the piece on the chessBoard object at the
appropriate position.
3. Update the AllPieces, WhitePieces, and BlackPieces
bitboards appropriately.
*/
PIECE newPiece;
switch ( type )
{
case 1:
case 7:
newPiece = ROOK();
break;
case 2:
case 8:
newPiece = KNIGHT();
break;
case 3:
case 9:
newPiece = BISHOP();
break;
case 4:
case 10:
newPiece = QUEEN();
break;
case 5:
case 11:
newPiece = KING();
break;
case 6:
case 12:
default:
newPiece = PAWN();
break;
}
newPiece.SetPosition( col, row );
newPiece.SetOwner( owner );
newPiece.SetType ( type );
//The ID number is set
newPiece.SetID( (++PrevPieceID) );
//Place the piece on the chess board
chessBoard[col][row] = newPiece.GetID();
//Place the piece in the chess piece map
chessPieces [ newPiece.GetID() ] = newPiece;
//Place the piece in the appropriate bitboards.
AllPieces [ row * 010 + col ] = true;
if ( owner == WHITE_TEAM )
WhitePieces [ row * 010 + col ] = true;
else BlackPieces [ row * 010 + col ] = true;
return newPiece.GetID(); //return the new piece's ID
}
The on-mouse-click event handler, which references pieces and asks them for their possible moves:
Code:
void Chess::on_mouse_button_pressed(int c, int r, int type, int button)
{
cout << "Mouse button pressed..." << endl;
BOARD gameBoard = gameManager.GetGameBoard();
PIECE pieceSelected;
pieceSelected = gameBoard.GetPiece ( c, r );
bitset<64> pMoves = pieceSelected.GetPossibleMoves();
cout << "Piece selected type: " << pieceSelected.GetType() << endl;
cout << "Possible moves bitset: " << pMoves << endl;
for ( int row = 0; row < 8; row++ )
{
for ( int col = 0; col < 8; col++ )
{
if ( pMoves[ row * 8 + col ] == true )
highlight_square ( c, r, BLUE_SQUARE );
}
}
}