summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-09-09 23:28:03 +0300
committereug-vs <eugene@eug-vs.xyz>2022-09-10 04:20:40 +0300
commit8cdd60bca12974870f7ec8ea82c175b181cd1132 (patch)
treeb847698d21005c133e6f34ed2cb29a188c1e1201
parent0681e521d8a81a640ba2a4019dcc73c8f9e64f5a (diff)
downloadc-chess-8cdd60bca12974870f7ec8ea82c175b181cd1132.tar.gz
feat: add quiscence search
-rw-r--r--src/main.c41
1 files changed, 39 insertions, 2 deletions
diff --git a/src/main.c b/src/main.c
index b950144..15c7618 100644
--- a/src/main.c
+++ b/src/main.c
@@ -494,7 +494,7 @@ int generate_pseudolegal_moves(Move* moves, int* board, int color, int only_capt
while(ray_move && *ray_move) {
for (int destination = origin + *ray_move; !(destination & 0x88); destination += *ray_move) {
- if ((board[destination] == EMPTY && !only_captures) || board[destination] % 2 != color) {
+ if ((board[destination] == EMPTY && !only_captures) || (board[destination] != EMPTY && board[destination] % 2 != color)) {
if (moves) moves[moves_count] = (Move){ origin, destination, 0 };
moves_count++;
}
@@ -672,6 +672,43 @@ int order_moves(Move* moves, int moves_count, int* board, long hash, Transpositi
return cache_hits; // Only needed for display
}
+int quiscence(int* board, int color, int alpha, int beta, int* metrics, long hash, Transposition* transposition_table, long* zobrist_seed, clock_t deadline) {
+ int is_maximizer = (color == 0);
+
+ int evaluation = static_evaluate(board, 0, 0);
+ if(evaluation >= beta ) return beta;
+ if(alpha < evaluation) alpha = evaluation;
+
+ Move pseudolegal_moves[MAX_AVAILABLE_MOVES];
+ int pseudolegal_moves_count = generate_pseudolegal_moves(pseudolegal_moves, board, color, 1);
+ int cache_hits = order_moves(pseudolegal_moves, pseudolegal_moves_count, board, hash, transposition_table, zobrist_seed);
+
+ for (int i = 0; i < pseudolegal_moves_count; i++) {
+ *metrics += 1;
+ Move move = pseudolegal_moves[i];
+
+ long move_hash = hash_after_move(move, board, zobrist_seed, hash);
+ int captured_piece = make_move(move, board);
+ int score = quiscence(board, 1 - color, alpha, beta, metrics, move_hash, transposition_table, zobrist_seed, deadline);
+ unmake_move(move, captured_piece, board);
+
+ if (is_maximizer) {
+ if (score > evaluation) evaluation = score;
+ alpha = MAX(evaluation, alpha);
+ } else {
+ if (score < evaluation) evaluation = score;
+ beta = MIN(evaluation, beta);
+ }
+
+ if (beta <= alpha) break;
+
+ // We ran out of time
+ if (clock() > deadline) break;
+ }
+
+ return evaluation;
+}
+
/*
* Alpha-beta pruning
* Value here is white material advantage over black
@@ -699,7 +736,7 @@ Move minimax_search(int* board, int color, int depth, int alpha, int beta, int*
move.value = depth > 0
? minimax_search(board, 1 - color, depth - 1, alpha, beta, metrics, move_hash, transposition_table, zobrist_seed, deadline).value
- : static_evaluate(board, pseudolegal_moves_count, color);
+ : quiscence(board, 1 - color, alpha, beta, metrics, hash, transposition_table, zobrist_seed, deadline);
if (transposition_table[move_hash].depth < depth) {
if (transposition_table[move_hash].depth == -1) TRANSPOSITION_TABLE_CARDINALITY++;