From 436f2d7164867abcccad2cac4308fe5f41b7248a Mon Sep 17 00:00:00 2001 From: rektdie Date: Sat, 29 Jun 2024 16:56:43 +0200 Subject: [PATCH] added GetHashKey function to hash a given board --- source/tt.cpp | 34 ++++++++++++++++++++++++++++++++-- source/tt.h | 2 ++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/source/tt.cpp b/source/tt.cpp index 489cc1b..41cc1c3 100644 --- a/source/tt.cpp +++ b/source/tt.cpp @@ -1,6 +1,4 @@ #include "tt.h" -#include "types.h" -#include static inline U64 initialSeed = 0x60919C48E57863B9; @@ -43,3 +41,35 @@ void InitZobrist() { // init side key zSide = RandomNum(); } + +U64 GetHashKey(Board &board) { + U64 key; + + // XORing pieces + for (int color = White; color <= Black; color++) { + for (int piece = Pawn; piece <= King; piece++) { + Bitboard pieceSet = board.pieces[piece]; + + while (pieceSet) { + int square = pieceSet.getLS1BIndex(); + + key ^= zKeys[color][piece][square]; + + pieceSet.PopBit(square); + } + } + } + + // XORing en passant + if (board.enPassantTarget != -1) { + key ^= zEnPassant[board.enPassantTarget % 8]; + } + + // XORing castling rights + key ^= zCastle[board.castlingRights]; + + // XORing side to move + if (board.sideToMove) key ^= zSide; + + return key; +} diff --git a/source/tt.h b/source/tt.h index 56c31c5..e7f885a 100644 --- a/source/tt.h +++ b/source/tt.h @@ -1,3 +1,5 @@ #pragma once +#include "board.h" void InitZobrist(); +U64 GetHashKey(Board &board);