0

EDIT: We aren't using vectors for this, the piece class right now is just stubbed out. Different pieces don't exist yet, and valid moves aren't being considered. The board displays a 'P' in the square if a piece is there, and is blank if that pointer is pointing to null.

I am trying to write a simple chess program using object oriented programming. So far I have a board initialized to a 2d array of pointers. I can place pieces on the board, print the board and move the pieces. To get any further I need to overload the assignment operator for the board class. The class is:

    #ifndef BOARD_H
#define BOARD_H

class Board;
#include "Piece.h"
#include <iostream>
using namespace std;

#define MAXROWS 8
#define MAXCOLS 8

typedef Piece* PiecePtr;

class Board
{
public:
   Board();
   Board (const Board& other);
   ~Board();

   bool move (int fromX, int fromY, int toX, int toY);
   bool place (const PiecePtr& p, int x, int y);
   bool remove (int x, int y);

   void write (ostream& out) const;

   Board& operator= (const Board& other);

private:
   PiecePtr grid[MAXROWS][MAXCOLS];

   void initBoard();
   void clearBoard();
   void copyBoard(const Board& other);
   bool canMove (int fromX, int fromY, int toX, int toY);
};

ostream& operator<< (ostream& out, const Board& b);

#endif

So far I have written the assignment operator as follows:

Board& Board::operator= (const Board& other)
{
  PiecePtr temp = NULL;
  if(this != &other)
    {
      for(int x = 0; x < MAXCOLS; x++)
        {
          for(int y = 0; y < MAXROWS; y++)
            {
              temp = grid[x][y];
              delete temp;
            }
        }
      ///////////keep going here                                                                                                                                                                 
    }


  return *this;
}

I feel like I"m making this far too complicated, from what I understand the Board& other parameter would be the left hand side of the x=y statement. I don't understand why my books say that the memory needs to be deallocated, and how anything gets copied if we delete everything first. I have asked at school and was told to put some thought into it. So that helped a lot. If someone could explain where I'm going wrong with this I would really appreciate it.

Don
  • 1
  • 1
  • Can you post the definition of `Piece`? It might be easier to use `std::vector>` instead of a 2D array of pointers. – R Sahu Apr 04 '15 at 19:30
  • @Don, *from what I understand the Board& other parameter would be the left hand side of the x=y statement*. No, `other` is the RHS, i.e. `x=y` translates as `x.operator=(y)`. – vsoftco Apr 04 '15 at 19:33
  • You should be writing a copy constructor first. Once you do that (and write a destructor), the assignment operator becomes a 2 line function. – PaulMcKenzie Apr 04 '15 at 19:33
  • @PaulMcKenzie how's that? I don't think it's a good idea to implement the copy ctor in terms of `operator=` or the other way around. Are you thinking about copy-and-swap? – vsoftco Apr 04 '15 at 19:35
  • @vsoftco Then you are not aware of the copy/swap idiom? http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom Once you have a working copy constructor and a working destructor, the assignment operator is nothing more than `swap` calls. – PaulMcKenzie Apr 04 '15 at 19:37
  • @PaulMcKenzie I knew that, actually I edited my comment just moments before you posted the comment. But you need to implement a `swap`, your comment seemed to imply that `operator=` is implemented in terms of the copy ctor. – vsoftco Apr 04 '15 at 19:39
  • @vsoftco It requires a working copy constructor, so it is implemented in terms of it. The copy constructor is called implicitly, but it still gets called and must work correctly. – PaulMcKenzie Apr 04 '15 at 19:40
  • @PaulMcKenzie, I have a constructor that initializes the array with every pointer nulled out, and a destructor that traverses the entire array and deleted that pieces then nulls the pointers. Can you describe the copy constructor? Am I creating another board then just copying the piece locations over? – Don Apr 04 '15 at 19:41
  • @Don, try taking a look at the link Paul posted and you'll see how its done – vsoftco Apr 04 '15 at 19:41
  • @Don you need to implement the copy constructor -- it is one of the "big 3" functions. Otherwise I can easily break your program with a 2 line main() program, even if you have the assignment operator working perfectly. `Board::Board(const Board& other);` That's the function that's missing. – PaulMcKenzie Apr 04 '15 at 19:44
  • @Don - Why a 2d array of pointers? If you need a board of 64 squares, why not `Piece grid[64]`? Then all the `Board` ops would be trivial. – Bo Persson Apr 06 '15 at 10:16

0 Answers0