diff --git a/docs/pages/extending-the-library.md b/docs/pages/extending-the-library.md index 79dabe6..6334b26 100644 --- a/docs/pages/extending-the-library.md +++ b/docs/pages/extending-the-library.md @@ -67,5 +67,8 @@ int main() { } ``` +> [!IMPORTANT] +> If you do this you must call setFen after creating the board, otherwise the board won't use the overriden placePiece function. + If this was still not enough for you, think about adding the desired functionality back to master, in case they are universal enough. diff --git a/include/chess.hpp b/include/chess.hpp index 285bd58..bff1bde 100644 --- a/include/chess.hpp +++ b/include/chess.hpp @@ -25,7 +25,7 @@ THIS FILE IS AUTO GENERATED DO NOT CHANGE MANUALLY. Source: https://github.com/Disservin/chess-library -VERSION: 0.6.68 +VERSION: 0.6.69 */ #ifndef CHESS_HPP @@ -1776,7 +1776,7 @@ class Board { explicit Board(std::string_view fen = constants::STARTPOS, bool chess960 = false) { prev_states_.reserve(256); chess960_ = chess960; - setFenInternal(fen); + setFenInternal(fen); } virtual void setFen(std::string_view fen) { setFenInternal(fen); } @@ -2794,8 +2794,25 @@ class Board { bool chess960_ = false; private: + void placePieceInternal(Piece piece, Square sq) { + assert(board_[sq.index()] == Piece::NONE); + + auto type = piece.type(); + auto color = piece.color(); + auto index = sq.index(); + + assert(type != PieceType::NONE); + assert(color != Color::NONE); + assert(index >= 0 && index < 64); + + pieces_bb_[type].set(index); + occ_bb_[color].set(index); + board_[index] = piece; + } + /// @brief [Internal Usage] /// @param fen + template void setFenInternal(std::string_view fen) { original_fen_ = fen; @@ -2854,7 +2871,14 @@ class Board { square -= 16; } else { auto p = Piece(std::string_view(&curr, 1)); - placePiece(p, square); + + // prevent warnings about virtual method bypassing virtual dispatch + if constexpr (ctor) { + placePieceInternal(p, Square(square)); + } else { + placePiece(p, square); + } + key_ ^= Zobrist::piece(p, Square(square)); ++square; } diff --git a/src/board.hpp b/src/board.hpp index 33e1ccd..3138a7b 100644 --- a/src/board.hpp +++ b/src/board.hpp @@ -121,7 +121,7 @@ class Board { explicit Board(std::string_view fen = constants::STARTPOS, bool chess960 = false) { prev_states_.reserve(256); chess960_ = chess960; - setFenInternal(fen); + setFenInternal(fen); } virtual void setFen(std::string_view fen) { setFenInternal(fen); } @@ -1139,8 +1139,25 @@ class Board { bool chess960_ = false; private: + void placePieceInternal(Piece piece, Square sq) { + assert(board_[sq.index()] == Piece::NONE); + + auto type = piece.type(); + auto color = piece.color(); + auto index = sq.index(); + + assert(type != PieceType::NONE); + assert(color != Color::NONE); + assert(index >= 0 && index < 64); + + pieces_bb_[type].set(index); + occ_bb_[color].set(index); + board_[index] = piece; + } + /// @brief [Internal Usage] /// @param fen + template void setFenInternal(std::string_view fen) { original_fen_ = fen; @@ -1199,7 +1216,14 @@ class Board { square -= 16; } else { auto p = Piece(std::string_view(&curr, 1)); - placePiece(p, square); + + // prevent warnings about virtual method bypassing virtual dispatch + if constexpr (ctor) { + placePieceInternal(p, Square(square)); + } else { + placePiece(p, square); + } + key_ ^= Zobrist::piece(p, Square(square)); ++square; } diff --git a/src/include.hpp b/src/include.hpp index 915cc51..e20ac8f 100644 --- a/src/include.hpp +++ b/src/include.hpp @@ -25,7 +25,7 @@ THIS FILE IS AUTO GENERATED DO NOT CHANGE MANUALLY. Source: https://github.com/Disservin/chess-library -VERSION: 0.6.68 +VERSION: 0.6.69 */ #ifndef CHESS_HPP