From 0ffc2c77deb781cd07ed011bcc72a974baa68df2 Mon Sep 17 00:00:00 2001 From: rektdie Date: Thu, 20 Mar 2025 20:39:12 +0100 Subject: [PATCH] bigger refactor in several files --- source/board.cpp | 32 ++++++++++---------- source/main.cpp | 4 +-- source/tt.cpp | 79 +----------------------------------------------- source/tt.h | 14 --------- source/uci.cpp | 4 +-- source/utils.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++ source/utils.h | 19 ++++++++++++ 7 files changed, 111 insertions(+), 112 deletions(-) diff --git a/source/board.cpp b/source/board.cpp index 0d6da91..21540d4 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -29,8 +29,8 @@ void Board::SetByFen(std::string_view fen) { // Starting from top left int currSquare = a8; - std::vector tokens = split(fen, ' '); - std::vector pieceTokens = split(tokens[0], '/'); + std::vector tokens = UTILS::split(fen, ' '); + std::vector pieceTokens = UTILS::split(tokens[0], '/'); constexpr std::string_view pieceTypes = "pnbrqk"; @@ -61,10 +61,10 @@ void Board::SetByFen(std::string_view fen) { else if (piece == 'q') castlingRights |= blackQueenRight; } - if (tokens[3] != "-") enPassantTarget = parseSquare(tokens[3]); + if (tokens[3] != "-") enPassantTarget = UTILS::parseSquare(tokens[3]); occupied = colors[White] | colors[Black]; - hashKey = GetHashKey(*this); + hashKey = UTILS::GetHashKey(*this); MOVEGEN::GenerateMoves(*this); MOVEGEN::GenAttackMaps(*this); } @@ -169,7 +169,7 @@ void Board::SetPiece(int piece, int square, bool color) { colors[color].SetBit(square); occupied.SetBit(square); - hashKey ^= zKeys[color][piece][square]; + hashKey ^= UTILS::zKeys[color][piece][square]; } void Board::RemovePiece(int piece, int square, bool color) { @@ -177,14 +177,14 @@ void Board::RemovePiece(int piece, int square, bool color) { colors[color].PopBit(square); occupied.PopBit(square); - hashKey ^= zKeys[color][piece][square]; + hashKey ^= UTILS::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]; + board.hashKey ^= UTILS::zCastle[board.castlingRights]; int queenSideRook = color ? a8 : a1; int kingSideRook = color ? h8 : h1; @@ -194,15 +194,15 @@ static void UpdateCastlingRights(Board &board, int square, int type, int color) board.castlingRights &= color ? ~blackKingRight : ~whiteKingRight; } - board.hashKey ^= zCastle[board.castlingRights]; + board.hashKey ^= UTILS::zCastle[board.castlingRights]; } else if (type == King) { // Removing old rights - board.hashKey ^= zCastle[board.castlingRights]; + board.hashKey ^= UTILS::zCastle[board.castlingRights]; board.castlingRights &= color ? ~blackKingRight : ~whiteKingRight; board.castlingRights &= color ? ~blackQueenRight : ~whiteQueenRight; - board.hashKey ^= zCastle[board.castlingRights]; + board.hashKey ^= UTILS::zCastle[board.castlingRights]; } } @@ -224,14 +224,14 @@ void Board::MakeMove(Move move) { int newEpTarget = noEPTarget; sideToMove = !sideToMove; - hashKey ^= zSide; + hashKey ^= UTILS::zSide; if (enPassantTarget != noEPTarget) { - hashKey ^= zEnPassant[enPassantTarget % 8]; + hashKey ^= UTILS::zEnPassant[enPassantTarget % 8]; } if (newEpTarget != noEPTarget) { - hashKey ^= zEnPassant[newEpTarget % 8]; + hashKey ^= UTILS::zEnPassant[newEpTarget % 8]; } enPassantTarget = newEpTarget; @@ -328,14 +328,14 @@ void Board::MakeMove(Move move) { UpdateCastlingRights(*this, move.MoveFrom(), attackerPiece, attackerColor); sideToMove = !attackerColor; - hashKey ^= zSide; + hashKey ^= UTILS::zSide; if (enPassantTarget != noEPTarget) { - hashKey ^= zEnPassant[enPassantTarget % 8]; + hashKey ^= UTILS::zEnPassant[enPassantTarget % 8]; } if (newEpTarget != noEPTarget) { - hashKey ^= zEnPassant[newEpTarget % 8]; + hashKey ^= UTILS::zEnPassant[newEpTarget % 8]; } enPassantTarget = newEpTarget; diff --git a/source/main.cpp b/source/main.cpp index 4578caf..93eb880 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,13 +1,13 @@ #include "board.h" #include "movegen.h" #include "uci.h" -#include "tt.h" +#include "utils.h" #include "benchmark.h" int main(int argc, char* argv[]) { MOVEGEN::initLeaperAttacks(); MOVEGEN::initSliderAttacks(); - InitZobrist(); + UTILS::InitZobrist(); Board board; diff --git a/source/tt.cpp b/source/tt.cpp index c92ceae..e65b2b7 100644 --- a/source/tt.cpp +++ b/source/tt.cpp @@ -1,82 +1,5 @@ #include "tt.h" -static U64 initialSeed = 0x60919C48E57863B9; - -// 2[colors] * 6[pieces] * 64[squares] -U64 zKeys[2][6][64]; - -// There are 8 files -U64 zEnPassant[8]; - -// 16 possible castling right variations -U64 zCastle[16]; - -U64 zSide; - -// PRNG using xorshift -static U64 RandomNum() { - initialSeed ^= initialSeed << 13; - initialSeed ^= initialSeed >> 7; - initialSeed ^= initialSeed << 17; - return initialSeed; -} - -void InitZobrist() { - // init piece square keys - for (int i = White; i <= Black; i++) { - for (int j = Pawn; j <= King; j++) { - for (int k = a1; k <= h8; k++) { - zKeys[i][j][k] = RandomNum(); - } - } - } - - // init en passant file keys - for (int i = 0; i < 8; i++) { - zEnPassant[i] = RandomNum(); - } - - // init castling rights keys - for (int i = 0; i < 16; i++) { - zCastle[i] = RandomNum(); - } - - // init side key - zSide = RandomNum(); -} - -U64 GetHashKey(Board &board) { - U64 key = 0ULL; - - // XORing pieces - for (int color = White; color <= Black; color++) { - for (int piece = Pawn; piece <= King; piece++) { - Bitboard pieceSet = board.pieces[piece] & board.colors[color]; - - 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; -} - SearchResults TTable::ReadEntry(U64 &hashKey, int depth, int alpha, int beta) { TTEntry *current = &table[hashKey % hashSize]; @@ -104,4 +27,4 @@ void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move b current->score = score; current->depth = depth; current->bestMove = bestMove; -} +} \ No newline at end of file diff --git a/source/tt.h b/source/tt.h index e167cf6..1b973ab 100644 --- a/source/tt.h +++ b/source/tt.h @@ -3,20 +3,6 @@ #include "search.h" #include -// 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); - class TTEntry { public: U64 hashKey; diff --git a/source/uci.cpp b/source/uci.cpp index aeef780..e9a735c 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -29,10 +29,10 @@ void ParsePosition(Board &board, std::string_view command) { } if (movesIndex != std::string::npos) { - std::vector moves = split(command.substr(movesIndex + 6, command.length() - (movesIndex + 5)), ' '); + std::vector moves = UTILS::split(command.substr(movesIndex + 6, command.length() - (movesIndex + 5)), ' '); for (std::string_view move : moves) { - board.MakeMove(parseMove(board, move)); + board.MakeMove(UTILS::parseMove(board, move)); } } diff --git a/source/utils.cpp b/source/utils.cpp index 6396104..12f8bf6 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -1,6 +1,76 @@ #include "utils.h" #include "movegen.h" +namespace UTILS { + +// Zobrist // + +U64 initialSeed = 0x60919C48E57863B9; + +// PRNG using xorshift +static U64 RandomNum() { + initialSeed ^= initialSeed << 13; + initialSeed ^= initialSeed >> 7; + initialSeed ^= initialSeed << 17; + return initialSeed; +} + +void InitZobrist() { + // init piece square keys + for (int i = White; i <= Black; i++) { + for (int j = Pawn; j <= King; j++) { + for (int k = a1; k <= h8; k++) { + zKeys[i][j][k] = RandomNum(); + } + } + } + + // init en passant file keys + for (int i = 0; i < 8; i++) { + zEnPassant[i] = RandomNum(); + } + + // init castling rights keys + for (int i = 0; i < 16; i++) { + zCastle[i] = RandomNum(); + } + + // init side key + zSide = RandomNum(); +} + +U64 GetHashKey(Board &board) { + U64 key = 0ULL; + + // XORing pieces + for (int color = White; color <= Black; color++) { + for (int piece = Pawn; piece <= King; piece++) { + Bitboard pieceSet = board.pieces[piece] & board.colors[color]; + + 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; +} + std::vector split(std::string_view str, char delim) { std::vector results; @@ -76,3 +146,4 @@ Move parseMove(Board &board, std::string_view str) { return Move(from, to, flag); } +} diff --git a/source/utils.h b/source/utils.h index 6c23585..0156f55 100644 --- a/source/utils.h +++ b/source/utils.h @@ -6,8 +6,27 @@ #include "move.h" #include "board.h" +namespace UTILS { + +// Zobrist // + +// 2[colors] * 6[pieces] * 64[squares] +inline U64 zKeys[2][6][64]; + +// There are 8 files +inline U64 zEnPassant[8]; + +// 16 possible castling right variations +inline U64 zCastle[16]; + +inline U64 zSide; + +void InitZobrist(); +U64 GetHashKey(Board &board); + std::vector split(std::string_view str, char delim); int parseSquare(std::string_view str); Move parseMove(Board &board, std::string_view str); +}