chess implementation: removing a piece from the board

I have an exercise where I need to implement the basics for a chess game.
It's not needed to have a fully working visual chess game, but it's more an exercise on what I've learned so far using classes, class relationships, ...

I have the following class Pion (Pawn; sorry for the Dutch implementation)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef PION
#define PION

enum KLEUR { ZWART, WIT };

class Pion
{
public:
	Pion(KLEUR kleur):
	  m_kleur(kleur) {}

	KLEUR getKleur() const { return m_kleur; }
private:
	KLEUR m_kleur;
};
#endif 


I don't need any other pieces yet, so inheritance isn't a factor.

My problem lies with creating the GameBoard class.
Following is the description of the class as provided by the exercise:
This class represents a chess board and exists out of double array pointers to Pawns (initialised to NULL pointers). The class provides methods to place Pawns on the board, to remove Pawns from the board and to move Pawns on the board.

This is what I've got:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "Spelbord.h";
#include <cstdlib>
#include <string.h>


Spelbord::Spelbord()
{
	m_vakken = NULL;
}

void Spelbord::plaatsPion(Pion p, int loc_x, int loc_y)
{
	m_vakken[loc_x][loc_y] = p;
}

void Spelbord::verplaatsPion(int from_loc_x, int from_loc_y, int to_loc_x, int to_loc_y)
{
	Pion p = m_vakken[from_loc_x][from_loc_y];
	verwijderPion(from_loc_x, from_loc_y);
	plaatsPion(p, to_loc_x, to_loc_y);
}

void Spelbord::verwijderPion(int loc_x, int loc_y)
{
	Pion ** temp = NULL;
	for(int i=0; i<sizeof(m_vakken); i++)
	{
		for(int j=0; j<sizeof(m_vakken[i]); j++)
		{
			if(i != loc_x && j != loc_y)
				temp[i][j] = m_vakken[i][j];
		}
	}
	m_vakken = NULL;
	memcpy(m_vakken, temp, sizeof(temp));
}


I've put something together to remove a pawn from the board in the method
void Spelbord::verwijderPion(int, int);

When I see this I know there must be a better approach to remove an element from the * array. I just don't know what it is.
delete or assigning NULL to an element won't work.
It seems broken. If your board is a 2D array of Pion objects, then how do you differentiate
between a square that is empty, a square containing a white pawn, and a square containing
a black pawn? Obviously the answer to the latter two is by the color of the pawn (getKleur).

If you are making this program such that it will be extended later to support other pieces
besides pawns, then you will need a 2D array of base class pointers. (Piece, from which
Pion etc are derived).

So the answer to your question is for now, you should consider making a 2D array of
pointers to Pions, not a 2D array of Pions.
Is this what you mean with a 2D array of pointers to Pions ?
Pion * m_vakken[8][8];

It seems much more natural than what I first had and now I can use 'delete' to remove a Pion.

Keeping the extension in mind. How would you go about to implement a move. Every piece has different rules when it comes to moving around. So I suppose a pure virtual function move is needed in the abstract base class.
Though in my piece derivative classes there isn't any reference to the position of the object on the board. This makes it hard to program the rules.

Edit: My exercise advises me to implement a "Move" class which holds the number of positions a pawn can go left, right, forward, backwards. I have no clue how to implement this, especially when other pieces beside pawns come into play.
Last edited on
Topic archived. No new replies allowed.