We have this massive project due tomorrow - a chess game that enables two or more players to play against each other.

We have a board class, that contains a 2D array of Piece object pointers representing the lay area of the board. In the board constructor, the Piece array is populated with the relevant piece pointers, and everywhere where an empty space is to be on the board, that specific pointer in the array is set to point to NULL.

Whenever I try to access the array, I get a segfault. I want to call each piece that the array points to's print function. The print function will print out something like K1 for player 1's king. It's done by calling arrPiecesOnBoard[iRow][iCol]->getPrintString() during a for loop structure that displays the board.

I will post the offending code in the next post.

>>a chess game that enables two or more players
Never heard of a chess game played by more than two people (or one person and a computer). There are only two sides in a chess game -- white and black. It would be interesting to find out how three people can play the same game without one of the people just being an observer.

In any event, your probblem is most likely attempting to access uninitialized pointers. Of course no one can tell you for sure because we can't see your monitor. Post the program here so that we can see it.

Here is the offending part of the game:

/**Forward Declarations*/
class twoDBoard;

/**Class Board*/
class Board
{
protected:


public:
	//Pure virtuals: Needs to be redefined in subclasses.
	//virtual void doBoardLayout ( ); //Creates the initial board layout.	

	//virtual void refreshDisplay() = 0;//Reprints a new board according to the current state in the game
	//virtual void move() = 0 ; //Moves a piece to a new location on the board.	
	//virtual bool selectPiece() = 0; //select a piece at a specified location
	//virtual Piece * CreatePiece(int iPieceID) = 0;
};


/**Class TwoDBoard*/
class TwoDBoard : public Board
{
	protected:
		FixedLayout layoutStrategy;
		string arrCurrentLayout[9][9];
		Piece * arrPiecesOnBoard[9][9];
		int iSelectedLocX, iSelectedLocY;
		
	public:
		TwoDBoard();
		void refreshDisplay();
		void move(int iDestX, int iDestY);
		bool selectPiece(int iSX, int iSY);
		Piece * CreatePiece(int iPieceID, int iX, int iY);
};
//2DBoard.C
#include <iostream>
#include <string>

#include "Board.h"
#include "BoardLayoutStrategy.h"

#include "Piece.h"
using namespace std;

TwoDBoard::TwoDBoard()
{
	
	int arrCurrentLayout[9][9] = 	 {{1,2,3,4,5,6,5,4,1},
		       			  {7,7,7,7,7,7,7,7,7},
					  {0,0,0,0,0,0,0,0,0},
					  {0,0,0,0,0,0,0,0,0},
					  {0,0,0,0,0,0,0,0,0},
					  {0,0,0,0,0,0,0,0,0},
					  {0,0,0,0,0,0,0,0,0},
					  {14,14,14,14,14,14,14,14,14},
					  {8,9,10,11,12,13,10,9,8}};
	//calls layoutstrategy's getLayoutPlan();
	//assigns layout plan to arrCurrentLayout
	//create spaces according to layout array
	for (int iRow = 0; iRow < 9; iRow ++)
	{
		for (int iCol = 0; iCol < 9; iCol++)
		{
			int iTempPieceID;
			iTempPieceID = arrCurrentLayout[iRow][iCol];
			Piece * tempPiece = TwoDBoard::CreatePiece(iTempPieceID, iRow, iCol);
			cout << "TEST" << endl;
			cout << arrPiecesOnBoard[iRow][iCol]->getPrintString() << endl;
		}
	}
}



void TwoDBoard::refreshDisplay()
{
	
 cout << "       a    b    c    d    e    f    g    h    i " << endl;
 cout << "    ______________________________________________" << endl;
 for (int iRow = 0; iRow < 9; iRow++){
	cout << "    |    |    |    |    |    |    |    |    |    |" << endl;
	cout << iRow << "   ";
	for (int iCol = 0; iCol < 9; iCol++)
	{
		string sTemp;
		//sTemp = TwoDBoard::arrPiecesOnBoard[iRow][iCol]->getPrintString();
		cout << "|  " << sTemp << "  ";
	}
	 cout << "|" << endl;
	 cout << "    |____|____|____|____|____|____|____|____|____|" << endl;

 }
 cout << endl;
 cout << endl;
}

void TwoDBoard::move(int iDestX, int iDestY)
{
	//Moves a piece to a new location on the board.
	//Recieves parameters from call coming from Game
	//Instructs relevant Piece to move (change its position attributes)
	//Instructs relvant Spaces to change pointers.

	try
	{
		if (arrPiecesOnBoard[iDestX][iDestY] != NULL)
		{
			arrPiecesOnBoard[iSelectedLocX][iSelectedLocY]->move(iDestX, iDestY);
			arrPiecesOnBoard[iDestX][iDestY] = arrPiecesOnBoard[iSelectedLocX][iSelectedLocY];
			arrPiecesOnBoard[iSelectedLocX][iSelectedLocY] = NULL;
		}
		else
			cout << "no piece present at current location!";
	}

	catch(char* exceptionString)
	{
		throw exceptionString;
	}
}

bool TwoDBoard::selectPiece(int iSX, int iSY)
{
	if(arrPiecesOnBoard[iSX][iSY] != NULL)
	{	
		iSelectedLocX = iSX;
		iSelectedLocY = iSY;
		arrPiecesOnBoard[iSelectedLocX][iSelectedLocY]->makeSelected();
		return true;
	}
	else{
		return false;
	}
}

Piece * TwoDBoard::CreatePiece(int iPieceID, int iX, int iY)
{	
	if(iPieceID != 0)
	{//Expand for additional pieces!!	
		FootSoldierPiece * tPiece = new FootSoldierPiece(iX, iY);
		return tPiece;
		delete tPiece;
	}
	else // iPieceID == 0
		return NULL; //Empty space, points to nothing.
}

The 2D board Object is created in the Game class by calling:

Board * board = new TwoDBoard();

Edited 6 Years Ago by getack: n/a

You might as well delete line 108 because that line will never get executed (unreachable code)

Just as I suspected -- line 34 dereferences an unallocated pointer.

Edited 6 Years Ago by Ancient Dragon: n/a

Well that's the line that is making the segfault.

The whole pointer thing is still quite new to me. In fact, C++ and I are only acquainted with each other for a year now...

What am I supposed to do to make it go away?

maybe this

Also, for non-static methods it's customary to use the this pointer instead of class name, such as this->CreatePiece() instead of TwoDBoard::CreatePiece(). this-> isn't really needed either, but some programmers like to use it for clarity.

for (int iCol = 0; iCol < 9; iCol++)
		{
			int iTempPieceID;
			iTempPieceID = arrCurrentLayout[iRow][iCol];
			arrPiecesOnBoard[iRow][iCol] = this->CreatePiece(iTempPieceID, iRow, iCol);
			cout << "TEST" << endl;
			cout << arrPiecesOnBoard[iRow][iCol]->getPrintString() << endl;
		}

Edited 6 Years Ago by Ancient Dragon: n/a

This question has already been answered. Start a new discussion instead.