aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--src/bot.cpp7
-rw-r--r--src/uci.cpp38
-rw-r--r--src/uci.hpp6
4 files changed, 42 insertions, 10 deletions
diff --git a/.gitignore b/.gitignore
index 1adb011..81658aa 100644
--- a/.gitignore
+++ b/.gitignore
@@ -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_ */