aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam <adammegarules1@gmail.com>2026-07-29 08:49:58 +0200
committerAdam <adammegarules1@gmail.com>2026-07-29 08:49:58 +0200
commit815abe7aabf47814dc391d19dedd6e9b23f100a1 (patch)
treed7a957fef8a6ea5c00bd84bf1b8046004421785c
parentccb929172587fbfb8fb000d7992bdbbdac6f53ae (diff)
adding transpositions
-rw-r--r--src/board.hpp1
-rw-r--r--src/bot.cpp27
-rw-r--r--src/main.cpp2
-rw-r--r--src/uci.cpp20
-rw-r--r--src/uci.hpp2
5 files changed, 38 insertions, 14 deletions
diff --git a/src/board.hpp b/src/board.hpp
index 9418571..687510e 100644
--- a/src/board.hpp
+++ b/src/board.hpp
@@ -46,6 +46,7 @@ struct Game {
bool canEnpassant = 0;
GameState state = TURN;
std::unordered_map<uint64_t, int> ThreeFoldMap;
+ std::unordered_map<uint64_t, float> *Transpositions = nullptr;
};
struct Move {
diff --git a/src/bot.cpp b/src/bot.cpp
index 1a156a2..8a570e4 100644
--- a/src/bot.cpp
+++ b/src/bot.cpp
@@ -1,14 +1,16 @@
#include "bot.hpp"
#include "board.hpp"
#include "moves.hpp"
+#include "zobrist.hpp"
#include <algorithm>
#include <cassert>
#include <cmath>
+#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <vector>
-const int SEARCH_DEPTH = 4;
+const int SEARCH_DEPTH = 5;
const int PAWN_VALUE = 100;
const int KNIGHT_VALUE = 320;
@@ -132,17 +134,25 @@ float minimax(int depth, Game *b, float alpha, float beta) {
if (depth == 0) {
return EvaluateBoardForWhite(b, depth);
}
+ uint64_t gameHash = GenerateZobristKey(b);
+
+ if (b->Transpositions->contains(gameHash)) {
+ float value = b->Transpositions->at(gameHash);
+ return value;
+ }
auto moves = GetLegalMoves(b);
if (moves.size() == 0) {
return EvaluateBoardForWhite(b, depth);
}
+ float bestEval;
if (b->turn) {
- float bestEval = -INFINITY;
+ bestEval = -INFINITY;
for (Move move : moves) {
UndoMove undo = MakeMove(move, b);
float eval = minimax(depth - 1, b, alpha, beta);
+
UnMakeMove(undo, b);
bestEval = std::max(bestEval, eval);
@@ -152,26 +162,27 @@ float minimax(int depth, Game *b, float alpha, float beta) {
break;
}
}
-
- return bestEval;
} else {
- float BestEval = INFINITY;
+ bestEval = INFINITY;
for (Move move : moves) {
UndoMove undo = MakeMove(move, b);
float eval = minimax(depth - 1, b, alpha, beta);
+
UnMakeMove(undo, b);
- BestEval = std::min(BestEval, eval);
+ bestEval = std::min(bestEval, eval);
- beta = std::min(beta, BestEval);
+ beta = std::min(beta, bestEval);
if (alpha >= beta) {
break; // *snips*
}
}
- return BestEval;
}
+
+ b->Transpositions->operator[](gameHash) = bestEval;
+ return bestEval;
}
int PSTIndex(int square, bool white) { return white ? square : (56 ^ square); }
diff --git a/src/main.cpp b/src/main.cpp
index 8a1821e..0bdd150 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -4,6 +4,8 @@
#include <cstdlib>
#include <ctime>
+using namespace std;
+
int main() {
InitZobrist();
srand(time(0));
diff --git a/src/uci.cpp b/src/uci.cpp
index 4e32d6f..b4e384a 100644
--- a/src/uci.cpp
+++ b/src/uci.cpp
@@ -15,6 +15,7 @@
#include <chrono>
#include <fstream>
#include <iomanip>
+#include <unordered_map>
std::ofstream uciLog("uci_log.txt", std::ios::app);
@@ -44,7 +45,8 @@ Move UciToMove(string uci) {
return {from, to};
}
-static Game initBoard(string startingFEN) {
+Game initBoard(string startingFEN,
+ std::unordered_map<uint64_t, float> *transPositions) {
Game b{};
b.enPassant = IndexToPosition(0); // default value
b.canEnpassant = false;
@@ -53,7 +55,7 @@ static Game initBoard(string startingFEN) {
b.castle = "";
b.halfMoveClock = 0;
b.MoveClock = 0;
- setBoardFen(startingFEN, &b);
+ b.Transpositions = transPositions;
Piece EmptyPiece = {
false,
@@ -64,11 +66,15 @@ static Game initBoard(string startingFEN) {
b.pieces[i] = EmptyPiece;
};
+ setBoardFen(startingFEN, &b);
+
return b;
};
void Uci() {
+ unordered_map<uint64_t, float> transpositions = {};
Game game =
- initBoard("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
+ initBoard("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
+ &transpositions);
string line;
@@ -85,7 +91,8 @@ void Uci() {
cout.flush();
} else if (cmd == "ucinewgame") {
game =
- initBoard("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
+ initBoard("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
+ &transpositions);
} else if (cmd == "isready") {
cout << "readyok\n";
cout.flush();
@@ -126,7 +133,8 @@ void Uci() {
if (token == "startpos") {
game = initBoard(
- "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
+ "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
+ &transpositions);
} else if (token == "fen") {
string fen;
string part;
@@ -139,7 +147,7 @@ void Uci() {
fen += part;
}
- game = initBoard(fen);
+ game = initBoard(fen, &transpositions);
}
// check for moves
diff --git a/src/uci.hpp b/src/uci.hpp
index 4d0a91b..4d84215 100644
--- a/src/uci.hpp
+++ b/src/uci.hpp
@@ -7,4 +7,6 @@
Move UciToMove(std::string uci);
void LogUci(const std::string &message);
void Uci();
+Game initBoard(std::string startingFEN,
+ std::unordered_map<uint64_t, float> *transPositions);
#endif /* SRC_UCI_H_ */