From e442906d66642b156989e41aacde424e529167c1 Mon Sep 17 00:00:00 2001 From: "rektdie@gmail.com" Date: Thu, 4 Apr 2024 02:17:18 +0200 Subject: [PATCH] Added attack maps and fixed illegal castle move generation --- source/board.cpp | 32 ++++++++++++++------------------ source/board.h | 3 +++ source/main.cpp | 1 + source/movegen.cpp | 35 +++++++++++++++++++++++++++++++++-- source/movegen.h | 2 ++ 5 files changed, 53 insertions(+), 20 deletions(-) diff --git a/source/board.cpp b/source/board.cpp index fea3ce7..d5104c8 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -13,7 +13,6 @@ void Board::Reset() { fullMoves = 0; sideToMove = White; occupied = 0ULL; - std::cout << '\n'; pieces = std::array(); colors = std::array(); @@ -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--; diff --git a/source/board.h b/source/board.h index c46658b..002b295 100644 --- a/source/board.h +++ b/source/board.h @@ -13,6 +13,8 @@ public: std::array pieces; std::array colors; + U64 attackMaps[2][64]; + Bitboard occupied; std::vector 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); diff --git a/source/main.cpp b/source/main.cpp index b9e95c1..689288b 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -9,6 +9,7 @@ int main() { initSliderAttacks(); Board board; + Perft(board, 2); return 0; diff --git a/source/movegen.cpp b/source/movegen.cpp index 3365100..d55eef6 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -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) { diff --git a/source/movegen.h b/source/movegen.h index f1e0999..b657b53 100644 --- a/source/movegen.h +++ b/source/movegen.h @@ -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); \ No newline at end of file