From d26b99ec280e5e0971c4abc9cfc85445f40fc4d5 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Wed, 31 Aug 2022 02:38:44 +0300 Subject: feat: implement initial zobrist hashing --- src/config.h | 1 + src/main.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/config.h b/src/config.h index 9273f0e..4031d91 100644 --- a/src/config.h +++ b/src/config.h @@ -4,3 +4,4 @@ #define MAX_AVAILABLE_MOVES 64 * 64 #define INFINITY 1000000 #define PLAYER WHITE +#define MAX_ZOBRIST_SEEDS 1500 diff --git a/src/main.c b/src/main.c index 5a66201..f1eb6a3 100644 --- a/src/main.c +++ b/src/main.c @@ -12,6 +12,28 @@ #define MAX(x, y) (((x) > (y)) ? (x) : (y)) #define MIN(x, y) (((x) < (y)) ? (x) : (y)) +void generate_zobrist_seed(int* seed) { + for (int i = 0; i < 781; i++) { + seed[i] = rand(); + } +} + +int zobrist_hash(int* seed, int* board, int color_to_move) { + int result = seed[color_to_move]; + 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]; + } + } + } + return result; +} + void print_board(int board[128]) { for (int rank = 7; rank >= 0; rank--) { @@ -500,7 +522,13 @@ int main() { Move move; int color = WHITE; + int zobrist_seed[MAX_ZOBRIST_SEEDS]; + generate_zobrist_seed(zobrist_seed); + while (1) { + int 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]); move = input_move(); -- cgit v1.2.3