fixed bugs in hashKey generation + added incremental updates to it
This commit is contained in:
1 parent
436f2d7164
commit
2e984643c0
5 files changed
+58
-16
No files matched your search
+31
-4
@@ -1,6 +1,7 @@
|
||||
#include "board.h"
|
||||
#include "movegen.h"
|
||||
#include <iostream>
|
||||
#include "tt.h"
|
||||
|
||||
void Board::Init() {
|
||||
SetByFen(StartingFen);
|
||||
@@ -19,6 +20,8 @@ void Board::Reset() {
|
||||
|
||||
moveList = std::array<Move, 218>();
|
||||
currentMoveIndex = 0;
|
||||
|
||||
hashKey = 0ULL;
|
||||
}
|
||||
|
||||
void Board::SetByFen(const char* fen) {
|
||||
@@ -187,6 +190,7 @@ void Board::SetByFen(const char* fen) {
|
||||
|
||||
occupied = (colors[White] | colors[Black]);
|
||||
GenAttackMaps(*this);
|
||||
hashKey = GetHashKey(*this);
|
||||
}
|
||||
|
||||
void Board::PrintBoard() {
|
||||
@@ -236,7 +240,9 @@ void Board::PrintBoard() {
|
||||
if (castlingRights & whiteQueenRight) std::cout << "Q"; else std::cout << "-";
|
||||
if (castlingRights & blackKingRight) std::cout << "k"; else std::cout << "-";
|
||||
if (castlingRights & blackQueenRight) std::cout << "q"; else std::cout << "-";
|
||||
std::cout << '\n';
|
||||
std::cout << "\n\n";
|
||||
|
||||
std::cout << " Hashkey: " << std::hex << hashKey << '\n';
|
||||
}
|
||||
|
||||
void Board::AddMove(Move move) {
|
||||
@@ -249,11 +255,9 @@ void Board::ResetMoves() {
|
||||
}
|
||||
|
||||
void Board::ListMoves() {
|
||||
int count = 1;
|
||||
for (int i = 0; i < currentMoveIndex; i++) {
|
||||
std::cout << count << ". ";
|
||||
std::cout << i+1 << ". ";
|
||||
moveList[i].PrintMove();
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -287,17 +291,23 @@ void Board::SetPiece(int piece, int square, bool color) {
|
||||
pieces[piece].SetBit(square);
|
||||
colors[color].SetBit(square);
|
||||
occupied.SetBit(square);
|
||||
|
||||
hashKey ^= zKeys[color][piece][square];
|
||||
}
|
||||
|
||||
void Board::RemovePiece(int piece, int square, bool color) {
|
||||
pieces[piece].PopBit(square);
|
||||
colors[color].PopBit(square);
|
||||
occupied.PopBit(square);
|
||||
|
||||
hashKey ^= zKeys[color][piece][square];
|
||||
}
|
||||
|
||||
// Updates castling rights
|
||||
static void UpdateCastlingRights(Board &board, int square, int type, int color) {
|
||||
if (type == Rook) {
|
||||
// Removing old rights
|
||||
board.hashKey ^= zCastle[board.castlingRights];
|
||||
int queenSideRook = color ? a8 : a1;
|
||||
int kingSideRook = color ? h8 : h1;
|
||||
|
||||
@@ -306,9 +316,16 @@ static void UpdateCastlingRights(Board &board, int square, int type, int color)
|
||||
} else if (square == kingSideRook) {
|
||||
board.castlingRights &= color ? ~blackKingRight : ~whiteKingRight;
|
||||
}
|
||||
|
||||
board.hashKey ^= zCastle[board.castlingRights];
|
||||
} else if (type == King) {
|
||||
// Removing old rights
|
||||
board.hashKey ^= zCastle[board.castlingRights];
|
||||
|
||||
board.castlingRights &= color ? ~blackKingRight : ~whiteKingRight;
|
||||
board.castlingRights &= color ? ~blackQueenRight : ~whiteQueenRight;
|
||||
|
||||
board.hashKey ^= zCastle[board.castlingRights];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -414,6 +431,16 @@ void Board::MakeMove(Move move) {
|
||||
halfMoves++;
|
||||
if (halfMoves % 2 == 0) fullMoves++;
|
||||
sideToMove = !attackerColor;
|
||||
hashKey ^= zSide;
|
||||
|
||||
if (enPassantTarget != -1) {
|
||||
hashKey ^= zEnPassant[enPassantTarget % 8];
|
||||
}
|
||||
|
||||
if (newEpTarget != -1) {
|
||||
hashKey ^= zEnPassant[newEpTarget % 8];
|
||||
}
|
||||
|
||||
enPassantTarget = newEpTarget;
|
||||
|
||||
GenAttackMaps(*this);
|
||||
|
||||
+3
-1
@@ -24,7 +24,9 @@ public:
|
||||
int fullMoves = 0;
|
||||
|
||||
uint8_t castlingRights = 0;
|
||||
int enPassantTarget;
|
||||
int enPassantTarget = -1;
|
||||
|
||||
U64 hashKey = 0ULL;
|
||||
|
||||
void Init();
|
||||
void Reset();
|
||||
|
||||
@@ -12,6 +12,5 @@ int main() {
|
||||
|
||||
UCILoop(board);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
+13
-10
@@ -1,20 +1,23 @@
|
||||
#include "tt.h"
|
||||
|
||||
static inline U64 initialSeed = 0x60919C48E57863B9;
|
||||
static U64 initialSeed = 0x60919C48E57863B9;
|
||||
|
||||
// 2[colors] * 6[pieces] * 64[squares]
|
||||
static U64 zKeys[2][6][64];
|
||||
U64 zKeys[2][6][64];
|
||||
|
||||
// There are 8 files
|
||||
static U64 zEnPassant[8];
|
||||
U64 zEnPassant[8];
|
||||
|
||||
static U64 zCastle[16];
|
||||
static U64 zSide;
|
||||
// 16 possible castling right variations
|
||||
U64 zCastle[16];
|
||||
|
||||
U64 zSide;
|
||||
|
||||
// PRNG using xorshift
|
||||
static U64 RandomNum() {
|
||||
initialSeed ^= initialSeed << 13;
|
||||
initialSeed ^= initialSeed >> 17;
|
||||
initialSeed ^= initialSeed << 5;
|
||||
initialSeed ^= initialSeed >> 7;
|
||||
initialSeed ^= initialSeed << 17;
|
||||
return initialSeed;
|
||||
}
|
||||
|
||||
@@ -43,13 +46,13 @@ void InitZobrist() {
|
||||
}
|
||||
|
||||
U64 GetHashKey(Board &board) {
|
||||
U64 key;
|
||||
U64 key = 0ULL;
|
||||
|
||||
// XORing pieces
|
||||
for (int color = White; color <= Black; color++) {
|
||||
for (int piece = Pawn; piece <= King; piece++) {
|
||||
Bitboard pieceSet = board.pieces[piece];
|
||||
|
||||
Bitboard pieceSet = board.pieces[piece] & board.colors[color];
|
||||
|
||||
while (pieceSet) {
|
||||
int square = pieceSet.getLS1BIndex();
|
||||
|
||||
|
||||
+11
@@ -1,5 +1,16 @@
|
||||
#pragma once
|
||||
#include "board.h"
|
||||
|
||||
// 2[colors] * 6[pieces] * 64[squares]
|
||||
extern U64 zKeys[2][6][64];
|
||||
|
||||
// There are 8 files
|
||||
extern U64 zEnPassant[8];
|
||||
|
||||
// 16 possible castling right variations
|
||||
extern U64 zCastle[16];
|
||||
|
||||
extern U64 zSide;
|
||||
|
||||
void InitZobrist();
|
||||
U64 GetHashKey(Board &board);
|
||||
Reference in new issue
Block a user