diff options
| author | Adam <adammegarules1@gmail.com> | 2026-08-18 11:26:44 +0200 |
|---|---|---|
| committer | Adam <adammegarules1@gmail.com> | 2026-08-18 11:26:44 +0200 |
| commit | 419f931a8001ca1c8c5960d68ae6bdef53c76f16 (patch) | |
| tree | b6162a5d4324fb4df8e831aff3a1a869d794daca | |
| parent | 8a3d56d5bb57b58fa60966edcec0b33d3c2d1041 (diff) | |
refactor(book): refactoring opening book code
| -rw-r--r-- | src/book.cpp | 18 | ||||
| -rw-r--r-- | src/book.hpp | 4 | ||||
| -rw-r--r-- | src/bot.cpp | 11 | ||||
| -rw-r--r-- | src/uci.cpp | 27 | ||||
| -rw-r--r-- | src/zobrist.cpp | 9 |
5 files changed, 32 insertions, 37 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; } diff --git a/src/book.hpp b/src/book.hpp index 4382dc7..bc08782 100644 --- a/src/book.hpp +++ b/src/book.hpp @@ -1,7 +1,7 @@ #pragma once #include <cstdint> +#include <optional> #include <string> void InitBook(const std::string &path); -uint16_t ProbeBook(uint64_t key); -int GetBookDepth(); +std::optional<uint16_t> ProbeBook(uint64_t key);
\ No newline at end of file diff --git a/src/bot.cpp b/src/bot.cpp index 7b937bd..99f60aa 100644 --- a/src/bot.cpp +++ b/src/bot.cpp @@ -12,6 +12,7 @@ #include <vector> #include "board/board.hpp" +#include "book.hpp" #include "bot.hpp" #include "evaluate.hpp" #include "moves.hpp" @@ -19,6 +20,7 @@ constexpr int DEFAULT_TIME = 7; constexpr int Q_DEPTH_LIMIT = 4; +constexpr int BOOK_DEPTH = 100; constexpr int MATE = 10000; @@ -320,6 +322,15 @@ uint16_t GetBestMove(Game *b, int maxDepth, move_options options) { searchStopped = false; Nodes = 0; + uint64_t hash = GenerateZobristKey(b); + + if (b->MoveClock <= BOOK_DEPTH) { + std::optional<uint16_t> bookMove = ProbeBook(hash); + if (bookMove.has_value()) { + return bookMove.value(); + } + } + // use depth INF if caller hasnt provided depth int actualDepth = maxDepth > 0 ? maxDepth : INF; diff --git a/src/uci.cpp b/src/uci.cpp index 23e8419..812a0cd 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -13,8 +13,8 @@ #include "board/board.hpp" #include "board/fen.hpp" -#include "bot.hpp" #include "book.hpp" +#include "bot.hpp" #include "misc.hpp" #include "moves.hpp" #include "uci.hpp" @@ -224,24 +224,13 @@ static void GoCommand(Game *game, std::string &cmd) { exit(1); } - // Try opening book first - uint16_t bookMove = 0; - if (game->MoveClock < GetBookDepth()) { - bookMove = ProbeBook(GenerateZobristKey(game)); - } - - uint16_t best; - if (bookMove != 0) { - best = bookMove; - } else { - move_options options = { - .wtime = wtime, - .btime = btime, - .wncr = wncr, - .bncr = bncr, - }; - best = GetBestMove(game, depth, options); - } + move_options options = { + .wtime = wtime, + .btime = btime, + .wncr = wncr, + .bncr = bncr, + }; + uint16_t best = GetBestMove(game, depth, options); Piece piece = game->pieces[getFromValueFromMove(best)]; diff --git a/src/zobrist.cpp b/src/zobrist.cpp index 5250687..c06f1a2 100644 --- a/src/zobrist.cpp +++ b/src/zobrist.cpp @@ -312,9 +312,10 @@ static bool IsEnpassantLegal(Game *g) { // PolyglotRandom64 layout (standard Polyglot spec): // [0..767] piece keys, kind = (pieceType-1)*2 + color: // [0-63] black pawn, [64-127] white pawn, [128-191] black knight, -// [192-255] white knight, [256-319] black bishop, [320-383] white bishop, -// [384-447] black rook, [448-511] white rook, [512-575] black queen, -// [576-639] white queen, [640-703] black king, [704-767] white king +// [192-255] white knight, [256-319] black bishop, [320-383] white +// bishop, [384-447] black rook, [448-511] white rook, [512-575] +// black queen, [576-639] white queen, [640-703] black king, +// [704-767] white king // [768] white kingside castling // [769] white queenside castling // [770] black kingside castling @@ -326,7 +327,7 @@ void InitZobrist() { // Piece keys: interleaved by kind = (pieceType-1)*2 + color for (int kind = 0; kind < 12; ++kind) { - int color = kind % 2; // 0=black, 1=white + int color = kind % 2; // 0=black, 1=white int pieceType = (kind / 2) + 1; // PAWN=1..KING=6 for (int square = 0; square < 64; ++square) { PieceKeys[color][pieceType][square] = PolyglotRandom64[idx++]; |
