C++ Chess board design and smart pointers [closed]

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật