diff options
author | eug-vs <eugene@eug-vs.xyz> | 2022-08-31 04:25:46 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2022-08-31 04:25:46 +0300 |
commit | 1a37a1b9e79b4a4de8df12cd51bf1604ea728cf3 (patch) | |
tree | 8b36d57e2e5fb66637f1414ea860f255614bee5a | |
parent | 698d67d148afff5c639f31dd0a0b9341861e620d (diff) | |
download | c-chess-1a37a1b9e79b4a4de8df12cd51bf1604ea728cf3.tar.gz |
fix: return empty squares when computing hash
-rw-r--r-- | src/main.c | 15 |
1 files changed, 8 insertions, 7 deletions
@@ -13,16 +13,16 @@ #define MIN(x, y) (((x) < (y)) ? (x) : (y)) void generate_zobrist_seed(int* seed) { - for (int i = 0; i < 781; i++) { + for (int i = 0; i < MAX_ZOBRIST_SEEDS; i++) { seed[i] = rand(); } } int zobrist_hash(int* seed, int* board, int color_to_move) { - int result = 1; + int hash = 1; // Last feature means white to move - if (color_to_move == WHITE) result ^= seed[MAX_ZOBRIST_SEEDS - 1]; + if (color_to_move == WHITE) hash ^= seed[MAX_ZOBRIST_SEEDS - 1]; for (int rank = 7; rank >= 0; rank--) { for (int file = 0; file < 8; file++) { @@ -31,10 +31,10 @@ int zobrist_hash(int* seed, int* board, int color_to_move) { // Unique index of this piece on this position int zobrist_index = piece * 128 + position; - result ^= seed[zobrist_index]; + hash ^= seed[zobrist_index]; } } - return result; + return hash; } int apply_move_zobrist(Move move, int* board, int* zobrist_seed, int hash) { @@ -42,6 +42,7 @@ int apply_move_zobrist(Move move, int* board, int* zobrist_seed, int hash) { int target_piece = board[move.destination]; hash ^= zobrist_seed[MAX_ZOBRIST_SEEDS - 1]; // Flip color + hash ^= zobrist_seed[EMPTY * 128 + move.origin]; // Add empty to origin 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 @@ -511,7 +512,7 @@ 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.75) printf("More than 75 percent cache hits (%i)\n", cache_hits); + if (depth == 0 && cache_hits && (double)cache_hits / (double)available_moves_count > 0.50) printf("More than 50 percent cache hits (%i)\n", cache_hits); for (int i = 0; i < available_moves_count; i++) { *metrics += 1; @@ -561,7 +562,7 @@ int main() { print_board(board); while (1) { - printf("Expected zobrist hash: %i (found %i)\n", hash, zobrist_hash(zobrist_seed, board, color)); + printf("Zobrist hash: %i\n", hash); if (color == PLAYER) { printf("Enter a move for %s:\n", COLORS[color]); |