i want to make a chess game consisting of the following classes:
Game // will have a player as a private member, the game object will be the interface between the user and the game i.e it takes in input and passes it to the other objects like Player or the Board.
Player
Piece
Board //the board will consist of coordinates
Coordinate //the block on the chess board
I desgined my Piece to have a private member Player pointer that points to a player reference, so i can distinguish between player ownership, but my Player class contains an array of pieces. now i have:
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
|
#ifndef PIECE_H
#define PIECE_H
#include "Player.h"
class Piece
{
public:
Piece();
Piece(const Player &);
void moveTo(Board*);
void setAffinity(Player &);
Coordinate getCoordinate();
void setCoordinate();
Reach getReach() const;
void setReach(int);
protected:
affinity;
Reach reach;
Coordinate position;
int life;
};
#endif
|
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
|
#ifndef PLAYER_H
#define PLAYER_H
#include "Piece.h"
#include "Turn.h"
#include <vector>
class Player
{
public:
Player();
void addPiece(Piece);
Piece getPieces() const;
void endTurn();
void setTurn(Turn*);
bool isTurn() const;
virtual ~Player();
private:
vector<Piece*> pieces;
Turn* turn;
};
#endif
|
Also, my player class contains a Turn object, which utilizes the State pattern. heres it's header file:
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
|
#ifndef TURN_H
#define TURN_H
#include "Player.h"
class Turn
{
public:
Turn();
virtual void end(Player*) = 0;
virtual void move(Player*) = 0;
};
class Wait : public Turn
{
public:
virtual void end(Player* p)
{
p->setTurn(new Current());
delete this;
}
};
class Current : public Turn
{
public:
virtual void end(Player*)
{
p->setTurn(new Wait());
delete this;
}
};
#endif
|
As you can see Piece.h conversely includes Player.h and the same relationship between Player.h and Turn.h, which does not work.
Don't worry about the move method thats not implemented. I'm just trying to figure out how to get the turn class to capture movement actions and return a appropriate error if the wrong player accesses a piece.
I'm really unsure how to reslove this problem, i know i can put all the code in one header file, but that would be messy and unorganized, i wish to keep to coding standards. I also read up on using smart pointers. using the memory library and std::auto_ptr<> though it also seems to be a bad form of coding. How else would i be able to work around this problem?