From 419f931a8001ca1c8c5960d68ae6bdef53c76f16 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 18 Aug 2026 11:26:44 +0200 Subject: refactor(book): refactoring opening book code --- src/book.cpp | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) (limited to 'src/book.cpp') diff --git a/src/book.cpp b/src/book.cpp index e1cd127..abf9693 100644 --- a/src/book.cpp +++ b/src/book.cpp @@ -6,13 +6,12 @@ #include #include #include +#include #include #include #include "board/board.hpp" -constexpr int BOOK_DEPTH = 16; - struct BookEntry { uint64_t key; uint16_t move; @@ -27,7 +26,7 @@ static std::mt19937 rng{std::random_device{}()}; void InitBook(const std::string &path) { std::ifstream file(path, std::ios::binary); if (!file.is_open()) { - std::cerr << "info string Could not open book file: " << path << "\n"; + std::cerr << "info string Failed to open: " << path << "\n"; return; } @@ -74,12 +73,9 @@ void InitBook(const std::string &path) { ((l & 0x00FF0000U) >> 8) | ((l & 0xFF000000U) >> 24); } - std::cerr << "info string Book loaded: " << book.size() << " entries\n"; + std::cerr << "info string " << path << ": " << book.size() << " entries\n"; } -// Convert Polyglot move to Mono internal move format. -// Polyglot: bits 0-5=to, 6-11=from, 12-14=promo (0=N,1=B,2=R,3=Q) -// Mono: bits 0-1=promo (00=N,11=B,01=R,10=Q), 2-7=from, 8-13=to static uint16_t PolyglotToMono(uint16_t polyglotMove) { uint8_t to = polyglotMove & 63; uint8_t from = (polyglotMove >> 6) & 63; @@ -106,9 +102,9 @@ static uint16_t PolyglotToMono(uint16_t polyglotMove) { return CreateMove(from, to, promotion); } -uint16_t ProbeBook(uint64_t key) { +std::optional ProbeBook(uint64_t key) { if (book.empty()) { - return 0; + return {}; } // Binary search for first matching entry @@ -124,7 +120,7 @@ uint16_t ProbeBook(uint64_t key) { } if (matches.empty()) { - return 0; + return {}; } // Weighted random selection @@ -145,5 +141,3 @@ uint16_t ProbeBook(uint64_t key) { return PolyglotToMono(matches.back()->move); } - -int GetBookDepth() { return BOOK_DEPTH; } -- cgit v1.2.3