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() {
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;
}
}
+1 -1
View File
@@ -23,7 +23,7 @@ public:
int halfMoves = 0;
int fullMoves = 0;
std::array<bool, 4> castlingRights = { false, false, false, false };
uint8_t castlingRights = 0;
int enPassantTarget;
void Init();
+1 -2
View File
@@ -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;
}
+5 -4
View File
@@ -807,8 +807,10 @@ static void GenKingMoves(Board &board, bool color, bool isInCheck) {
}
// Generating castles
if (!isInCheck) {
if (board.castlingRights[color * 2 + 1]) {
int kingRight = color ? blackKingRight : whiteKingRight;
int queenRight = color ? blackQueenRight : whiteQueenRight;
if (board.castlingRights & queenRight) {
U64 QueenSide = color ? 0x1f00000000000000 : 0x1f;
U64 mask = color ? 0x1c00000000000000 : 0x1c;
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 mask = color ? 0x7000000000000000 : 0x70;
int targetSquare = color ? g8 : g1;
@@ -828,7 +830,6 @@ static void GenKingMoves(Board &board, bool color, bool isInCheck) {
}
}
}
}
// Generates moves to get out of check
static void GenCheckEvasions(Board &board, bool color) {
+7
View File
@@ -20,6 +20,13 @@ enum PIECE {
King
};
enum CASTLING_RIGHTS {
whiteKingRight = 1,
whiteQueenRight = 2,
blackKingRight = 4,
blackQueenRight = 8
};
constexpr std::array<const char, 12> PIECE_LETTERS = {
'P', 'p',
'N', 'n',