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.