summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-08-31 04:08:36 +0300
committereug-vs <eugene@eug-vs.xyz>2022-08-31 04:08:36 +0300
commit698d67d148afff5c639f31dd0a0b9341861e620d (patch)
treec54dafa9d0938f89714a632bcd43841d87316d8e
parent3026d9260cc0d7a1a88c4cdd0cd389e442e705cd (diff)
downloadc-chess-698d67d148afff5c639f31dd0a0b9341861e620d.tar.gz
fix: compute zobrist hash before applying move
-rw-r--r--src/main.c26
1 files changed, 14 insertions, 12 deletions
diff --git a/src/main.c b/src/main.c
index 74d83ce..7985d41 100644
--- a/src/main.c
+++ b/src/main.c
@@ -19,16 +19,19 @@ void generate_zobrist_seed(int* seed) {
}
int zobrist_hash(int* seed, int* board, int color_to_move) {
- int result = seed[color_to_move];
+ int result = 1;
+
+ // Last feature means white to move
+ if (color_to_move == WHITE) result ^= seed[MAX_ZOBRIST_SEEDS - 1];
+
for (int rank = 7; rank >= 0; rank--) {
for (int file = 0; file < 8; file++) {
int position = rank * 16 + file;
int piece = board[position];
- if (piece != EMPTY) {
- // Unique index of this piece on this position
- int zobrist_index = (piece * 128) + position;
- result ^= seed[zobrist_index];
- }
+
+ // Unique index of this piece on this position
+ int zobrist_index = piece * 128 + position;
+ result ^= seed[zobrist_index];
}
}
return result;
@@ -38,11 +41,10 @@ int apply_move_zobrist(Move move, int* board, int* zobrist_seed, int hash) {
int piece = board[move.origin];
int target_piece = board[move.destination];
+ hash ^= zobrist_seed[MAX_ZOBRIST_SEEDS - 1]; // Flip color
hash ^= zobrist_seed[piece * 128 + move.origin]; // Remove piece from origin
hash ^= zobrist_seed[piece * 128 + move.destination]; // Add piece to destination
hash ^= zobrist_seed[target_piece * 128 + move.destination]; // Remove target piece from destination
- hash ^= zobrist_seed[1 - piece % 2]; // It's now opponent's turn
- hash ^= zobrist_seed[piece % 2]; // Not a current color's turn
return hash;
}
@@ -509,12 +511,13 @@ Move find_best_move(int* board, int color, int depth, int alpha, int beta, int*
Move available_moves[MAX_AVAILABLE_MOVES];
int available_moves_count = list_available_moves(available_moves, board, color);
int cache_hits = sort_moves(available_moves, available_moves_count, board, hash, transposition_table, zobrist_seed);
- if (cache_hits && (double)cache_hits / (double)available_moves_count > 0.05) printf("More than 5 percent cache hits (%i)\n", cache_hits);
+ if (cache_hits && (double)cache_hits / (double)available_moves_count > 0.75) printf("More than 75 percent cache hits (%i)\n", cache_hits);
for (int i = 0; i < available_moves_count; i++) {
*metrics += 1;
Move move = available_moves[i];
+ int move_hash = apply_move_zobrist(move, board, zobrist_seed, hash);
int captured_piece = apply_move(move, board);
move.value = depth < MAX_DEPTH
@@ -522,7 +525,6 @@ Move find_best_move(int* board, int color, int depth, int alpha, int beta, int*
: compute_score(board, available_moves_count, color);
if (depth < MAX_DEPTH) {
- int move_hash = apply_move_zobrist(move, board, zobrist_seed, hash);
transposition_table[move_hash % TRANSPOSITION_TABLE_SIZE] = move.value;
}
@@ -559,7 +561,7 @@ int main() {
print_board(board);
while (1) {
- printf("Zobrist hash: %i\n", hash);
+ printf("Expected zobrist hash: %i (found %i)\n", hash, zobrist_hash(zobrist_seed, board, color));
if (color == PLAYER) {
printf("Enter a move for %s:\n", COLORS[color]);
@@ -586,8 +588,8 @@ int main() {
printf("Checkmate, %s is victorious!\n", COLORS[1 - color]);
break;
} else {
- apply_move(move, board);
hash = apply_move_zobrist(move, board, zobrist_seed, hash);
+ apply_move(move, board);
print_board(board);