Chess Engine
C++ chess engine with movegen, bitboards, and Arduino-friendly docs
Loading...
Searching...
No Matches
hash_keys.cpp
Go to the documentation of this file.
1#include <cstdio>
2#include "../defs.h"
3#include "setup.h"
4
6 U64 finalKey = 0ULL;
7
8 // Pieces
9 for (int sq = 0; sq < BRD_SQ_NUM; ++sq) {
10 int piece = board->pieces[sq];
11 if (piece != NO_SQ && piece != EMPTY && piece != OFFBOARD) {
12 ASSERT(piece >= wP && piece <= bK);
13 finalKey ^= zobristPieceKeys[piece][sq];
14 }
15 }
16
17 // Side to move
18 if (board->side == WHITE) {
19 finalKey ^= zobristSideKey;
20 }
21
22 // En passant square
23 if (board->enPas != NO_SQ) {
24 ASSERT(board->enPas >= 0 && board->enPas < BRD_SQ_NUM);
25 ASSERT(SqOnBoard(board->enPas));
26 ASSERT(rankIndex120[board->enPas] == RANK_3 || rankIndex120[board->enPas] == RANK_6);
27 finalKey ^= zobristPieceKeys[EMPTY][board->enPas];
28 }
29
30 // Castling rights
31 ASSERT(board->castlePerm >= 0 && board->castlePerm <= 15);
32 finalKey ^= zobristCastleKeys[board->castlePerm];
33
34 return finalKey;
35}
unsigned long long U64
Definition defs.h:26
@ EMPTY
Definition defs.h:40
@ bK
Definition defs.h:40
@ wP
Definition defs.h:40
@ WHITE
Definition defs.h:44
#define BRD_SQ_NUM
Definition defs.h:29
@ RANK_6
Definition defs.h:42
@ RANK_3
Definition defs.h:42
#define ASSERT(n)
Definition defs.h:14
@ NO_SQ
Definition defs.h:54
@ OFFBOARD
Definition defs.h:54
U64 generatePositionKeys(const S_BOARD *board)
Generates a Zobrist hash key for the given board position.
Definition hash_keys.cpp:5
U64 zobristSideKey
Zobrist hashing key for side to move.
Definition setup.cpp:23
int rankIndex120[BRD_SQ_NUM]
Definition setup.cpp:27
U64 zobristCastleKeys[16]
Zobrist hashing keys for castling rights.
Definition setup.cpp:24
U64 zobristPieceKeys[13][120]
Zobrist hashing keys for each piece on each square.
Definition setup.cpp:22
int pieces[BRD_SQ_NUM]
Definition defs.h:102
int castlePerm
Definition defs.h:114
int enPas
Definition defs.h:108
int side
Definition defs.h:107
int SqOnBoard(const int sq)
Checks if a given square index refers to a valid on-board square.
Definition validate.cpp:29