storing castling rights cheaper

This commit is contained in:
rektdie committed 2024-06-19 16:01:35 +02:00
1 parent 6e8f7e3d86
commit 70bea0713c
5 files changed
+27 -20

No files matched your search

+13 -13
View File
@@ -7,7 +7,7 @@ void Board::Init() {
} }
void Board::Reset() { void Board::Reset() {
castlingRights = { false, false, false, false }; castlingRights = 0;
enPassantTarget = -1; enPassantTarget = -1;
halfMoves = 0; halfMoves = 0;
fullMoves = 0; fullMoves = 0;
@@ -126,16 +126,16 @@ void Board::SetByFen(const char* fen) {
} }
break; break;
case 'K': case 'K':
castlingRights[White] = true; castlingRights |= whiteKingRight;
break; break;
case 'Q': case 'Q':
castlingRights[White + 1] = true; castlingRights |= whiteQueenRight;
break; break;
case 'k': case 'k':
castlingRights[Black * 2] = true; castlingRights |= blackKingRight;
break; break;
case 'q': case 'q':
castlingRights[Black * 2 + 1] = true; castlingRights |= blackQueenRight;
break; break;
default: default:
if (*fen != '-') { if (*fen != '-') {
@@ -232,10 +232,10 @@ void Board::PrintBoard() {
} }
std::cout << " Castling rights: "; std::cout << " Castling rights: ";
if (castlingRights[0]) std::cout << "K"; else std::cout << "-"; if (castlingRights & whiteKingRight) std::cout << "K"; else std::cout << "-";
if (castlingRights[1]) std::cout << "Q"; else std::cout << "-"; if (castlingRights & whiteQueenRight) std::cout << "Q"; else std::cout << "-";
if (castlingRights[2]) std::cout << "k"; else std::cout << "-"; if (castlingRights & blackKingRight) std::cout << "k"; else std::cout << "-";
if (castlingRights[3]) std::cout << "q"; else std::cout << "-"; if (castlingRights & blackQueenRight) std::cout << "q"; else std::cout << "-";
std::cout << '\n'; std::cout << '\n';
} }
@@ -302,13 +302,13 @@ static void UpdateCastlingRights(Board &board, int square, int type, int color)
int kingSideRook = color ? h8 : h1; int kingSideRook = color ? h8 : h1;
if (square == queenSideRook) { if (square == queenSideRook) {
board.castlingRights[color * 2 + 1] = false; board.castlingRights &= color ? ~blackQueenRight : ~whiteQueenRight;
} else if (square == kingSideRook) { } else if (square == kingSideRook) {
board.castlingRights[color * 2] = false; board.castlingRights &= color ? ~blackKingRight : ~whiteKingRight;
} }
} else if (type == King) { } else if (type == King) {
board.castlingRights[color * 2 + 1] = false; board.castlingRights &= color ? ~blackKingRight : ~whiteKingRight;
board.castlingRights[color * 2] = false; board.castlingRights &= color ? ~blackQueenRight : ~whiteQueenRight;
} }
} }
+1 -1
View File
@@ -23,7 +23,7 @@ public:
int halfMoves = 0; int halfMoves = 0;
int fullMoves = 0; int fullMoves = 0;
std::array<bool, 4> castlingRights = { false, false, false, false }; uint8_t castlingRights = 0;
int enPassantTarget; int enPassantTarget;
void Init(); void Init();
+1 -2
View File
@@ -1,7 +1,6 @@
#include "board.h" #include "board.h"
#include "movegen.h" #include "movegen.h"
#include "uci.h" #include "uci.h"
#include "perft.h"
int main() { int main() {
initLeaperAttacks(); initLeaperAttacks();
@@ -9,7 +8,7 @@ int main() {
Board board; Board board;
Perft(board, 7); UCILoop(board);
return 0; return 0;
} }
+5 -4
View File
@@ -807,8 +807,10 @@ static void GenKingMoves(Board &board, bool color, bool isInCheck) {
} }
// Generating castles // Generating castles
if (!isInCheck) { int kingRight = color ? blackKingRight : whiteKingRight;
if (board.castlingRights[color * 2 + 1]) { int queenRight = color ? blackQueenRight : whiteQueenRight;
if (board.castlingRights & queenRight) {
U64 QueenSide = color ? 0x1f00000000000000 : 0x1f; U64 QueenSide = color ? 0x1f00000000000000 : 0x1f;
U64 mask = color ? 0x1c00000000000000 : 0x1c; U64 mask = color ? 0x1c00000000000000 : 0x1c;
int targetSquare = color ? c8 : c1; int targetSquare = color ? c8 : c1;
@@ -818,7 +820,7 @@ static void GenKingMoves(Board &board, bool color, bool isInCheck) {
} }
} }
if (board.castlingRights[color * 2]) { if (board.castlingRights & kingRight) {
U64 KingSide = color ? 0xf000000000000000 : 0xf0; U64 KingSide = color ? 0xf000000000000000 : 0xf0;
U64 mask = color ? 0x7000000000000000 : 0x70; U64 mask = color ? 0x7000000000000000 : 0x70;
int targetSquare = color ? g8 : g1; int targetSquare = color ? g8 : g1;
@@ -827,7 +829,6 @@ static void GenKingMoves(Board &board, bool color, bool isInCheck) {
board.AddMove(Move(kingSquare, targetSquare, kingCastle)); board.AddMove(Move(kingSquare, targetSquare, kingCastle));
} }
} }
}
} }
// Generates moves to get out of check // Generates moves to get out of check
+7
View File
@@ -20,6 +20,13 @@ enum PIECE {
King King
}; };
enum CASTLING_RIGHTS {
whiteKingRight = 1,
whiteQueenRight = 2,
blackKingRight = 4,
blackQueenRight = 8
};
constexpr std::array<const char, 12> PIECE_LETTERS = { constexpr std::array<const char, 12> PIECE_LETTERS = {
'P', 'p', 'P', 'p',
'N', 'n', 'N', 'n',