From 70bea0713c135480801e6851b57c5eb77e4f101c Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 19 Jun 2024 16:01:35 +0200 Subject: [PATCH] storing castling rights cheaper --- source/board.cpp | 26 +++++++++++++------------- source/board.h | 2 +- source/main.cpp | 3 +-- source/movegen.cpp | 41 +++++++++++++++++++++-------------------- source/types.h | 9 ++++++++- 5 files changed, 44 insertions(+), 37 deletions(-) diff --git a/source/board.cpp b/source/board.cpp index 15ef005..3fecb9b 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -7,7 +7,7 @@ void Board::Init() { } void Board::Reset() { - castlingRights = { false, false, false, false }; + castlingRights = 0; enPassantTarget = -1; halfMoves = 0; fullMoves = 0; @@ -126,16 +126,16 @@ void Board::SetByFen(const char* fen) { } break; case 'K': - castlingRights[White] = true; + castlingRights |= whiteKingRight; break; case 'Q': - castlingRights[White + 1] = true; + castlingRights |= whiteQueenRight; break; case 'k': - castlingRights[Black * 2] = true; + castlingRights |= blackKingRight; break; case 'q': - castlingRights[Black * 2 + 1] = true; + castlingRights |= blackQueenRight; break; default: if (*fen != '-') { @@ -232,10 +232,10 @@ void Board::PrintBoard() { } std::cout << " Castling rights: "; - if (castlingRights[0]) std::cout << "K"; else std::cout << "-"; - if (castlingRights[1]) std::cout << "Q"; else std::cout << "-"; - if (castlingRights[2]) std::cout << "k"; else std::cout << "-"; - if (castlingRights[3]) std::cout << "q"; else std::cout << "-"; + if (castlingRights & whiteKingRight) std::cout << "K"; else std::cout << "-"; + 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'; } @@ -302,13 +302,13 @@ static void UpdateCastlingRights(Board &board, int square, int type, int color) int kingSideRook = color ? h8 : h1; if (square == queenSideRook) { - board.castlingRights[color * 2 + 1] = false; + board.castlingRights &= color ? ~blackQueenRight : ~whiteQueenRight; } else if (square == kingSideRook) { - board.castlingRights[color * 2] = false; + board.castlingRights &= color ? ~blackKingRight : ~whiteKingRight; } } else if (type == King) { - board.castlingRights[color * 2 + 1] = false; - board.castlingRights[color * 2] = false; + board.castlingRights &= color ? ~blackKingRight : ~whiteKingRight; + board.castlingRights &= color ? ~blackQueenRight : ~whiteQueenRight; } } diff --git a/source/board.h b/source/board.h index aecf557..84b6a78 100644 --- a/source/board.h +++ b/source/board.h @@ -23,7 +23,7 @@ public: int halfMoves = 0; int fullMoves = 0; - std::array castlingRights = { false, false, false, false }; + uint8_t castlingRights = 0; int enPassantTarget; void Init(); diff --git a/source/main.cpp b/source/main.cpp index 245fe21..4505175 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,7 +1,6 @@ #include "board.h" #include "movegen.h" #include "uci.h" -#include "perft.h" int main() { initLeaperAttacks(); @@ -9,7 +8,7 @@ int main() { Board board; - Perft(board, 7); + UCILoop(board); return 0; } diff --git a/source/movegen.cpp b/source/movegen.cpp index 3a9f4dc..0df13a2 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -807,27 +807,28 @@ static void GenKingMoves(Board &board, bool color, bool isInCheck) { } // Generating castles - if (!isInCheck) { - if (board.castlingRights[color * 2 + 1]) { - U64 QueenSide = color ? 0x1f00000000000000 : 0x1f; - U64 mask = color ? 0x1c00000000000000 : 0x1c; - int targetSquare = color ? c8 : c1; - if ((Bitboard(QueenSide) & board.occupied).PopCount() == 2 - && !(mask & board.GetAttackMaps(!color))) { - board.AddMove(Move(kingSquare, targetSquare, queenCastle)); - } - } + int kingRight = color ? blackKingRight : whiteKingRight; + int queenRight = color ? blackQueenRight : whiteQueenRight; - if (board.castlingRights[color * 2]) { - U64 KingSide = color ? 0xf000000000000000 : 0xf0; - U64 mask = color ? 0x7000000000000000 : 0x70; - int targetSquare = color ? g8 : g1; - if ((Bitboard(KingSide) & board.occupied).PopCount() == 2 - && !(mask & board.GetAttackMaps(!color))) { - board.AddMove(Move(kingSquare, targetSquare, kingCastle)); - } - } - } + if (board.castlingRights & queenRight) { + U64 QueenSide = color ? 0x1f00000000000000 : 0x1f; + U64 mask = color ? 0x1c00000000000000 : 0x1c; + int targetSquare = color ? c8 : c1; + if ((Bitboard(QueenSide) & board.occupied).PopCount() == 2 + && !(mask & board.GetAttackMaps(!color))) { + board.AddMove(Move(kingSquare, targetSquare, queenCastle)); + } + } + + if (board.castlingRights & kingRight) { + U64 KingSide = color ? 0xf000000000000000 : 0xf0; + U64 mask = color ? 0x7000000000000000 : 0x70; + int targetSquare = color ? g8 : g1; + if ((Bitboard(KingSide) & board.occupied).PopCount() == 2 + && !(mask & board.GetAttackMaps(!color))) { + board.AddMove(Move(kingSquare, targetSquare, kingCastle)); + } + } } // Generates moves to get out of check diff --git a/source/types.h b/source/types.h index 7b1cd65..809a86d 100644 --- a/source/types.h +++ b/source/types.h @@ -20,6 +20,13 @@ enum PIECE { King }; +enum CASTLING_RIGHTS { + whiteKingRight = 1, + whiteQueenRight = 2, + blackKingRight = 4, + blackQueenRight = 8 +}; + constexpr std::array PIECE_LETTERS = { 'P', 'p', 'N', 'n', @@ -87,4 +94,4 @@ enum DIRECTION { noEa = north + east, soWe = south + west, soEa = south + east -}; \ No newline at end of file +};