added Transposition tables

This commit is contained in:
rektdie committed 2024-07-13 22:20:15 +02:00
1 parent 2e984643c0
commit 7c4fbc1f6d
4 files changed
+93 -15

No files matched your search

+41 -15
View File
@@ -2,6 +2,7 @@
#include "movegen.h"
#include "evaluate.h"
#include <algorithm>
#include "tt.h"
static inline int nodes = 0;
static inline int ply = 0;
@@ -65,16 +66,26 @@ void ListScores(Board &board) {
for (int i = 0; i < board.currentMoveIndex; i++) {
Move currentMove = board.moveList[i];
std::cout << squareCoords[currentMove.MoveFrom()] << squareCoords[currentMove.MoveTo()]; std::cout << ": " << ScoreMove(board, currentMove) << '\n';
std::cout << squareCoords[currentMove.MoveFrom()] << squareCoords[currentMove.MoveTo()];
std::cout << ": " << ScoreMove(board, currentMove) << '\n';
}
}
static int Quiescence(Board board, int alpha, int beta) {
int score = ReadEntry(board.hashKey, ply, alpha, beta);
if (score != 10000) {
return score;
}
int nodeType = AllNode;
nodes++;
int staticScore = Evaluate(board);
if (staticScore >= beta) {
WriteEntry(board.hashKey, ply, beta, PV);
return beta;
}
@@ -90,25 +101,39 @@ static int Quiescence(Board board, int alpha, int beta) {
if (board.moveList[i].IsCapture()) {
Board copy = board;
board.MakeMove(board.moveList[i]);
int score = -Quiescence(board, -beta, -alpha);
score = -Quiescence(board, -beta, -alpha);
board = copy;
if (score >= beta) {
WriteEntry(board.hashKey, ply, beta, CutNode);
return beta;
}
if (score > alpha) {
nodeType = PV;
alpha = score;
}
}
}
WriteEntry(board.hashKey, ply, alpha, nodeType);
return alpha;
}
static int NegaMax(Board board, int depth, int alpha, int beta, bool isRoot) {
int score = ReadEntry(board.hashKey, depth, alpha, beta);
if (score != 10000) {
return score;
}
int nodeType = AllNode;
if (depth == 0) {
return Quiescence(board, alpha, beta);
score = Quiescence(board, alpha, beta);
// Write to TT (PV)
WriteEntry(board.hashKey, depth, score, PV);
return score;
}
nodes++;
@@ -127,15 +152,13 @@ static int NegaMax(Board board, int depth, int alpha, int beta, bool isRoot) {
}
}
int max = -50000;
for (int i = 0; i < board.currentMoveIndex; i++) {
Board copy = board;
board.MakeMove(board.moveList[i]);
ply++;
int score = -NegaMax(board, depth - 1, -beta, -alpha, false);
board = copy; ply--;
score = -NegaMax(board, depth - 1, -beta, -alpha, false);
board = copy;
ply--;
if (score >= beta) {
if (board.moveList[i].IsCapture()) {
@@ -144,29 +167,31 @@ static int NegaMax(Board board, int depth, int alpha, int beta, bool isRoot) {
killerMoves[0][ply] = board.moveList[i];
}
// Write to TT (CutNode)
WriteEntry(board.hashKey, depth, beta, CutNode);
return beta;
}
if (score > alpha) {
alpha = score;
}
if (score > max) {
max = score;
// Write to TT (PV)
// if this doesnt occur, then it remains AllNode
nodeType = PV;
}
if (isRoot) {
if (max > bestScore) {
bestScore = max;
if (alpha > bestScore) {
bestScore = alpha;
bestMove = board.moveList[i];
}
}
}
return max;
WriteEntry(board.hashKey, depth, alpha, nodeType);
return alpha;
}
void SearchPosition(Board &board, int depth) {
bestScore = -50000;
bestMove = Move();
@@ -174,4 +199,5 @@ void SearchPosition(Board &board, int depth) {
std::cout << "info depth " << depth << " nodes " << nodes << '\n';
std::cout << "bestmove " << squareCoords[bestMove.MoveFrom()]
<< squareCoords[bestMove.MoveTo()] << '\n';
nodes = 0;
}