3

Hi if anybody could advise how to correctly do this. Basically I'm trying to make a class variable called Board who holds in it a two dimensional array of ChessPiece instances.

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

class ChessPiece
{
public:
    char ToChar() { return '#'; };
};

class ChessBoard
{
    int Size;   //This means board is 8x8.
    ChessPiece ** Board;

public:
    ChessBoard();
    int GetSize() { return Size; };

    void PlotBoard();
};

ChessBoard::ChessBoard() {
    Size = 8;
    ChessPiece newBoard[Size][Size];
    Board = newBoard; //Problem here!!! How do I make Board an 8x8 array of ChessPiece?
}

void ChessBoard::PlotBoard() {
    int x, y;
    for (x = 0; x < Size; x++) {
        for (y = 0; y < Size; y++)
            printf("%c", Board[x][y].ToChar());
    }
}

int main()
{
    // ChessBoard board;
    // printf("%d", board.GetSize());
    // board.PlotBoard();

    ChessBoard * a = new ChessBoard();

    return 0;
}

Pretty basic thing I'm missing here really, but I can't seem to figure it out. Thank you!

3
  • 4
    std::vector<std::vector<ChessPiece>>, or even more simply std::vector<ChessPiece> and track the index as row/column using simple multiplication. Commented Aug 13, 2013 at 19:00
  • I'd do it exactly like @Chad. I myself would use the second version, but that's not important. Commented Aug 13, 2013 at 19:02
  • Does the program need to support only the standard 8x8 chess board, or do you want to be able to play on arbitrary-sized chessboards other than 8x8 as well? If you only need the standard 8x8, you might as well make Size a constant and then just use a simple 8x8 fixed-size two-dimensional array. Commented Aug 14, 2013 at 2:57

5 Answers 5

2

There is really no reason to use raw pointers in your scenario. I'd suggest holding the board as a single dimensional std::vector, and simply use multiplication to of the row/column when iterating the board.

class ChessBoard
{
public:
   ChessBoard(size_t row_count) : Size(row_count), Board(row_count * row_count)
   {
   }

   void PlotBoard()
   {
     for(size_t row = 0; row < Size; ++row)
     {
        for(size_t col = 0; col < Size; ++col)
            printf("%c", Board[(row * Size) + col].ToChar());
     }
   }
}

private:
   size_t Size;
   std::vector<ChessPiece> Board;
};
Sign up to request clarification or add additional context in comments.

1 Comment

You certainly meant Board[row * Size + col], didn't you?
1

Change your constructor to:

ChessBoard::ChessBoard():Size(8) {
    Board=new ChessPiece*[Size];

    for (int i=0;i<Size;++i)
        Board[i]=new ChessPiece[Size];
}

You will also need a destructor:

ChessBoard::~ChessBoard()
{
    for (int i=0;i<Size;++i)
        delete [] Board[i];

    delete [] Board;
}

If you want to allow for copying/moving, be sure to add a proper copy/move constructor and copy/move assignment operator(s).

Comments

1
class ChessBoard {
    public:
    enum Field {
       Empty,
       Pawn,
       ...
    };
    const Columns = 8;
    const Rows = 8;
    Field& operator()(unsigned column, unsigned row) { 
        return fields(column * Rows + row);
    } // omitting const/non const and range checks

    private:
    Field fields[Columns * Rows];
};

Comments

0

I did a similar type of project last year. For it I created a Square class, so that a Board would be made up of a 2-D array of Squares. I think this way you have more flexibility in that you can then create methods for Square like checking if there is a Piece (class of its own) on the Square, what type of Piece and adding and removing Pieces.

I think you need to come at the problem in a basic OO manner.

Comments

-1

The issue here is that newBoard is being created with automatic storage duration, which means it will be wiped out when you go out of scope (at the end of the ChessBoard constructor). If Size will never change you can set it to a constant and create the board like this:

#define SIZE 8
class ChessBoard
{
    ChessPiece Board[SIZE][SIZE];

If Size might change you'll need to allocate the arrays in a loop like this:

Board = new ChessPiece *[Size];
for (int i = 0; i < Size; i++){
    Board[i] = new ChessPiece[Size];
}

1 Comment

-1: using new is counter-productive and error prone. (consider std::vector, already recommended in another answer) Also, you don't mention in your answer how to deallocate memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.