aboutsummaryrefslogtreecommitdiff
path: root/src/book.cpp
diff options
context:
space:
mode:
authorAdam <adammegarules1@gmail.com>2026-08-18 11:26:44 +0200
committerAdam <adammegarules1@gmail.com>2026-08-18 11:26:44 +0200
commit419f931a8001ca1c8c5960d68ae6bdef53c76f16 (patch)
treeb6162a5d4324fb4df8e831aff3a1a869d794daca /src/book.cpp
parent8a3d56d5bb57b58fa60966edcec0b33d3c2d1041 (diff)
refactor(book): refactoring opening book code
Diffstat (limited to 'src/book.cpp')
-rw-r--r--src/book.cpp18
1 files changed, 6 insertions, 12 deletions
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 <cstdlib>
#include <fstream>
#include <iostream>
+#include <optional>
#include <random>
#include <vector>
#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<uint16_t> 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; }