You don't need pointers for this.
Each piece has an ID number.
The 'map' or 'board' represents how the board looks using these ID numbers.
That way:
1. All pieces can be managed in a single class.
2. The board has nothing to do with objects - and everything to do with numbers....which means your board has now become a simple 1 dimensional array.
Memory req:
Pieces: NumberOfPieces*sizeof(class_object)
Board: sizeof(BYTE) * NumRows*NumCols
Using BYTE as the datatype for the Piece ID's will allow you 255 unique pieces.
If you need more then change the data type of the board array,
Code:
class CGamePieceMgr;
class CGamePiece
{
friend class CGamePieceMgr;
protected:
DWORD m_dwID;
int m_iRow;
int m_iColumn;
int m_iBoardOffset;
CGamePiece();
public:
virtual ~CGamePiece();
};
class CGamePieceMgr
{
protected:
CGamePiece *m_pObjects;
DWORD m_dwNumObjects;
public:
CGamePieceMgr():m_pObjects(NULL),m_dwNumObjects(0) { }
bool Create(DWORD dwNumObjects)
{
if (m_pObjects) return true;
m_dwNumObjects=dwNumObjects;
m_pObjects=new CGamePiece[dwNumObjects];
if (m_pObjects) return false;
return true;
}
DWORD Add(CGamePiece *pNewPiece);
bool Remove(DWORD dwID);
...
};
#define BOARD_ERROR 0xFF
class CGameBoard
{
protected:
BYTE *m_pBoard;
int m_iRows;
int m_iCols;
int m_iSize;
public:
CGameBoard():m_pBoard(NULL),m_iRows(0),m_iCols(0),m_iSize(0) { }
virtual ~CGameBoard();
bool Create(int iRows,int iCols)
{
if (m_pBoard) return true;
m_iRows=iRows;
m_iCols=iCols;
m_iSize=iRows*iCols;
m_pBoard=new BYTE[iRows*iCols];
if (m_pBoard) return false
return true;
}
BYTE GetValue(int iOffset)
{
if (iOffset>iSize || iOffset<0) return BOARD_ERROR;
return m_pBoard[iOffset];
}
BYTE GetValue(int iRow,int iCol)
{
if ( (iRow<0 || iRow>m_iRows) || (iCol<0 || iCol>m_iCols) ) return BOARD_ERROR;
int iOffset=iRow*m_iCols+iCol;
return m_pBoard[iOffset];
}
BYTE SetValue(int iOffset,BYTE uValue)
{
if (iOffset>iSize || iOffset<0) return BOARD_ERROR;
BYTE temp=m_pBoard[iOffset];
m_pBoard[iOffset]=uValue;
return temp;
}
BYTE SetValue(int iRow,int iCol)
{
if ( (iRow<0 || iRow>m_iRows) || (iCol<0 || iCol>m_iCols) ) return BOARD_ERROR;
int iOffset=iRow*m_iCols+iCol;
if iOffset>iSize || iOffset<0) return BOARD_ERROR;
BYTE temp=m_pBoard[iOffset];
m_pBoard[iOffset]=uValue;
return temp;
}
};
Now that shows a good start to your problem. Modify it as you need. I highly recommend separating the board from any objects. The board doesn't care what's inside of itself...it just keeps track of values. The objects don't care about the board, they just care about where they are (row and column). In other words the board and pieces are two distinct objects that have nothing to do with each other.