No globals #50

Merged
rektdie merged 23 commits from no_globals into main 2025-04-12 13:52:41 +02:00
6 changed files with 34 additions and 37 deletions
Showing only changes of commit 2e8c23103f - Show all commits

No files matched your search

-2
View File
@@ -65,7 +65,6 @@ void RunBenchmark() {
Board board; Board board;
int depth = BENCH_DEPTH; int depth = BENCH_DEPTH;
int ply = 0; int ply = 0;
board.positionIndex = 0;
// Clearing // Clearing
TT.Clear(); TT.Clear();
@@ -81,5 +80,4 @@ void RunBenchmark() {
std::cout << ctx.nodes << " nodes " << int(ctx.nodes/sw.GetElapsedSec()) << " nps" << std::endl; std::cout << ctx.nodes << " nodes " << int(ctx.nodes/sw.GetElapsedSec()) << " nps" << std::endl;
TT.Clear(); TT.Clear();
board.positionIndex = 0;
} }
-26
View File
@@ -393,7 +393,6 @@ bool Board::MakeMove(Move move) {
} }
positionIndex++; positionIndex++;
positionHistory[positionIndex] = hashKey;
return true; return true;
} }
@@ -444,28 +443,3 @@ void Board::ResetAccPair() {
blackPieces.PopBit(square); blackPieces.PopBit(square);
} }
} }
static bool IsThreefold(Board& board) {
for (int i = 0; i < board.positionIndex; i++) {
if (positionHistory[i] == board.hashKey) {
// repetition found
return true;
}
}
// no repetition
return false;
}
static bool IsFifty(Board& board) {
return (board.halfMoves >= 100);
}
static bool IsInsuffMat(Board& board) {
return (board.occupied.PopCount() <= 3
&& !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook]));
}
bool Board::IsDraw() {
return IsFifty(*this) || IsInsuffMat(*this) || IsThreefold(*this);
}
-4
View File
@@ -10,8 +10,6 @@ constexpr int noEPTarget = -1;
constexpr int MAX_MOVES = 218; constexpr int MAX_MOVES = 218;
inline U64 positionHistory[1000];
class Board { class Board {
public: public:
Board() { Board() {
@@ -67,6 +65,4 @@ public:
bool MakeMove(Move move); bool MakeMove(Move move);
bool InPossibleZug(); bool InPossibleZug();
bool IsDraw();
}; };
+3 -3
View File
@@ -62,8 +62,8 @@ static void PlayRandMoves(Board &board) {
MOVEGEN::GenerateMoves<All>(board); MOVEGEN::GenerateMoves<All>(board);
} }
static bool IsGameOver(Board &board) { static bool IsGameOver(Board &board, SEARCH::SearchContext& ctx) {
if (board.IsDraw()) return true; if (SEARCH::IsDraw(board, ctx)) return true;
int moveSeen = 0; int moveSeen = 0;
@@ -106,7 +106,7 @@ void PlayGame(std::atomic<int>& positions, U64 targetPositions, std::vector<Game
SEARCH::SearchResults safeResults; SEARCH::SearchResults safeResults;
MOVEGEN::GenerateMoves<All>(board); MOVEGEN::GenerateMoves<All>(board);
while (!IsGameOver(board)) { while (!IsGameOver(board, ctx)) {
SEARCH::SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx); SEARCH::SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx);
if (!results.bestMove) break; if (!results.bestMove) break;
+28 -1
View File
@@ -92,6 +92,31 @@ void ListScores(Board &board, int ply, SearchContext& ctx) {
} }
} }
static bool IsThreefold(Board& board, SearchContext& ctx) {
for (int i = 0; i < board.positionIndex; i++) {
if (ctx.positionHistory[i] == board.hashKey) {
// repetition found
return true;
}
}
// no repetition
return false;
}
static bool IsFifty(Board& board) {
return (board.halfMoves >= 100);
}
static bool IsInsuffMat(Board& board) {
return (board.occupied.PopCount() <= 3
&& !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook]));
}
static bool IsDraw(Board &board, SearchContext& ctx) {
return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board, ctx);
}
static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply) { static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply) {
int reduction = 0; int reduction = 0;
@@ -147,6 +172,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply, Searc
Board copy = board; Board copy = board;
int isLegal = copy.MakeMove(board.moveList[i]); int isLegal = copy.MakeMove(board.moveList[i]);
if (!isLegal) continue; if (!isLegal) continue;
ctx.positionHistory[copy.positionIndex] = copy.hashKey;
ctx.nodes++; ctx.nodes++;
int score = -Quiescence<mode>(copy, -beta, -alpha, ply + 1, ctx).score; int score = -Quiescence<mode>(copy, -beta, -alpha, ply + 1, ctx).score;
@@ -191,7 +217,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo
} }
ctx.pvLine.SetLength(ply); ctx.pvLine.SetLength(ply);
if (ply && (board.IsDraw())) return 0; if (ply && (IsDraw(board, ctx))) return 0;
// if NOT PV node then we try to hit the TTable // if NOT PV node then we try to hit the TTable
@@ -261,6 +287,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo
bool isLegal = copy.MakeMove(currMove); bool isLegal = copy.MakeMove(currMove);
if (!isLegal) continue; if (!isLegal) continue;
ctx.positionHistory[copy.positionIndex] = copy.hashKey;
ctx.nodes++; ctx.nodes++;
int reductions = GetReductions(board, currMove, depth, moveSeen, ply); int reductions = GetReductions(board, currMove, depth, moveSeen, ply);
+3 -1
View File
@@ -113,6 +113,7 @@ public:
History history; History history;
std::array<std::array<int, MAX_PLY>, 2> killerMoves{}; std::array<std::array<int, MAX_PLY>, 2> killerMoves{};
std::array<U64, 1000> positionHistory;
Stopwatch sw; Stopwatch sw;
@@ -121,6 +122,7 @@ public:
history.Clear(); history.Clear();
sw.Restart(); sw.Restart();
killerMoves = {}; killerMoves = {};
positionHistory = {};
} }
}; };
@@ -130,7 +132,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo
template <searchMode mode> template <searchMode mode>
SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx);
bool IsDraw(Board &board); bool IsDraw(Board &board, SearchContext& ctx);
constexpr int moveScoreTable[6][6] = { constexpr int moveScoreTable[6][6] = {
105, 205, 305, 405, 505, 605, 105, 205, 305, 405, 505, 605,