commit 7bb17c1e229bb64c927a65833273bd21fe07e2bd
parent 67d1edbf1daf0345b19c76305a4f6bd88bb70493
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date: Sat, 5 Aug 2023 15:04:04 +0200
Principle Variation Search
Diffstat:
2 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
@@ -3,7 +3,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
project(
Stellar
- VERSION 0.0.10
+ VERSION 0.0.11
DESCRIPTION "Chess engine written in C"
HOMEPAGE_URL https://git.dimitrijedobrota.com/stellar.git
LANGUAGES C
diff --git a/src/engine/engine.c b/src/engine/engine.c
@@ -153,6 +153,7 @@ int negamax(Stats *stats, const Board *board, int alpha, int beta, int depth) {
move_list_sort(stats, &list);
int legal_moves = 0;
+ int found_pv = 0;
for (int i = 0; i < move_list_size(&list); i++) {
Move move = move_list_move(&list, i);
@@ -162,7 +163,15 @@ int negamax(Stats *stats, const Board *board, int alpha, int beta, int depth) {
}
stats->ply++;
- int score = -negamax(stats, ©, -beta, -alpha, depth - 1);
+
+ int score = alpha;
+ if (found_pv)
+ score = -negamax(stats, ©, -alpha - 1, -alpha, depth - 1);
+
+ // no PVS or the last one failed
+ if (!found_pv || (score > alpha) && (score < beta))
+ score = -negamax(stats, ©, -beta, -alpha, depth - 1);
+
stats->ply--;
legal_moves++;
@@ -179,7 +188,7 @@ int negamax(Stats *stats, const Board *board, int alpha, int beta, int depth) {
stats->history_moves[piece_index(move_piece(move))]
[move_target(move)] += depth;
alpha = score;
-
+ found_pv = 1;
stats->pv_table[ply][ply] = move;
for (int i = stats->ply + 1; i < stats->pv_length[ply + 1]; i++)
stats->pv_table[ply][i] = stats->pv_table[ply + 1][i];
@@ -208,7 +217,6 @@ void search_position(const Board *board, int depth) {
for (int crnt = 1; crnt <= depth; crnt++) {
stats.follow_pv = 1;
- stats.nodes = 0;
int score = negamax(&stats, board, -50000, 50000, crnt);