From 2f512931427121ea287cf2812ddf0b3fd6d7b83c Mon Sep 17 00:00:00 2001 From: Adam Date: Wed, 29 Jul 2026 11:16:11 +0200 Subject: saving cache to dish --- src/bot.cpp | 7 +------ src/uci.cpp | 38 ++++++++++++++++++++++++++++++++++---- src/uci.hpp | 6 ++++++ 3 files changed, 41 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/bot.cpp b/src/bot.cpp index 84949bb..f114f36 100644 --- a/src/bot.cpp +++ b/src/bot.cpp @@ -145,10 +145,8 @@ float minimax(int depth, Game *b, float alpha, float beta) { return EvaluateBoardForWhite(b, depth); } - float bestEval; + float bestEval = b->turn ? -INFINITY : INFINITY; if (b->turn) { - bestEval = -INFINITY; - for (Move move : moves) { UndoMove undo = MakeMove(move, b); @@ -164,10 +162,7 @@ float minimax(int depth, Game *b, float alpha, float beta) { } } } else { - bestEval = INFINITY; - for (Move move : moves) { - UndoMove undo = MakeMove(move, b); float eval = minimax(depth - 1, b, alpha, beta); diff --git a/src/uci.cpp b/src/uci.cpp index b45df57..310d31c 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -17,11 +18,40 @@ #include #include -std::ofstream uciLog("uci_log.txt", std::ios::app); +std::ofstream uciLog("cache/uci_log.txt", std::ios::app); + +bool LoadTable( + const std::string &filename, + std::unordered_map *transpositions) { + std::ifstream file(filename); + + if (!file.is_open()) + return false; + + uint64_t hash; + int depth; + float eval; + + while (file >> hash >> depth >> eval) { + transpositions->operator[](hash) = {depth, eval}; + } + + return true; +} +void SaveTranspositionTable( + const std::unordered_map &table) { + + std::ofstream file("cache/positions.txt", std::ios::trunc); + + for (const auto &[key, value] : table) { + file << key << ' ' << value.depth << ' ' << value.Eval << '\n'; + } +}; void LogUci(const std::string &message) { - if (!uciLog.is_open()) + if (!uciLog.is_open()) { return; + } auto now = std::chrono::system_clock::now(); auto time = std::chrono::system_clock::to_time_t(now); @@ -76,7 +106,7 @@ void Uci() { Game game = initBoard("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", &transpositions); - + LoadTable("cache/positions.txt", &transpositions); string line; while (getline(cin, line)) { @@ -122,7 +152,7 @@ void Uci() { } } println(); - + SaveTranspositionTable(transpositions); cout.flush(); } else if (cmd.starts_with("position")) { stringstream ss(line); diff --git a/src/uci.hpp b/src/uci.hpp index 9352c48..7b6dca4 100644 --- a/src/uci.hpp +++ b/src/uci.hpp @@ -3,6 +3,7 @@ #include "board.hpp" #include +#include Move UciToMove(std::string uci); void LogUci(const std::string &message); @@ -10,4 +11,9 @@ void Uci(); Game initBoard( std::string startingFEN, std::unordered_map *transPositions); +void SaveTranspositionTable( + const std::unordered_map &table); +bool LoadTable( + const std::string &filename, + std::unordered_map *transpositions); #endif /* SRC_UCI_H_ */ -- cgit v1.2.3