diff options
| author | Adam <adammegarules1@gmail.com> | 2026-07-29 11:16:11 +0200 |
|---|---|---|
| committer | Adam <adammegarules1@gmail.com> | 2026-07-29 11:16:11 +0200 |
| commit | 2f512931427121ea287cf2812ddf0b3fd6d7b83c (patch) | |
| tree | 76c938fe92604e8c22f6af784b886d2c68beeee6 | |
| parent | 165fecafb6703edbec84fca6410c3f7f4005c4cc (diff) | |
saving cache to dish
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | src/bot.cpp | 7 | ||||
| -rw-r--r-- | src/uci.cpp | 38 | ||||
| -rw-r--r-- | src/uci.hpp | 6 |
4 files changed, 42 insertions, 10 deletions
@@ -4,3 +4,4 @@ tmp .cache/ uci_log.txt moves.txt +cache/ 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 <algorithm> #include <cassert> #include <cctype> +#include <cstdint> #include <iostream> #include <print> #include <sstream> @@ -17,11 +18,40 @@ #include <iomanip> #include <unordered_map> -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<uint64_t, TranspositionsEntry> *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<uint64_t, TranspositionsEntry> &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 <string> +#include <unordered_map> 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<uint64_t, TranspositionsEntry> *transPositions); +void SaveTranspositionTable( + const std::unordered_map<uint64_t, TranspositionsEntry> &table); +bool LoadTable( + const std::string &filename, + std::unordered_map<uint64_t, TranspositionsEntry> *transpositions); #endif /* SRC_UCI_H_ */ |
