2d array of object pointers

This is a discussion on 2d array of object pointers within the C++ Programming forums, part of the General Programming Boards category; You don't need pointers for this. Each piece has an ID number. The 'map' or 'board' represents how the board ...

  1. #16
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    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.

  2. #17
    Registered User
    Join Date
    Jun 2006
    Posts
    10
    This looks good too!
    Did you mean to make the ID a byte instead of a dword in the CGamePieceMgr and for m_dwNumObjects in CGamePieceMgr?

  3. #18
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    Well make it what you want. Obviously if you have more than 255 objects you will need to alter the datatype of the array and anything else that relates to the array or the IDs.
    Oversight on my part and I was just showing you what I think is a better approach.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. 2D array pointers (almost got it)
    By tikelele in forum C Programming
    Replies: 4
    Last Post: 11-08-2007, 06:59 AM
  3. Array of Pointers + Deleting An Object = Problems
    By Nereus in forum C++ Programming
    Replies: 3
    Last Post: 03-04-2004, 11:16 AM
  4. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 10:01 PM
  5. Replies: 4
    Last Post: 09-12-2001, 02:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21