storing castling rights cheaper
This commit is contained in:
1 parent
6e8f7e3d86
commit
70bea0713c
5 files changed
+43
-36
No files matched your search
+13
-13
@@ -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
@@ -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
@@ -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;
|
||||
}
|
||||
+21
-20
@@ -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
|
||||
|
||||
@@ -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',
|
||||
|
||||
Reference in new issue
Block a user