[EDIT AGAIN]
I am editing this post a bit based on some new stuff that has come up. I discovered that a part of my problem was in the copy constructor of my BOARD class. I was passing the BOARD around to various parts of the program, by value, and therefore the copy constructor and the destructor were being called often. Well, whenever the destructor was being called, it was killing my piece information. I was hesitant to create new copies of the pieces every time I wanted to pass the BOARD around, so I decided to just pass the game board by reference using pointers and stuff, so that way the destructor wouldnt get called until the very end of the game when I want it to be called. That way there is no bad stuff happening to my piece data. So that problem is solved. I clicked on the pieces and they began to work for the most part, EXCEPT for bishops. An odd problem, that only bishops wouldn't work?
[/END 2ND EDIT]
The pieces are stored in a map like thus:
Code:
map < int, PIECE * > chessPieces;
The segmentation fault is happening in the following function. It is the same function as it was happening before, and in fact it is happening on the same line of code. The difference is that the fault is isolated to only bishops now. That code follows:
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 );
if ( !pieceSelected )
{
cout << "NULL square selected!" << endl;
return;
}
else
{
pieceSelected->Test(cout);
}
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 );
}
}
gameBoard = NULL;
}
The code is faulting on this line:
Code:
bitset<64> pMoves = pieceSelected->GetPossibleMoves();
The piece creation function looks thus:
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 = new ROOK();
break;
case 2:
case 8:
newPiece = new KNIGHT();
break;
case 3:
case 9:
newPiece = new BISHOP();
break;
case 4:
case 10:
newPiece = new QUEEN();
break;
case 5:
case 11:
newPiece = new KING();
break;
case 6:
case 12:
default:
newPiece = new PAWN();
break;
}
newPiece->SetPosition( col, row );
newPiece->SetOwner( owner );
newPiece->SetType ( type );
//The ID number is set to be the integer value of all pieces
//currently on the board. This should be different for every piece
//when it is set on the board.
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
}
Those are the pieces of code that are probably the most prone to have pointer errors in them. I am searching for where the error could be, but so far I haven't found it yet, so you guys see anything blatantly obvious or annoyingly subtle that I might be missing?
I thought it might be useful to post this function as well. It is a short but important one:
Code:
/* GetPiece() - returns a pointer to the PIECE at Column and Row */
PIECE * BOARD::GetPiece ( int Column, int Row )
{
if ( chessBoard[Column][Row] == -1 )
return NULL;
else return chessPieces [ chessBoard[Column][Row] ];
}