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.