diff options
author | eug-vs <eugene@eug-vs.xyz> | 2022-08-31 02:38:44 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2022-08-31 02:38:44 +0300 |
commit | d26b99ec280e5e0971c4abc9cfc85445f40fc4d5 (patch) | |
tree | 9090584c58ffe750a0a3ec76a481a1d8b53a05f8 | |
parent | cf1464e003c8f8ad3ad11b3e064db9906e6613bb (diff) | |
download | c-chess-d26b99ec280e5e0971c4abc9cfc85445f40fc4d5.tar.gz |
feat: implement initial zobrist hashing
-rw-r--r-- | src/config.h | 1 | ||||
-rw-r--r-- | src/main.c | 28 |
2 files changed, 29 insertions, 0 deletions
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 @@ -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(); |