I wrote a Chess engine in Java and I am porting it over to C++. I am new to C++.
The idea:
I have a Board object which holds a 2-dimensionnal array of Piece objects. Queen, Rook, Bishop, etc are subclasses of Piece. They all have a method getPossibleMoves(). Therefore they need to have a reference to the board to be able to generate the possible moves.
The implementation:
Board class:
class Board
{
public:
using PiecePtr = std::shared_ptr<Piece>;
std::array<std::array<PiecePtr, BOARD_SIZE>, BOARD_SIZE> myPieces;
};
Piece class:
class Piece
{
public:
std::weak_ptr<Board> myBoard
};
I used shared_ptr and weak_ptr because the Board can/should exist whitout Pieces. Is this approach correct?
Note: I know this OOP approch to represent the chess board is not the best for performance. Other approaches (Bitboards for instance) will be more efficient.
14
This is a case of premature over-design.
The pieces don’t need a pointer to the board. Just pass the board to the ‘generateMoves’ method. You don’t even need objects for the individual chessmen. All pawns are alike, so you only need one Pawn instance.
Also, by the laws of chess, there is a game state that is also needed to correctly generate moves. Pawns can capture e.p. but only after a previous two-square advance. A king can castle with a rook but not if either has previously moved.
My suggestion is to forget all the class design stuff. Start with a single ChessGame class with two methods: generateMoves and makeMove. Write it test-first. Start by only having two kings on the board. Then add the different kinds of chessmen one at a time. Extract classes as you find it convenient.
A small “being there, done that” suggestion… migrating my chess engine from a 0x88 board representation to bitboard was relatively simple. Obtaining a performance improvement was way harder than I expected.
The fact is that you can have a wonderful design for your board/piece class, perfect modularity, minimum dependencies among classes… but it’s sure that you must have arranged your evaluation logic depending on what you can do fast or slow (and this is linked to board representation).
Changing the board representation will trigger changes in the static evaluation logic: that’s complex, time consuming and risky.
Moreover, while you change the evaluation logic, you discover that some search extensions aren’t needed anymore or that you need a better quiescence search.
Eventually you could have a completely different player.
So you can start with a “temporary” board representation but everything should be done bearing in mind the final representation (strong/weak points of a representation you are not using could be unexpected).
Your implementation is the typical Team/Members relationship.
The Team object (the board) will have pointers to its Members (the pieces) and the members will also have a back pointer to their Team object.
So weak_ptr
is used to break the dependency cycle: the “owner” use shared_ptr
and the “owned” use a weak_ptr
to its parent and convert it temporarily to shared_ptr
when it needs access to its parent.
So, from this point of view, it’s a correct implementation.
However the getPossibleMoves()
function will need the piece position to perform its task.
The function could find out the position scanning the board but this would slow down the move generation. To avoid data redundancy you shouldn’t store the coordinates inside Piece
and getPossibleMoves()
will need two arguments:
std::list<Move> ThePiece::getPossibleMoves(unsigned row, unsigned column)
{
// ...
}
// ...
Board b;
// ...
auto moves = b[row][column]->getPossibleMoves(row, column);
Now why not remove the myBoard
injected dependency from Piece
and add one more argument to getPossibleMoves
?
std::list<Move> ThePiece::getPossibleMoves(const Board &b, unsigned row, unsigned column)
{
// ...
}
After this change the shared_ptr aren‘t required anymore… along this way we‘re going to completely change the design.
Please review the background and use cases for weak_ptr. I don’t think this is one of them.
weak_ptr is typically used for as a cache. The owner of the weak_ptr must be ok with the weak_ptr being null. If the weak_ptr is null, the owner can adapt by constructing a new instance or otherwise operating differently.
In your case, a Piece cannot operate without a Board. The weak_ptr should be a smart_ptr.
I would go farther and say that you should not be using pointers at all. The entire system is well defined without any pointers. Try an entirely value based design. For example, the Board for the pieces could be a reference instead of a pointer.
6