summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-09-05 01:05:52 +0300
committereug-vs <eugene@eug-vs.xyz>2022-09-05 01:08:24 +0300
commit6351ab0f49ba8f1d4c103baa849b2be8c1826455 (patch)
tree0462868e64e6fe4861d8212aca58b24958dda9f5
parent0dc968bab085df048f46d5b86c616c2749842573 (diff)
downloadc-chess-6351ab0f49ba8f1d4c103baa849b2be8c1826455.tar.gz
fix: ensure hash always maps to positive index
-rw-r--r--src/main.c26
1 files changed, 16 insertions, 10 deletions
diff --git a/src/main.c b/src/main.c
index 360a4e8..6abea38 100644
--- a/src/main.c
+++ b/src/main.c
@@ -20,6 +20,12 @@ void generate_zobrist_seed(long* seed) {
}
}
+long crop_hash(long hash) {
+ long modulo = hash % TRANSPOSITION_TABLE_SIZE;
+ if (modulo < 0) modulo += TRANSPOSITION_TABLE_SIZE;
+ return modulo;
+}
+
int zobrist_hash(long* seed, int* board, int color_to_move) {
long hash = 1;
@@ -36,7 +42,7 @@ int zobrist_hash(long* seed, int* board, int color_to_move) {
hash ^= seed[zobrist_index];
}
}
- return hash;
+ return crop_hash(hash);
}
long hash_after_move(Move move, int* board, long* zobrist_seed, long hash) {
@@ -78,7 +84,7 @@ long hash_after_move(Move move, int* board, long* zobrist_seed, long hash) {
}
}
- return hash;
+ return crop_hash(hash);
}
@@ -563,8 +569,8 @@ int order_moves(Move* moves, int moves_count, int* board, long hash, Transpositi
int color = board[moves[i].origin] % 2;
// Lookup move in transpoition table
long move_hash = hash_after_move(moves[i], board, zobrist_seed, hash);
- if (transposition_table[move_hash % TRANSPOSITION_TABLE_SIZE].depth > -1) {
- moves[i].value = transposition_table[move_hash % TRANSPOSITION_TABLE_SIZE].value * (color == WHITE ? 1 : -1);
+ if (transposition_table[move_hash].depth > -1) {
+ moves[i].value = transposition_table[move_hash].value * (color == WHITE ? 1 : -1);
cache_hits++;
} else {
int origin_value = get_piece_raw_value(board[moves[i].origin]);
@@ -603,16 +609,16 @@ Move minimax_search(int* board, int color, int depth, int alpha, int beta, int*
long move_hash = hash_after_move(move, board, zobrist_seed, hash);
int captured_piece = make_move(move, board);
- int local_depth = MAX_DEPTH - depth;
+ int relative_depth = MAX_DEPTH - depth;
- move.value = local_depth > 0
+ move.value = relative_depth > 0
? minimax_search(board, 1 - color, depth + 1, alpha, beta, metrics, hash, transposition_table, zobrist_seed).value
: evaluate_position(board, available_moves_count, color);
- if (transposition_table[move_hash % TRANSPOSITION_TABLE_SIZE].depth < local_depth) {
- if (transposition_table[move_hash % TRANSPOSITION_TABLE_SIZE].depth == -1) TRANSPOSITION_TABLE_CARDINALITY++;
- transposition_table[move_hash % TRANSPOSITION_TABLE_SIZE].value = move.value;
- transposition_table[move_hash % TRANSPOSITION_TABLE_SIZE].depth = local_depth;
+ if (transposition_table[move_hash].depth < relative_depth) {
+ if (transposition_table[move_hash].depth == -1) TRANSPOSITION_TABLE_CARDINALITY++;
+ transposition_table[move_hash].value = move.value;
+ transposition_table[move_hash].depth = relative_depth;
}
unmake_move(move, captured_piece, board);