diff options
| author | Aargh Rai <aargh.rai+git@gmail.com> | 2026-07-18 18:45:15 +0530 |
|---|---|---|
| committer | Aargh Rai <aargh.rai+git@gmail.com> | 2026-07-18 18:45:15 +0530 |
| commit | c9d9bddf3df97ef7987c9824747ec1602522b542 (patch) | |
| tree | 449acf383fb10ccf2210d9af96515275b52cf1a9 /src/engine/transposition_table.c | |
| parent | 9395e857db95481f6e2494d3a841632be0ff97c8 (diff) | |
zobrist hashing
Diffstat (limited to 'src/engine/transposition_table.c')
| -rw-r--r-- | src/engine/transposition_table.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/engine/transposition_table.c b/src/engine/transposition_table.c new file mode 100644 index 0000000..1afbc81 --- /dev/null +++ b/src/engine/transposition_table.c @@ -0,0 +1,44 @@ +#include "engine/transposition_table.h" + +void transposition_table_init() { + for (int i = 0; i < PIECE_TYPE_COUNT; i++) { + for (int j = 0; j < 64; j++) { + zobrist_keys.piece[i][j] = random_u64(); + } + } + for (int i = 0; i < CASTLE_COUNT; i++) { + zobrist_keys.castle[i] = random_u64(); + } + for (int i = 0; i < TURN_COUNT; i++) { + zobrist_keys.turn[i] = random_u64(); + } + for (int i = 0; i < 64; i++) { + zobrist_keys.en_passant[i] = random_u64(); + } +} + +u64 zobrist_hash(position_t position) { + u64 output = 0; + for (int i = 0; i < 64; i++) { + int piece_type = find_piece_on_square(&position, i); + output ^= zobrist_keys.piece[piece_type][i]; + } + + if (position.castling & WHITE_SHORT_CASTLE) { + output ^= zobrist_keys.castle[0]; + } + if (position.castling & WHITE_LONG_CASTLE) { + output ^= zobrist_keys.castle[1]; + } + if (position.castling & BLACK_SHORT_CASTLE) { + output ^= zobrist_keys.castle[2]; + } + if (position.castling & BLACK_LONG_CASTLE) { + output ^= zobrist_keys.castle[3]; + } + + output ^= zobrist_keys.turn[position.turn]; + output ^= zobrist_keys.en_passant[position.passantable_file]; + + return output; +} |
