Added attack maps and fixed illegal castle move generation

This commit is contained in:
rektdie committed 2024-04-04 02:17:18 +02:00
1 parent 7dd2bd8823
commit e442906d66
5 files changed
+53 -20

No files matched your search

+14 -18
View File
@@ -13,7 +13,6 @@ void Board::Reset() {
fullMoves = 0;
sideToMove = White;
occupied = 0ULL;
std::cout << '\n';
pieces = std::array<Bitboard, 6>();
colors = std::array<Bitboard, 2>();
@@ -180,6 +179,7 @@ void Board::SetByFen(const char* fen) {
}
occupied = (colors[White] | colors[Black]);
GenAttackMaps(*this);
}
void Board::PrintBoard() {
@@ -261,26 +261,18 @@ int Board::GetPieceColor(int square) {
return (colors[Black].IsSet(square));
}
U64 Board::GetAttackMaps(bool side) {
U64 combined = 0ULL;
for (int type = Pawn; type <= King; type++) {
combined |= attackMaps[side][type];
}
return combined;
}
bool Board::InCheck(bool side) {
Bitboard myKingSquare = colors[side] & pieces[King];
for (int square = a1; square <= h8; square++) {
for (int piece = Pawn; piece <= King; piece++) {
if ((colors[!side] & pieces[piece]).IsSet(square)) {
if (piece == Pawn) {
if ((pawnAttacks[!side][square] & myKingSquare).GetBoard()) return true;
} else if (piece == Knight) {
if ((knightAttacks[square] & myKingSquare).GetBoard()) return true;
} else if (piece == Bishop) {
if ((getBishopAttack(square, occupied.GetBoard()) & myKingSquare).GetBoard()) return true;
} else if (piece == Rook) {
if ((getRookAttack(square, occupied.GetBoard()) & myKingSquare).GetBoard()) return true;
} else if (piece == Queen) {
if ((getQueenAttack(square, occupied.GetBoard()) & myKingSquare).GetBoard()) return true;
}
}
}
}
return GetAttackMaps(!side) & myKingSquare.GetBoard();
return false;
}
@@ -382,6 +374,8 @@ void Board::MakeMove(Move move) {
}
}
GenAttackMaps(*this);
halfMoves++;
if (halfMoves & 2 == 0) fullMoves++;
sideToMove = !attackerColor;
@@ -434,6 +428,8 @@ void Board::UnmakeMove() {
SetPiece(lastMove.capturedPiece, lastMove.moveTo, lastMove.capturedColor);
}
GenAttackMaps(*this);
castlingRights = lastMove.castlingRights;
if (halfMoves & 2 == 0) fullMoves--;
halfMoves--;
+3
View File
@@ -13,6 +13,8 @@ public:
std::array<Bitboard, 6> pieces;
std::array<Bitboard, 2> colors;
U64 attackMaps[2][64];
Bitboard occupied;
std::vector<Move> moveList;
@@ -38,6 +40,7 @@ public:
int GetPieceType(int square);
int GetPieceColor(int square);
U64 GetAttackMaps(bool side);
bool InCheck(bool side);
void SetPiece(int piece, int square, bool color);
+1
View File
@@ -9,6 +9,7 @@ int main() {
initSliderAttacks();
Board board;
Perft(board, 2);
return 0;
+33 -2
View File
@@ -478,21 +478,52 @@ static void GenKingMoves(Board &board, bool color) {
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) {
if ((Bitboard(QueenSide) & board.occupied).popCount() == 2
&& !(mask & board.GetAttackMaps(!color))) {
board.AddMove(Move(square, targetSquare, queenCastle));
}
}
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) {
if ((Bitboard(KingSide) & board.occupied).popCount() == 2
&& !(mask & board.GetAttackMaps(!color))) {
board.AddMove(Move(square, targetSquare, kingCastle));
}
}
}
void GenAttackMaps(Board &board) {
for (int color = White; color <= Black; color++) {
for (int type = Pawn; type <= King; type++) {
board.attackMaps[color][type] = 0ULL;
Bitboard pieces = board.pieces[type] & board.colors[color];
while (pieces.GetBoard()) {
int square = pieces.getLS1BIndex();
if (type == Pawn) {
board.attackMaps[color][type] |= pawnAttacks[color][square].GetBoard();
} else if (type == Knight) {
board.attackMaps[color][type] |= knightAttacks[square].GetBoard();
} else if (type == King) {
board.attackMaps[color][type] |= kingAttacks[square].GetBoard();
} else if (type == Rook) {
board.attackMaps[color][type] |= getRookAttack(square, board.occupied.GetBoard()).GetBoard();
} else if (type == Bishop) {
board.attackMaps[color][type] |= getBishopAttack(square, board.occupied.GetBoard()).GetBoard();
} else if (type == Queen) {
board.attackMaps[color][type] |= getQueenAttack(square, board.occupied.GetBoard()).GetBoard();
}
pieces.PopBit(square);
}
}
}
}
static bool isLegalMove(Board &board, Move move) {
if (board.InCheck(board.sideToMove)) {
if (move.moveFlags == kingCastle || move.moveFlags == queenCastle) {
+2
View File
@@ -204,4 +204,6 @@ Bitboard getBishopAttack(int square, U64 occupancy);
Bitboard getRookAttack(int square, U64 occupancy);
Bitboard getQueenAttack(int square, U64 occupancy);
void GenAttackMaps(Board &board);
void GenerateMoves(Board &board, bool side);