Merge pull request #2 from rektdie/TT

connected TT to search
This commit was merged in pull request #2.
This commit is contained in:
rektdie authored and GitHub committed 2025-02-27 04:09:24 +01:00
commit 272fa9a04c
4 files changed
+28 -10

No files matched your search

+19 -6
View File
@@ -128,17 +128,27 @@ SearchResults PVS(Board board, int depth, int alpha, int beta) {
} }
} }
nodes++; // if NOT PV node then we try to hit the TTable
if (depth == 0) return Quiescence(board, alpha, beta).score; if (beta - alpha == 1) {
SearchResults entry = ReadEntry(board.hashKey, depth, alpha, beta);
if (entry.score != invalidEntry) {
return entry;
}
}
int score = -50000; if (depth == 0) return Quiescence(board, alpha, beta);
int score = -inf;
int nodeType = AllNode;
nodes++;
GenerateMoves(board, board.sideToMove); GenerateMoves(board, board.sideToMove);
SortMoves(board); SortMoves(board);
if (board.currentMoveIndex == 0) { if (board.currentMoveIndex == 0) {
if (board.InCheck(board.sideToMove)) { // checkmate if (board.InCheck(board.sideToMove)) { // checkmate
return -49000 + ply; return -99000 + ply;
} else { // stalemate } else { // stalemate
return 0; return 0;
} }
@@ -172,10 +182,12 @@ SearchResults PVS(Board board, int depth, int alpha, int beta) {
} }
if (score >= beta) { if (score >= beta) {
WriteEntry(board.hashKey, depth, beta, CutNode);
return beta; return beta;
} }
if (score > alpha) { if (score > alpha) {
nodeType = PV;
alpha = score; alpha = score;
results.bestMove = board.moveList[i]; results.bestMove = board.moveList[i];
} }
@@ -183,6 +195,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta) {
results.score = alpha; results.score = alpha;
WriteEntry(board.hashKey, depth, results.score, nodeType);
return results; return results;
} }
@@ -194,13 +207,13 @@ static SearchResults ID(Board &board, SearchParams params) {
int inc = board.sideToMove ? params.binc : params.winc; int inc = board.sideToMove ? params.binc : params.winc;
SearchResults safeResults; SearchResults safeResults;
safeResults.score = -50000; safeResults.score = -inf;
for (int depth = 1; depth <= 99; depth++) { for (int depth = 1; depth <= 99; depth++) {
int timeRemaining = (fullTime / 20) + (inc / 2); int timeRemaining = (fullTime / 20) + (inc / 2);
timeToSearch = timeRemaining; timeToSearch = timeRemaining;
SearchResults currentResults = PVS(board, depth, -50000, 50000); SearchResults currentResults = PVS(board, depth, -inf, inf);
if (currentResults.score >= safeResults.score) { if (currentResults.score >= safeResults.score) {
safeResults = currentResults; safeResults = currentResults;
} }
+4
View File
@@ -5,6 +5,10 @@
extern int nodes; extern int nodes;
extern bool benchStarted; extern bool benchStarted;
constexpr int invalidEntry = 111111;
constexpr int inf = 100000;
class SearchResults { class SearchResults {
public: public:
int score = 0; int score = 0;
+3 -3
View File
@@ -79,7 +79,7 @@ U64 GetHashKey(Board &board) {
return key; return key;
} }
int ReadEntry(U64 &hashKey, int depth, int alpha, int beta) { SearchResults ReadEntry(U64 &hashKey, int depth, int alpha, int beta) {
TTEntry *current = &TTable[hashKey % hashSize]; TTEntry *current = &TTable[hashKey % hashSize];
if (current->hashKey == hashKey) { if (current->hashKey == hashKey) {
@@ -94,8 +94,8 @@ int ReadEntry(U64 &hashKey, int depth, int alpha, int beta) {
} }
} }
// returning unknown value // returning invalid value
return 10000; return invalidEntry;
} }
void WriteEntry(U64 &hashKey, int depth, int score, int nodeType) { void WriteEntry(U64 &hashKey, int depth, int score, int nodeType) {
+2 -1
View File
@@ -1,5 +1,6 @@
#pragma once #pragma once
#include "board.h" #include "board.h"
#include "search.h"
// 5 MB // 5 MB
constexpr int hashSize = 0x500000; constexpr int hashSize = 0x500000;
@@ -28,5 +29,5 @@ public:
extern TTEntry TTable[hashSize]; extern TTEntry TTable[hashSize];
int ReadEntry(U64 &hashKey, int depth, int alpha, int beta); SearchResults ReadEntry(U64 &hashKey, int depth, int alpha, int beta);
void WriteEntry(U64 &hashKey, int depth, int score, int nodeType); void WriteEntry(U64 &hashKey, int depth, int score, int nodeType);