From 28ce0bc64cf1528ea8ce382b55ac232c937fb7e8 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 24 Mar 2025 00:33:21 +0100 Subject: [PATCH 01/14] generating pawn moves --- source/board.cpp | 7 +- source/movegen.cpp | 590 +-------------------------------------------- source/movegen.h | 31 ++- source/movegen.tpp | 91 +++++++ source/perft.cpp | 4 +- source/search.cpp | 4 +- source/uci.cpp | 3 +- source/utils.cpp | 2 +- 8 files changed, 134 insertions(+), 598 deletions(-) create mode 100644 source/movegen.tpp diff --git a/source/board.cpp b/source/board.cpp index 2625a5f..2a9319e 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -65,8 +65,7 @@ void Board::SetByFen(std::string_view fen) { occupied = colors[White] | colors[Black]; hashKey = UTILS::GetHashKey(*this); - MOVEGEN::GenerateMoves(*this); - MOVEGEN::GenAttackMaps(*this); + MOVEGEN::GenerateMoves(*this); } void Board::PrintBoard() { @@ -339,9 +338,7 @@ void Board::MakeMove(Move move) { } enPassantTarget = newEpTarget; - - MOVEGEN::GenAttackMaps(*this); - + positionIndex++; positionHistory[positionIndex] = hashKey; } diff --git a/source/movegen.cpp b/source/movegen.cpp index 82b0387..3ee4521 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -2,11 +2,6 @@ namespace MOVEGEN { -static Bitboard pawnAttacks[2][64]; -static Bitboard knightAttacks[64]; -static Bitboard kingAttacks[64]; - - static Bitboard bishopMasks[64]; static Bitboard rookMasks[64]; @@ -240,7 +235,7 @@ void initSliderAttacks() { } } -static Bitboard getBishopAttack(int square, U64 occupancy) { +Bitboard getBishopAttack(int square, U64 occupancy) { occupancy &= bishopMasks[square]; occupancy *= bishopMagicNumbers[square]; occupancy >>= (64 - bishopRelevantBits[square]); @@ -248,7 +243,7 @@ static Bitboard getBishopAttack(int square, U64 occupancy) { return bishopAttacks[square][occupancy]; } -static Bitboard getRookAttack(int square, U64 occupancy) { +Bitboard getRookAttack(int square, U64 occupancy) { occupancy &= rookMasks[square]; occupancy *= rookMagicNumbers[square]; occupancy >>= (64 - rookRelevantBits[square]); @@ -256,7 +251,7 @@ static Bitboard getRookAttack(int square, U64 occupancy) { return rookAttacks[square][occupancy]; } -static Bitboard getQueenAttack(int square, U64 occupancy) { +Bitboard getQueenAttack(int square, U64 occupancy) { U64 bishopOccupancy = occupancy; U64 rookOccupancy = occupancy; @@ -271,7 +266,7 @@ static Bitboard getQueenAttack(int square, U64 occupancy) { return rookAttacks[square][rookOccupancy] | bishopAttacks[square][bishopOccupancy]; } -static Bitboard getPieceAttacks(int square, int piece, int color, U64 occupancy) { +Bitboard getPieceAttacks(int square, int piece, int color, U64 occupancy) { if (piece == Pawn) { return pawnAttacks[color][square]; } else if (piece == Knight) { @@ -287,7 +282,7 @@ static Bitboard getPieceAttacks(int square, int piece, int color, U64 occupancy) } } -static Bitboard getPawnPushes(int square, bool color, Bitboard &occupancy) { +Bitboard getPawnPushes(int square, bool color, Bitboard &occupancy) { Bitboard pushes; int direction = color ? south : north; @@ -429,579 +424,4 @@ static bool IsPinned(Board &board, int square) { return false; } - -static bool IsLegalEnPassant(Board &board, int square) { - Bitboard kingSquare = board.colors[board.sideToMove] & board.pieces[King]; - int pushDirection = board.sideToMove ? south : north; - Bitboard newOccupancy = board.occupied; - newOccupancy.PopBit(square); - newOccupancy.PopBit(board.enPassantTarget - pushDirection); - - // Looking for horizontal checks - int directionToKing = GetDirection(square, kingSquare.getLS1BIndex()); - - // Either east or west - if (abs(directionToKing) == 1) { - int currentSq = kingSquare.getLS1BIndex() - directionToKing; - - Bitboard endingFile = directionToKing > 0 ? files[H] : files[A]; - - while (!endingFile.IsSet(currentSq)) { - if (newOccupancy.IsSet(currentSq)) { - int squareColor = board.GetPieceColor(currentSq); - int squarePiece = board.GetPieceType(currentSq); - - if (squareColor != board.sideToMove) { - if (squarePiece == Rook || squarePiece == Queen) { - return false; - } - } - break; - } - currentSq -= directionToKing; - } - } - - return true; -} - -static void GenPawnMoves(Board &board, bool color) { - Bitboard pawns = board.colors[color] & board.pieces[Pawn]; - - while (pawns) { - int square = pawns.getLS1BIndex(); - - Bitboard pushes = getPawnPushes(square, color, board.occupied); - Bitboard captures = pawnAttacks[color][square]; - - Bitboard lastRank = color ? ranks[r_1] : ranks[r_8]; - - if (IsPinned(board, square)) { - int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex(); - int directionToKing = GetDirection(square, kingSquare); - - Bitboard pinningRay = GetPinningRay(board, square, -directionToKing); - pushes &= pinningRay; - captures &= pinningRay; - - // Checking for en passant - if (board.enPassantTarget != noEPTarget) { - if (captures.IsSet(board.enPassantTarget)) { - if (IsLegalEnPassant(board, square)) { - board.AddMove(Move(square, board.enPassantTarget, epCapture)); - } - } - } - - // Removing own pieces from capture mask - captures &= board.colors[!color]; - - while (pushes) { - int pushSquare = pushes.getLS1BIndex(); - - if (abs(square - pushSquare) == 16) { - board.AddMove(Move(square, pushSquare, doublePawnPush)); - } else { - board.AddMove(Move(square, pushSquare, quiet)); - } - - pushes.PopBit(pushSquare); - } - - while (captures) { - int targetSquare = captures.getLS1BIndex(); - - // Checking for promotion - if (lastRank.IsSet(targetSquare)) { - board.AddMove(Move(square, targetSquare, knightPromoCapture)); - board.AddMove(Move(square, targetSquare, bishopPromoCapture)); - board.AddMove(Move(square, targetSquare, rookPromoCapture)); - board.AddMove(Move(square, targetSquare, queenPromoCapture)); - } else { - board.AddMove(Move(square, targetSquare, capture)); - } - - captures.PopBit(targetSquare); - } - - pawns.PopBit(square); - continue; - } - - // Checking for en passant - if (board.enPassantTarget != noEPTarget) { - if (captures.IsSet(board.enPassantTarget)) { - // Checking if the move gives discovered check to own king - if (IsLegalEnPassant(board, square)) { - board.AddMove(Move(square, board.enPassantTarget, epCapture)); - } - } - } - - // Removing own pieces from capture mask - captures &= board.colors[!color]; - - while (pushes) { - int pushSquare = pushes.getLS1BIndex(); - - // Checking for promotion - if (lastRank.IsSet(pushSquare)) { - board.AddMove(Move(square, pushSquare, knightPromotion)); - board.AddMove(Move(square, pushSquare, bishopPromotion)); - board.AddMove(Move(square, pushSquare, rookPromotion)); - board.AddMove(Move(square, pushSquare, queenPromotion)); - } else { - if (abs(square - pushSquare) == 16) { - board.AddMove(Move(square, pushSquare, doublePawnPush)); - } else { - board.AddMove(Move(square, pushSquare, quiet)); - } - } - - pushes.PopBit(pushSquare); - } - - while (captures) { - int targetSquare = captures.getLS1BIndex(); - - // Checking for promotion - if (lastRank.IsSet(targetSquare)) { - board.AddMove(Move(square, targetSquare, knightPromoCapture)); - board.AddMove(Move(square, targetSquare, bishopPromoCapture)); - board.AddMove(Move(square, targetSquare, rookPromoCapture)); - board.AddMove(Move(square, targetSquare, queenPromoCapture)); - } else { - board.AddMove(Move(square, targetSquare, capture)); - } - - captures.PopBit(targetSquare); - } - - pawns.PopBit(square); - } -} - -static void GenKnightMoves(Board &board, bool color) { - Bitboard knights = board.colors[color] & board.pieces[Knight]; - - while (knights) { - int square = knights.getLS1BIndex(); - - if (IsPinned(board, square)) { - knights.PopBit(square); - continue; - } - - Bitboard moves = knightAttacks[square] & ~board.colors[color]; - Bitboard captures = moves & board.colors[!color]; - moves &= ~captures; - - while (moves) { - int targetSquare = moves.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, quiet)); - - moves.PopBit(targetSquare); - } - - while (captures) { - int targetSquare = captures.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, capture)); - - captures.PopBit(targetSquare); - } - - knights.PopBit(square); - } -} - -static void GenRookMoves(Board &board, bool color) { - Bitboard rooks = board.colors[color] & board.pieces[Rook]; - - while (rooks) { - int square = rooks.getLS1BIndex(); - - Bitboard moves = getRookAttack(square, board.occupied) & ~board.colors[color]; - Bitboard captures = moves & board.colors[!color]; - moves &= ~captures; - - if (IsPinned(board, square)) { - int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex(); - int directionToKing = GetDirection(square, kingSquare); - - Bitboard pinningRay = GetPinningRay(board, square, -directionToKing); - - moves &= pinningRay; - captures &= pinningRay; - - while (moves) { - int targetSquare = moves.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, quiet)); - - moves.PopBit(targetSquare); - } - - while (captures) { - int targetSquare = captures.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, capture)); - - captures.PopBit(targetSquare); - } - - rooks.PopBit(square); - continue; - } - - - while (moves) { - int targetSquare = moves.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, quiet)); - - moves.PopBit(targetSquare); - } - - while (captures) { - int targetSquare = captures.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, capture)); - - captures.PopBit(targetSquare); - } - - rooks.PopBit(square); - } -} - -static void GenBishopMoves(Board &board, bool color) { - Bitboard bishops = board.colors[color] & board.pieces[Bishop]; - - while (bishops) { - int square = bishops.getLS1BIndex(); - - Bitboard moves = getBishopAttack(square, board.occupied) & ~board.colors[color]; - Bitboard captures = moves & board.colors[!color]; - moves &= ~captures; - - if (IsPinned(board, square)) { - int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex(); - int directionToKing = GetDirection(square, kingSquare); - - Bitboard pinningRay = GetPinningRay(board, square, -directionToKing); - - moves &= pinningRay; - captures &= pinningRay; - - while (moves) { - int targetSquare = moves.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, quiet)); - - moves.PopBit(targetSquare); - } - - while (captures) { - int targetSquare = captures.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, capture)); - - captures.PopBit(targetSquare); - } - - bishops.PopBit(square); - continue; - } - - - while (moves) { - int targetSquare = moves.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, quiet)); - - moves.PopBit(targetSquare); - } - - while (captures) { - int targetSquare = captures.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, capture)); - - captures.PopBit(targetSquare); - } - - bishops.PopBit(square); - } -} - -static void GenQueenMoves(Board &board, bool color) { - Bitboard queens = board.colors[color] & board.pieces[Queen]; - - while (queens) { - int square = queens.getLS1BIndex(); - - Bitboard moves = getQueenAttack(square, board.occupied) & ~board.colors[color]; - Bitboard captures = moves & board.colors[!color]; - moves &= ~captures; - - if (IsPinned(board, square)) { - int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex(); - int directionToKing = GetDirection(square, kingSquare); - - Bitboard pinningRay = GetPinningRay(board, square, -directionToKing); - - moves &= pinningRay; - captures &= pinningRay; - - while (moves) { - int targetSquare = moves.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, quiet)); - - moves.PopBit(targetSquare); - } - - while (captures) { - int targetSquare = captures.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, capture)); - - captures.PopBit(targetSquare); - } - - queens.PopBit(square); - continue; - } - - - while (moves) { - int targetSquare = moves.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, quiet)); - - moves.PopBit(targetSquare); - } - - while (captures) { - int targetSquare = captures.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, capture)); - - captures.PopBit(targetSquare); - } - - queens.PopBit(square); - } -} - -static void GenKingMoves(Board &board, bool color, bool isInCheck) { - int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex(); - - Bitboard moves = (kingAttacks[kingSquare] & ~board.GetAttackMaps(!color)) & ~board.colors[color]; - Bitboard captures = moves & board.colors[!color]; - moves &= ~board.colors[!color]; - - while (moves) { - int square = moves.getLS1BIndex(); - - board.AddMove(Move(kingSquare, square, quiet)); - - moves.PopBit(square); - } - - while (captures) { - int square = captures.getLS1BIndex(); - - Move move(kingSquare, square, capture); - board.AddMove(move); - - captures.PopBit(square); - } - - // Generating castles - 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; - 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 -static void GenCheckEvasions(Board &board, bool color) { - int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex(); - - // Adding moves that step out of check - GenKingMoves(board, color, true); - - Bitboard captureMask = Checkers(board, color); // Checker locations - Bitboard pushMask; // Where blockers can move to - if (captureMask.PopCount() == 1) { - int checkerSquare = captureMask.getLS1BIndex(); - int checkerType = board.GetPieceType(checkerSquare); - - if (checkerType == Rook || checkerType == Bishop || checkerType == Queen) { - pushMask = SliderRayToSquare(checkerSquare, kingSquare); - pushMask.PopBit(kingSquare); - pushMask.PopBit(checkerSquare); - } - - // looping through own pieces to add captures and blocks - for (int type = Pawn; type < King; type++) { - Bitboard currentPiece = board.colors[color] & board.pieces[type]; - - while (currentPiece) { - int square = currentPiece.getLS1BIndex(); - - if (IsPinned(board, square)) { - currentPiece.PopBit(square); - continue; - } - - Bitboard captures; - Bitboard pushes; - - if (type == Pawn) { - pushes |= getPawnPushes(square, color, board.occupied) & pushMask; - // Checking for en passant capture - if (board.enPassantTarget != noEPTarget) { - Bitboard attackedSquare = pawnAttacks[color][square] - & Bitboard::GetSquare(board.enPassantTarget); - if (attackedSquare) { - // Checking if it's legal - if (IsLegalEnPassant(board, square)) { - int direction = color ? south : north; - int targetSquare = captureMask.getLS1BIndex() + direction; - captureMask.SetBit(targetSquare); - if ((attackedSquare & pushMask) - || (attackedSquare & captureMask)) { - board.AddMove(Move(square, board.enPassantTarget, epCapture)); - } - captureMask.PopBit(targetSquare); - } - } - } - } else { - pushes |= getPieceAttacks(square, type, color, board.occupied) & pushMask; - } - - captures |= getPieceAttacks(square, type, color, board.occupied) & captureMask; - - Bitboard lastRank = color ? ranks[r_1] : ranks[r_8]; - - while (captures) { - int captureSquare = captures.getLS1BIndex(); - - // Checking for promotion - if (type == Pawn) { - if (lastRank.IsSet(captureSquare)) { - board.AddMove(Move(square, captureSquare, knightPromoCapture)); - board.AddMove(Move(square, captureSquare, bishopPromoCapture)); - board.AddMove(Move(square, captureSquare, rookPromoCapture)); - board.AddMove(Move(square, captureSquare, queenPromoCapture)); - } else { - board.AddMove(Move(square, captureSquare, capture)); - } - } else { - board.AddMove(Move(square, captureSquare, capture)); - } - - captures.PopBit(captureSquare); - } - - while (pushes) { - int pushSquare = pushes.getLS1BIndex(); - - if (type == Pawn) { - // Checking for promotion - if (lastRank.IsSet(pushSquare)) { - board.AddMove(Move(square, pushSquare, knightPromotion)); - board.AddMove(Move(square, pushSquare, bishopPromotion)); - board.AddMove(Move(square, pushSquare, rookPromotion)); - board.AddMove(Move(square, pushSquare, queenPromotion)); - } else { - if (abs(square - pushSquare) == 16) { - board.AddMove(Move(square, pushSquare, doublePawnPush)); - } else { - board.AddMove(Move(square, pushSquare, quiet)); - } - } - } else { - board.AddMove(Move(square, pushSquare, quiet)); - } - - pushes.PopBit(pushSquare); - } - - currentPiece.PopBit(square); - } - } - } else { // We can only step out of double check - return; - } -} - -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) { - int square = pieces.getLS1BIndex(); - if (type == Pawn) { - board.attackMaps[color][type] |= pawnAttacks[color][square]; - } else if (type == Knight) { - board.attackMaps[color][type] |= knightAttacks[square]; - } else if (type == King) { - board.attackMaps[color][type] |= kingAttacks[square]; - } else if (type == Rook) { - board.attackMaps[color][type] |= getRookAttack(square, board.occupied - & ~(board.colors[!color] & board.pieces[King])); - } else if (type == Bishop) { - board.attackMaps[color][type] |= getBishopAttack(square, board.occupied - & ~(board.colors[!color] & board.pieces[King])); - } else if (type == Queen) { - board.attackMaps[color][type] |= getQueenAttack(square, board.occupied - & ~(board.colors[!color] & board.pieces[King])); - } - pieces.PopBit(square); - } - } - } -} - -void GenerateMoves(Board &board) { - board.ResetMoves(); - - if (board.InCheck()) { - GenCheckEvasions(board, board.sideToMove); - return; - } - - GenPawnMoves(board, board.sideToMove); - GenKnightMoves(board, board.sideToMove); - GenKingMoves(board, board.sideToMove, false); - GenBishopMoves(board, board.sideToMove); - GenRookMoves(board, board.sideToMove); - GenQueenMoves(board, board.sideToMove); -} } \ No newline at end of file diff --git a/source/movegen.h b/source/movegen.h index 04a965f..36758bc 100644 --- a/source/movegen.h +++ b/source/movegen.h @@ -3,15 +3,43 @@ #include "move.h" #include "board.h" +enum MovegenMode { + All, + Noisy +}; + namespace MOVEGEN { +inline Bitboard pawnAttacks[2][64]; +inline Bitboard knightAttacks[64]; +inline Bitboard kingAttacks[64]; + void initLeaperAttacks(); void initSliderAttacks(); -void GenAttackMaps(Board &board); +Bitboard getBishopAttack(int square, U64 occupancy); +Bitboard getRookAttack(int square, U64 occupancy); +Bitboard getQueenAttack(int square, U64 occupancy); +Bitboard getPawnPushes(int square, bool color, Bitboard &occupancy); +template +void GenPawnMoves(Board &board); +template +void GenKnightMoves(Board &board); +template +void GenRookMoves(Board &board); +template +void GenBishopMoves(Board &board); +template +void GenQueenMoves(Board &board); +template +void GenKingMoves(Board &board); + +template void GenerateMoves(Board &board); + + // bishop relevant occupancy bit count constexpr int bishopRelevantBits[64] = { 6, 5, 5, 5, 5, 5, 5, 6, @@ -169,4 +197,5 @@ constexpr U64 bishopMagicNumbers[64] = { 0x8918844842082200ULL, 0x4010011029020020ULL }; +#include "movegen.tpp" } \ No newline at end of file diff --git a/source/movegen.tpp b/source/movegen.tpp new file mode 100644 index 0000000..a275f39 --- /dev/null +++ b/source/movegen.tpp @@ -0,0 +1,91 @@ +template +void GenPawnMoves(Board &board) { + Bitboard pawns = board.pieces[Pawn] & board.colors[board.sideToMove]; + + while (pawns) { + int square = pawns.getLS1BIndex(); + + Bitboard pushes = getPawnPushes(square, board.sideToMove, board.occupied); + + Bitboard lastRank = board.sideToMove ? ranks[r_1] : ranks[r_8]; + + if constexpr (mode == Noisy) { + Bitboard captures = pawnAttacks[board.sideToMove][square] & board.colors[!board.sideToMove]; + pushes &= lastRank; + + if (board.enPassantTarget != noEPTarget) { + if (captures.IsSet(board.enPassantTarget)) { + board.AddMove(Move(square, board.enPassantTarget, epCapture)); + } + } + + while (captures) { + int targetSquare = captures.getLS1BIndex(); + + if (lastRank.IsSet(targetSquare)) { + board.AddMove(Move(square, targetSquare, knightPromoCapture)); + board.AddMove(Move(square, targetSquare, bishopPromoCapture)); + board.AddMove(Move(square, targetSquare, rookPromoCapture)); + board.AddMove(Move(square, targetSquare, queenPromoCapture)); + } else { + board.AddMove(Move(square, targetSquare, capture)); + } + + captures.PopBit(targetSquare); + } + + while (pushes) { + int pushSquare = pushes.getLS1BIndex(); + + // Only promotions because NOISY + board.AddMove(Move(square, pushSquare, knightPromotion)); + board.AddMove(Move(square, pushSquare, bishopPromotion)); + board.AddMove(Move(square, pushSquare, rookPromotion)); + board.AddMove(Move(square, pushSquare, queenPromotion)); + + pushes.PopBit(pushSquare); + } + } else { + while (pushes) { + int pushSquare = pushes.getLS1BIndex(); + + if (std::abs(square - pushSquare) == 16) { + board.AddMove(Move(square, pushSquare, doublePawnPush)); + } else { + board.AddMove(Move(square, pushSquare, quiet)); + } + + pushes.PopBit(pushSquare); + } + } + + pawns.PopBit(square); + } +} +template +void GenKnightMoves(Board &board) { +} +template +void GenRookMoves(Board &board) { +} +template +void GenBishopMoves(Board &board) { +} +template +void GenQueenMoves(Board &board) { +} +template +void GenKingMoves(Board &board) { +} + +template +void GenerateMoves(Board &board) { + board.ResetMoves(); + + GenPawnMoves(board); + GenKnightMoves(board); + GenKingMoves(board); + GenBishopMoves(board); + GenRookMoves(board); + GenQueenMoves(board); +}; \ No newline at end of file diff --git a/source/perft.cpp b/source/perft.cpp index ed8f828..9320e71 100644 --- a/source/perft.cpp +++ b/source/perft.cpp @@ -3,7 +3,7 @@ #include "movegen.h" static U64 HelperPerft(Board board, int depth) { - MOVEGEN::GenerateMoves(board); + MOVEGEN::GenerateMoves(board); if (depth == 1) return board.currentMoveIndex; if (depth == 0) return 1ULL; @@ -21,7 +21,7 @@ static U64 HelperPerft(Board board, int depth) { } void Perft(Board &board, int depth) { - MOVEGEN::GenerateMoves(board); + MOVEGEN::GenerateMoves(board); U64 totalNodes = 0; diff --git a/source/search.cpp b/source/search.cpp index 42b02b1..c1f6981 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -130,7 +130,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) { alpha = bestScore; } - MOVEGEN::GenerateMoves(board); + MOVEGEN::GenerateMoves(board); SortMoves(board, ply); @@ -213,7 +213,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { } } - MOVEGEN::GenerateMoves(board); + MOVEGEN::GenerateMoves(board); if (board.currentMoveIndex == 0) { if (board.InCheck()) { // checkmate diff --git a/source/uci.cpp b/source/uci.cpp index d9f72b4..9d6e8a2 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -36,8 +36,7 @@ static void ParsePosition(Board &board, std::string_view command) { } } - MOVEGEN::GenerateMoves(board); - MOVEGEN::GenAttackMaps(board); + MOVEGEN::GenerateMoves(board); } static int ReadParam(std::string param, std::string &command) { diff --git a/source/utils.cpp b/source/utils.cpp index 12f8bf6..a9b645c 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -95,7 +95,7 @@ int parseSquare(std::string_view str) { } Move parseMove(Board &board, std::string_view str) { - MOVEGEN::GenerateMoves(board); + MOVEGEN::GenerateMoves(board); int from = parseSquare(str.substr(0, 2)); int to = parseSquare(str.substr(2, 2)); From b8a2635410d55f694f6be2ce2c5a9184da2690e9 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 24 Mar 2025 00:43:44 +0100 Subject: [PATCH 02/14] fixed pawn gen --- source/movegen.tpp | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/source/movegen.tpp b/source/movegen.tpp index a275f39..b7d2456 100644 --- a/source/movegen.tpp +++ b/source/movegen.tpp @@ -6,11 +6,11 @@ void GenPawnMoves(Board &board) { int square = pawns.getLS1BIndex(); Bitboard pushes = getPawnPushes(square, board.sideToMove, board.occupied); + Bitboard captures = pawnAttacks[board.sideToMove][square] & board.colors[!board.sideToMove]; Bitboard lastRank = board.sideToMove ? ranks[r_1] : ranks[r_8]; if constexpr (mode == Noisy) { - Bitboard captures = pawnAttacks[board.sideToMove][square] & board.colors[!board.sideToMove]; pushes &= lastRank; if (board.enPassantTarget != noEPTarget) { @@ -45,18 +45,51 @@ void GenPawnMoves(Board &board) { pushes.PopBit(pushSquare); } + + continue; } else { + // Checking for en passant + if (board.enPassantTarget != noEPTarget) { + if (captures.IsSet(board.enPassantTarget)) { + board.AddMove(Move(square, board.enPassantTarget, epCapture)); + } + } + while (pushes) { int pushSquare = pushes.getLS1BIndex(); - - if (std::abs(square - pushSquare) == 16) { - board.AddMove(Move(square, pushSquare, doublePawnPush)); + + // Checking for promotion + if (lastRank.IsSet(pushSquare)) { + board.AddMove(Move(square, pushSquare, knightPromotion)); + board.AddMove(Move(square, pushSquare, bishopPromotion)); + board.AddMove(Move(square, pushSquare, rookPromotion)); + board.AddMove(Move(square, pushSquare, queenPromotion)); } else { - board.AddMove(Move(square, pushSquare, quiet)); + if (abs(square - pushSquare) == 16) { + board.AddMove(Move(square, pushSquare, doublePawnPush)); + } else { + board.AddMove(Move(square, pushSquare, quiet)); + } } - + pushes.PopBit(pushSquare); } + + while (captures) { + int targetSquare = captures.getLS1BIndex(); + + // Checking for promotion + if (lastRank.IsSet(targetSquare)) { + board.AddMove(Move(square, targetSquare, knightPromoCapture)); + board.AddMove(Move(square, targetSquare, bishopPromoCapture)); + board.AddMove(Move(square, targetSquare, rookPromoCapture)); + board.AddMove(Move(square, targetSquare, queenPromoCapture)); + } else { + board.AddMove(Move(square, targetSquare, capture)); + } + + captures.PopBit(targetSquare); + } } pawns.PopBit(square); From 887231df689e91c341a35868fd0b6d79d922fba9 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 24 Mar 2025 00:49:35 +0100 Subject: [PATCH 03/14] generating knight moves --- source/movegen.tpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/source/movegen.tpp b/source/movegen.tpp index b7d2456..44161ec 100644 --- a/source/movegen.tpp +++ b/source/movegen.tpp @@ -46,6 +46,7 @@ void GenPawnMoves(Board &board) { pushes.PopBit(pushSquare); } + pawns.PopBit(square); continue; } else { // Checking for en passant @@ -97,6 +98,47 @@ void GenPawnMoves(Board &board) { } template void GenKnightMoves(Board &board) { + Bitboard knights = board.colors[board.sideToMove] & board.pieces[Knight]; + + while (knights) { + int square = knights.getLS1BIndex(); + + Bitboard moves = knightAttacks[square] & ~board.colors[board.sideToMove]; + Bitboard captures = moves & board.colors[!board.sideToMove]; + + if constexpr (mode == Noisy) { + while (captures) { + int targetSquare = captures.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, capture)); + + captures.PopBit(targetSquare); + + captures.PopBit(targetSquare); + } + + knights.PopBit(square); + continue; + } else { + while (moves) { + int targetSquare = moves.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, quiet)); + + moves.PopBit(targetSquare); + } + + while (captures) { + int targetSquare = captures.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, capture)); + + captures.PopBit(targetSquare); + } + + knights.PopBit(square); + } + } } template void GenRookMoves(Board &board) { From 1b710b646b628fd7393057992fd634711767f209 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 24 Mar 2025 00:53:02 +0100 Subject: [PATCH 04/14] generating rook moves --- source/movegen.tpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/source/movegen.tpp b/source/movegen.tpp index 44161ec..28c31a6 100644 --- a/source/movegen.tpp +++ b/source/movegen.tpp @@ -142,6 +142,46 @@ void GenKnightMoves(Board &board) { } template void GenRookMoves(Board &board) { + Bitboard rooks = board.colors[board.sideToMove] & board.pieces[Rook]; + + while (rooks) { + int square = rooks.getLS1BIndex(); + + Bitboard moves = getRookAttack(square, board.occupied) & ~board.colors[board.sideToMove]; + Bitboard captures = moves & board.colors[!board.sideToMove]; + moves &= ~captures; + + if constexpr (mode == Noisy) { + while (captures) { + int targetSquare = captures.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, capture)); + + captures.PopBit(targetSquare); + } + + rooks.PopBit(square); + continue; + } + + while (moves) { + int targetSquare = moves.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, quiet)); + + moves.PopBit(targetSquare); + } + + while (captures) { + int targetSquare = captures.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, capture)); + + captures.PopBit(targetSquare); + } + + rooks.PopBit(square); + } } template void GenBishopMoves(Board &board) { From f0cd9d0ee03f676ff4eb3cc25cd9dca78d11baf8 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 24 Mar 2025 01:12:43 +0100 Subject: [PATCH 05/14] generating queen attacks --- source/movegen.tpp | 143 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 140 insertions(+), 3 deletions(-) diff --git a/source/movegen.tpp b/source/movegen.tpp index 28c31a6..61d9255 100644 --- a/source/movegen.tpp +++ b/source/movegen.tpp @@ -105,14 +105,13 @@ void GenKnightMoves(Board &board) { Bitboard moves = knightAttacks[square] & ~board.colors[board.sideToMove]; Bitboard captures = moves & board.colors[!board.sideToMove]; + moves &= ~captures; if constexpr (mode == Noisy) { while (captures) { int targetSquare = captures.getLS1BIndex(); board.AddMove(Move(square, targetSquare, capture)); - - captures.PopBit(targetSquare); captures.PopBit(targetSquare); } @@ -136,8 +135,8 @@ void GenKnightMoves(Board &board) { captures.PopBit(targetSquare); } - knights.PopBit(square); } + knights.PopBit(square); } } template @@ -185,12 +184,150 @@ void GenRookMoves(Board &board) { } template void GenBishopMoves(Board &board) { + Bitboard bishops = board.colors[board.sideToMove] & board.pieces[Bishop]; + + while (bishops) { + int square = bishops.getLS1BIndex(); + + Bitboard moves = getBishopAttack(square, board.occupied) & ~board.colors[board.sideToMove]; + Bitboard captures = moves & board.colors[!board.sideToMove]; + moves &= ~captures; + + if constexpr (mode == Noisy) { + while (captures) { + int targetSquare = captures.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, capture)); + + captures.PopBit(targetSquare); + } + + bishops.PopBit(square); + continue; + } + + while (moves) { + int targetSquare = moves.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, quiet)); + + moves.PopBit(targetSquare); + } + + while (captures) { + int targetSquare = captures.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, capture)); + + captures.PopBit(targetSquare); + } + + bishops.PopBit(square); + } } template void GenQueenMoves(Board &board) { + Bitboard queens = board.colors[board.sideToMove] & board.pieces[Queen]; + + while (queens) { + int square = queens.getLS1BIndex(); + + Bitboard moves = getQueenAttack(square, board.occupied) & ~board.colors[board.sideToMove]; + Bitboard captures = moves & board.colors[!board.sideToMove]; + moves &= ~captures; + + if constexpr (mode == Noisy) { + while (captures) { + int targetSquare = captures.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, capture)); + + captures.PopBit(targetSquare); + } + + queens.PopBit(square); + continue; + } + + while (moves) { + int targetSquare = moves.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, quiet)); + + moves.PopBit(targetSquare); + } + + while (captures) { + int targetSquare = captures.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, capture)); + + captures.PopBit(targetSquare); + } + + queens.PopBit(square); + } } template void GenKingMoves(Board &board) { + int kingSquare = (board.colors[board.sideToMove] & board.pieces[King]).getLS1BIndex(); + + Bitboard moves = (kingAttacks[kingSquare] & ~board.GetAttackMaps(!board.sideToMove)) & ~board.colors[board.sideToMove]; + Bitboard captures = moves & board.colors[!board.sideToMove]; + moves &= ~board.colors[!board.sideToMove]; + + if constexpr (mode == Noisy) { + while (captures) { + int square = captures.getLS1BIndex(); + + Move move(kingSquare, square, capture); + board.AddMove(move); + + captures.PopBit(square); + } + return; + } + + while (moves) { + int square = moves.getLS1BIndex(); + + board.AddMove(Move(kingSquare, square, quiet)); + + moves.PopBit(square); + } + + while (captures) { + int square = captures.getLS1BIndex(); + + Move move(kingSquare, square, capture); + board.AddMove(move); + + captures.PopBit(square); + } + + // Generating castles + int kingRight = board.sideToMove ? blackKingRight : whiteKingRight; + int queenRight = board.sideToMove ? blackQueenRight : whiteQueenRight; + + if (board.castlingRights & queenRight) { + U64 QueenSide = board.sideToMove ? 0x1f00000000000000 : 0x1f; + U64 mask = board.sideToMove ? 0x1c00000000000000 : 0x1c; + int targetSquare = board.sideToMove ? c8 : c1; + if ((Bitboard(QueenSide) & board.occupied).PopCount() == 2 + && !(mask & board.GetAttackMaps(!board.sideToMove))) { + board.AddMove(Move(kingSquare, targetSquare, queenCastle)); + } + } + + if (board.castlingRights & kingRight) { + U64 KingSide = board.sideToMove ? 0xf000000000000000 : 0xf0; + U64 mask = board.sideToMove ? 0x7000000000000000 : 0x70; + int targetSquare = board.sideToMove ? g8 : g1; + if ((Bitboard(KingSide) & board.occupied).PopCount() == 2 + && !(mask & board.GetAttackMaps(!board.sideToMove))) { + board.AddMove(Move(kingSquare, targetSquare, kingCastle)); + } + } } template From d0ba47c956041d37f50693c7453dcca380e0dd38 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 24 Mar 2025 02:08:26 +0100 Subject: [PATCH 06/14] removed attack maps --- source/board.cpp | 15 ++++----------- source/board.h | 6 ++---- source/movegen.cpp | 12 ------------ source/movegen.h | 1 + source/movegen.tpp | 8 +++----- source/perft.cpp | 10 ++++------ 6 files changed, 14 insertions(+), 38 deletions(-) diff --git a/source/board.cpp b/source/board.cpp index 2a9319e..97ba763 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -16,7 +16,6 @@ void Board::Reset() { colors = std::array(); moveList = std::array(); - attackMaps = std::array, 2>(); currentMoveIndex = 0; @@ -149,18 +148,12 @@ 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() { + // TODO } -bool Board::InCheck() { - Bitboard myKingSquare = colors[sideToMove] & pieces[King]; - - return GetAttackMaps(!sideToMove) & myKingSquare; +bool Board::IsLegal(Move &move) { + // TODO } void Board::SetPiece(int piece, int square, bool color) { diff --git a/source/board.h b/source/board.h index b823299..aad4225 100644 --- a/source/board.h +++ b/source/board.h @@ -18,8 +18,6 @@ public: std::array pieces; std::array colors; - std::array, 2> attackMaps; - //U64 attackMaps[2][64]; Bitboard occupied; @@ -44,9 +42,9 @@ public: int GetPieceType(int square); int GetPieceColor(int square); - - U64 GetAttackMaps(bool side); + bool InCheck(); + bool IsLegal(Move &move); void SetPiece(int piece, int square, bool color); void RemovePiece(int piece, int square, bool color); diff --git a/source/movegen.cpp b/source/movegen.cpp index 3ee4521..778b981 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -299,18 +299,6 @@ Bitboard getPawnPushes(int square, bool color, Bitboard &occupancy) { return pushes; } -// Returns bitboard of checkers -static Bitboard Checkers(Board &board, bool color) { - int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex(); - Bitboard checkerBoard = 0ULL; - - for (int type = Pawn; type < King; type++) { - Bitboard targets = getPieceAttacks(kingSquare, type, color, board.occupied); - checkerBoard |= targets & board.pieces[type] & board.colors[!color]; - } - return checkerBoard; -} - // Returns the direction to target from square static int GetDirection(int square, int target) { int rankDiff = (target / 8) - (square / 8); diff --git a/source/movegen.h b/source/movegen.h index 36758bc..d488044 100644 --- a/source/movegen.h +++ b/source/movegen.h @@ -21,6 +21,7 @@ Bitboard getBishopAttack(int square, U64 occupancy); Bitboard getRookAttack(int square, U64 occupancy); Bitboard getQueenAttack(int square, U64 occupancy); Bitboard getPawnPushes(int square, bool color, Bitboard &occupancy); +Bitboard getPieceAttacks(int square, int piece, int color, U64 occupancy); template void GenPawnMoves(Board &board); diff --git a/source/movegen.tpp b/source/movegen.tpp index 61d9255..4eb5c71 100644 --- a/source/movegen.tpp +++ b/source/movegen.tpp @@ -272,7 +272,7 @@ template void GenKingMoves(Board &board) { int kingSquare = (board.colors[board.sideToMove] & board.pieces[King]).getLS1BIndex(); - Bitboard moves = (kingAttacks[kingSquare] & ~board.GetAttackMaps(!board.sideToMove)) & ~board.colors[board.sideToMove]; + Bitboard moves = (kingAttacks[kingSquare] & ~board.colors[board.sideToMove]); Bitboard captures = moves & board.colors[!board.sideToMove]; moves &= ~board.colors[!board.sideToMove]; @@ -313,8 +313,7 @@ void GenKingMoves(Board &board) { U64 QueenSide = board.sideToMove ? 0x1f00000000000000 : 0x1f; U64 mask = board.sideToMove ? 0x1c00000000000000 : 0x1c; int targetSquare = board.sideToMove ? c8 : c1; - if ((Bitboard(QueenSide) & board.occupied).PopCount() == 2 - && !(mask & board.GetAttackMaps(!board.sideToMove))) { + if ((Bitboard(QueenSide) & board.occupied).PopCount() == 2) { board.AddMove(Move(kingSquare, targetSquare, queenCastle)); } } @@ -323,8 +322,7 @@ void GenKingMoves(Board &board) { U64 KingSide = board.sideToMove ? 0xf000000000000000 : 0xf0; U64 mask = board.sideToMove ? 0x7000000000000000 : 0x70; int targetSquare = board.sideToMove ? g8 : g1; - if ((Bitboard(KingSide) & board.occupied).PopCount() == 2 - && !(mask & board.GetAttackMaps(!board.sideToMove))) { + if ((Bitboard(KingSide) & board.occupied).PopCount() == 2) { board.AddMove(Move(kingSquare, targetSquare, kingCastle)); } } diff --git a/source/perft.cpp b/source/perft.cpp index 9320e71..801c537 100644 --- a/source/perft.cpp +++ b/source/perft.cpp @@ -12,10 +12,9 @@ static U64 HelperPerft(Board board, int depth) { for (int i = 0; i < board.currentMoveIndex; i++) { Board copy = board; - board.MakeMove(board.moveList[i]); - nodes += HelperPerft(board, depth - 1); + copy.MakeMove(board.moveList[i]); + nodes += HelperPerft(copy, depth - 1); positionIndex--; - board = copy; } return nodes; } @@ -28,10 +27,9 @@ void Perft(Board &board, int depth) { Stopwatch sw; for (int i = 0; i < board.currentMoveIndex; i++) { Board copy = board; - board.MakeMove(board.moveList[i]); - U64 nodeCount = HelperPerft(board, depth - 1); + copy.MakeMove(board.moveList[i]); + U64 nodeCount = HelperPerft(copy, depth - 1); positionIndex--; - board = copy; totalNodes += nodeCount; board.moveList[i].PrintMove(); From 2943fccce642be67a9303dff22be64e7b89e338e Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 24 Mar 2025 16:45:27 +0100 Subject: [PATCH 07/14] introduced basic InCheck and IsLegal --- source/board.cpp | 20 +++++++++++++++++--- source/board.h | 2 +- source/movegen.tpp | 9 +++++---- source/perft.cpp | 3 ++- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/source/board.cpp b/source/board.cpp index 97ba763..5bcc05f 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -132,6 +132,7 @@ void Board::ListMoves() { for (int i = 0; i < currentMoveIndex; i++) { std::cout << i+1 << ". "; moveList[i].PrintMove(); + std::cout << "( " << moveTypes[moveList[i].GetFlags()] << " )"; std::cout << std::endl; } } @@ -149,11 +150,24 @@ int Board::GetPieceColor(int square) { } bool Board::InCheck() { - // TODO + int kingSquare = (pieces[King] & colors[sideToMove]).getLS1BIndex(); + + for (int type = Pawn; type <= Queen; type++) { + Bitboard moves = MOVEGEN::getPieceAttacks(kingSquare, type, sideToMove, occupied); + + if (moves & colors[!sideToMove] & pieces[type]) return true; + } + + return false; } -bool Board::IsLegal(Move &move) { - // TODO +bool Board::IsLegal(Move move) { + Board copy = *this; + copy.MakeMove(move); + copy.sideToMove = sideToMove; + positionIndex--; + + return !copy.InCheck(); } void Board::SetPiece(int piece, int square, bool color) { diff --git a/source/board.h b/source/board.h index aad4225..4b15f95 100644 --- a/source/board.h +++ b/source/board.h @@ -44,7 +44,7 @@ public: int GetPieceColor(int square); bool InCheck(); - bool IsLegal(Move &move); + bool IsLegal(Move move); void SetPiece(int piece, int square, bool color); void RemovePiece(int piece, int square, bool color); diff --git a/source/movegen.tpp b/source/movegen.tpp index 4eb5c71..1e4deca 100644 --- a/source/movegen.tpp +++ b/source/movegen.tpp @@ -6,7 +6,8 @@ void GenPawnMoves(Board &board) { int square = pawns.getLS1BIndex(); Bitboard pushes = getPawnPushes(square, board.sideToMove, board.occupied); - Bitboard captures = pawnAttacks[board.sideToMove][square] & board.colors[!board.sideToMove]; + Bitboard attacks = pawnAttacks[board.sideToMove][square]; + Bitboard captures = attacks & board.colors[!board.sideToMove]; Bitboard lastRank = board.sideToMove ? ranks[r_1] : ranks[r_8]; @@ -14,7 +15,7 @@ void GenPawnMoves(Board &board) { pushes &= lastRank; if (board.enPassantTarget != noEPTarget) { - if (captures.IsSet(board.enPassantTarget)) { + if (attacks.IsSet(board.enPassantTarget)) { board.AddMove(Move(square, board.enPassantTarget, epCapture)); } } @@ -51,7 +52,7 @@ void GenPawnMoves(Board &board) { } else { // Checking for en passant if (board.enPassantTarget != noEPTarget) { - if (captures.IsSet(board.enPassantTarget)) { + if (attacks.IsSet(board.enPassantTarget)) { board.AddMove(Move(square, board.enPassantTarget, epCapture)); } } @@ -66,7 +67,7 @@ void GenPawnMoves(Board &board) { board.AddMove(Move(square, pushSquare, rookPromotion)); board.AddMove(Move(square, pushSquare, queenPromotion)); } else { - if (abs(square - pushSquare) == 16) { + if (std::abs(square - pushSquare) == 16) { board.AddMove(Move(square, pushSquare, doublePawnPush)); } else { board.AddMove(Move(square, pushSquare, quiet)); diff --git a/source/perft.cpp b/source/perft.cpp index 801c537..852c229 100644 --- a/source/perft.cpp +++ b/source/perft.cpp @@ -5,12 +5,12 @@ static U64 HelperPerft(Board board, int depth) { MOVEGEN::GenerateMoves(board); - if (depth == 1) return board.currentMoveIndex; if (depth == 0) return 1ULL; U64 nodes = 0; for (int i = 0; i < board.currentMoveIndex; i++) { + if (!board.IsLegal(board.moveList[i])) continue; Board copy = board; copy.MakeMove(board.moveList[i]); nodes += HelperPerft(copy, depth - 1); @@ -26,6 +26,7 @@ void Perft(Board &board, int depth) { Stopwatch sw; for (int i = 0; i < board.currentMoveIndex; i++) { + if (!board.IsLegal(board.moveList[i])) continue; Board copy = board; copy.MakeMove(board.moveList[i]); U64 nodeCount = HelperPerft(copy, depth - 1); From b9792e56da63b0a9d12eb17ad709012ba915bdf6 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 24 Mar 2025 18:21:07 +0100 Subject: [PATCH 08/14] fixed illegal castling --- source/board.cpp | 9 +++++++++ source/board.h | 3 +++ source/movegen.cpp | 32 +++++++++++++++++++++++++++++++- source/movegen.h | 4 ++-- source/movegen.tpp | 10 +++++++--- 5 files changed, 52 insertions(+), 6 deletions(-) diff --git a/source/board.cpp b/source/board.cpp index 5bcc05f..bd68172 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -14,6 +14,7 @@ void Board::Reset() { pieces = std::array(); colors = std::array(); + threatMaps = std::array, 2>(); moveList = std::array(); @@ -128,6 +129,14 @@ void Board::ResetMoves() { currentMoveIndex = 0; } +U64 Board::GetThreatMaps(bool side) { + U64 combined = 0ULL; + for (int type = Pawn; type <= King; type++) { + combined |= threatMaps[side][type]; + } + return combined; +} + void Board::ListMoves() { for (int i = 0; i < currentMoveIndex; i++) { std::cout << i+1 << ". "; diff --git a/source/board.h b/source/board.h index 4b15f95..eddadb6 100644 --- a/source/board.h +++ b/source/board.h @@ -21,6 +21,8 @@ public: Bitboard occupied; + std::array, 2> threatMaps; + std::array moveList; int currentMoveIndex = 0; @@ -40,6 +42,7 @@ public: void ResetMoves(); void ListMoves(); + U64 GetThreatMaps(bool side); int GetPieceType(int square); int GetPieceColor(int square); diff --git a/source/movegen.cpp b/source/movegen.cpp index 778b981..a82992a 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -412,4 +412,34 @@ static bool IsPinned(Board &board, int square) { return false; } -} \ No newline at end of file + +void GenThreatMaps(Board &board) { + for (int color = White; color <= Black; color++) { + for (int type = Pawn; type <= King; type++) { + board.threatMaps[color][type] = 0ULL; + Bitboard pieces = board.pieces[type] & board.colors[color]; + + while (pieces) { + int square = pieces.getLS1BIndex(); + if (type == Pawn) { + board.threatMaps[color][type] |= pawnAttacks[color][square]; + } else if (type == Knight) { + board.threatMaps[color][type] |= knightAttacks[square]; + } else if (type == King) { + board.threatMaps[color][type] |= kingAttacks[square]; + } else if (type == Rook) { + board.threatMaps[color][type] |= getRookAttack(square, board.occupied + & ~(board.colors[!color] & board.pieces[King])); + } else if (type == Bishop) { + board.threatMaps[color][type] |= getBishopAttack(square, board.occupied + & ~(board.colors[!color] & board.pieces[King])); + } else if (type == Queen) { + board.threatMaps[color][type] |= getQueenAttack(square, board.occupied + & ~(board.colors[!color] & board.pieces[King])); + } + pieces.PopBit(square); + } + } + } +} +} diff --git a/source/movegen.h b/source/movegen.h index d488044..d9f2fa0 100644 --- a/source/movegen.h +++ b/source/movegen.h @@ -39,7 +39,7 @@ void GenKingMoves(Board &board); template void GenerateMoves(Board &board); - +void GenThreatMaps(Board &board); // bishop relevant occupancy bit count constexpr int bishopRelevantBits[64] = { @@ -199,4 +199,4 @@ constexpr U64 bishopMagicNumbers[64] = { 0x4010011029020020ULL }; #include "movegen.tpp" -} \ No newline at end of file +} diff --git a/source/movegen.tpp b/source/movegen.tpp index 1e4deca..1ff4cd8 100644 --- a/source/movegen.tpp +++ b/source/movegen.tpp @@ -314,7 +314,8 @@ void GenKingMoves(Board &board) { U64 QueenSide = board.sideToMove ? 0x1f00000000000000 : 0x1f; U64 mask = board.sideToMove ? 0x1c00000000000000 : 0x1c; int targetSquare = board.sideToMove ? c8 : c1; - if ((Bitboard(QueenSide) & board.occupied).PopCount() == 2) { + if ((Bitboard(QueenSide) & board.occupied).PopCount() == 2 + && !(mask & board.GetThreatMaps(!board.sideToMove))) { board.AddMove(Move(kingSquare, targetSquare, queenCastle)); } } @@ -323,7 +324,8 @@ void GenKingMoves(Board &board) { U64 KingSide = board.sideToMove ? 0xf000000000000000 : 0xf0; U64 mask = board.sideToMove ? 0x7000000000000000 : 0x70; int targetSquare = board.sideToMove ? g8 : g1; - if ((Bitboard(KingSide) & board.occupied).PopCount() == 2) { + if ((Bitboard(KingSide) & board.occupied).PopCount() == 2 + && !(mask & board.GetThreatMaps(!board.sideToMove))) { board.AddMove(Move(kingSquare, targetSquare, kingCastle)); } } @@ -332,6 +334,8 @@ void GenKingMoves(Board &board) { template void GenerateMoves(Board &board) { board.ResetMoves(); + board.threatMaps = std::array, 2>(); + GenThreatMaps(board); GenPawnMoves(board); GenKnightMoves(board); @@ -339,4 +343,4 @@ void GenerateMoves(Board &board) { GenBishopMoves(board); GenRookMoves(board); GenQueenMoves(board); -}; \ No newline at end of file +}; From 3090dbac8b882d3570d2a012f7f783837bb644f1 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 24 Mar 2025 18:41:30 +0100 Subject: [PATCH 09/14] added legality check to search --- source/search.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/source/search.cpp b/source/search.cpp index c1f6981..5ee046b 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -137,6 +137,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) { SearchResults results; for (int i = 0; i < board.currentMoveIndex; i++) { + if (!board.IsLegal(board.moveList[i])) continue; if (board.moveList[i].IsCapture()) { Board copy = board; copy.MakeMove(board.moveList[i]); @@ -229,14 +230,18 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { int nodeType = AllNode; SearchResults results(-inf); + int moveSeen = 0; + // For all moves for (int i = 0; i < board.currentMoveIndex; i++) { Move currMove = board.moveList[i]; + if (!board.IsLegal(currMove)) continue; + Board copy = board; copy.MakeMove(currMove); - int reductions = GetReductions(board, currMove, depth, i, ply); + int reductions = GetReductions(board, currMove, depth, moveSeen, ply); // First move (suspected PV node) if (!i) { @@ -260,6 +265,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { score = -PVS(copy, depth - 1, -beta, -alpha, ply + 1).score; } + moveSeen++; positionIndex--; // Fail high (beta cutoff) From d4c7263b93a102741d5180a3913c174ce41ac1bf Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 24 Mar 2025 20:01:32 +0100 Subject: [PATCH 10/14] fixed checkmate and stalemate detection --- source/board.cpp | 21 +++++++++------------ source/board.h | 2 +- source/perft.cpp | 4 ++-- source/search.cpp | 22 +++++++++++----------- source/uci.cpp | 2 +- 5 files changed, 24 insertions(+), 27 deletions(-) diff --git a/source/board.cpp b/source/board.cpp index bd68172..125757f 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -159,20 +159,14 @@ int Board::GetPieceColor(int square) { } bool Board::InCheck() { - int kingSquare = (pieces[King] & colors[sideToMove]).getLS1BIndex(); + Bitboard myKingSquare = colors[sideToMove] & pieces[King]; - for (int type = Pawn; type <= Queen; type++) { - Bitboard moves = MOVEGEN::getPieceAttacks(kingSquare, type, sideToMove, occupied); - - if (moves & colors[!sideToMove] & pieces[type]) return true; - } - - return false; + return GetThreatMaps(!sideToMove) & myKingSquare; } bool Board::IsLegal(Move move) { Board copy = *this; - copy.MakeMove(move); + copy.MakeMove(move, true); copy.sideToMove = sideToMove; positionIndex--; @@ -231,9 +225,10 @@ void Board::Promote(int square, int pieceType, int color, bool isCapture) { SetPiece(pieceType, square, color); } -void Board::MakeMove(Move move) { - SEARCH::nodes++; - +void Board::MakeMove(Move move, bool legalityCheck) { + if (!legalityCheck) { + SEARCH::nodes++; + } // Null Move if (!move) { int newEpTarget = noEPTarget; @@ -357,6 +352,8 @@ void Board::MakeMove(Move move) { positionIndex++; positionHistory[positionIndex] = hashKey; + + MOVEGEN::GenThreatMaps(*this); } bool Board::InPossibleZug() { diff --git a/source/board.h b/source/board.h index eddadb6..11f9d12 100644 --- a/source/board.h +++ b/source/board.h @@ -53,7 +53,7 @@ public: void RemovePiece(int piece, int square, bool color); void Promote(int square, int pieceType, int color, bool isCapture); - void MakeMove(Move move); + void MakeMove(Move move, bool legalityCheck); bool InPossibleZug(); }; diff --git a/source/perft.cpp b/source/perft.cpp index 852c229..47e5706 100644 --- a/source/perft.cpp +++ b/source/perft.cpp @@ -12,7 +12,7 @@ static U64 HelperPerft(Board board, int depth) { for (int i = 0; i < board.currentMoveIndex; i++) { if (!board.IsLegal(board.moveList[i])) continue; Board copy = board; - copy.MakeMove(board.moveList[i]); + copy.MakeMove(board.moveList[i], false); nodes += HelperPerft(copy, depth - 1); positionIndex--; } @@ -28,7 +28,7 @@ void Perft(Board &board, int depth) { for (int i = 0; i < board.currentMoveIndex; i++) { if (!board.IsLegal(board.moveList[i])) continue; Board copy = board; - copy.MakeMove(board.moveList[i]); + copy.MakeMove(board.moveList[i], false); U64 nodeCount = HelperPerft(copy, depth - 1); positionIndex--; totalNodes += nodeCount; diff --git a/source/search.cpp b/source/search.cpp index 5ee046b..15e2178 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -140,7 +140,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) { if (!board.IsLegal(board.moveList[i])) continue; if (board.moveList[i].IsCapture()) { Board copy = board; - copy.MakeMove(board.moveList[i]); + copy.MakeMove(board.moveList[i], false); int score = -Quiescence(copy, -beta, -alpha, ply + 1).score; positionIndex--; @@ -201,7 +201,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { if (!doingNullMove && staticEval >= beta) { if (depth >= 3 && !board.InPossibleZug()) { Board copy = board; - copy.MakeMove(Move()); + copy.MakeMove(Move(), false); doingNullMove = true; int score = -PVS(copy, depth - 3, -beta, -beta + 1, ply + 1).score; @@ -216,14 +216,6 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { MOVEGEN::GenerateMoves(board); - if (board.currentMoveIndex == 0) { - if (board.InCheck()) { // checkmate - return -99000 + ply; - } else { // stalemate - return 0; - } - } - SortMoves(board, ply); int score = -inf; @@ -239,7 +231,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { if (!board.IsLegal(currMove)) continue; Board copy = board; - copy.MakeMove(currMove); + copy.MakeMove(currMove, false); int reductions = GetReductions(board, currMove, depth, moveSeen, ply); @@ -291,6 +283,14 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { } } + if (moveSeen == 0) { + if (board.InCheck()) { // checkmate + return -99000 + ply; + } else { // stalemate + return 0; + } + } + TT.WriteEntry(board.hashKey, depth, results.score, nodeType, results.bestMove); if (searchStopped) return 0; return results; diff --git a/source/uci.cpp b/source/uci.cpp index 9d6e8a2..21c2f3f 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -32,7 +32,7 @@ static void ParsePosition(Board &board, std::string_view command) { std::vector moves = UTILS::split(command.substr(movesIndex + 6, command.length() - (movesIndex + 5)), ' '); for (std::string_view move : moves) { - board.MakeMove(UTILS::parseMove(board, move)); + board.MakeMove(UTILS::parseMove(board, move), false); } } From 99f556e29ddc826d7f98ea7cd0b3443639eec02c Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 24 Mar 2025 23:33:59 +0100 Subject: [PATCH 11/14] bench 890807 --- source/benchmark.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/benchmark.cpp b/source/benchmark.cpp index a4e9f29..1699ab5 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -83,7 +83,7 @@ void RunBenchmark() { PVS(board, depth, -inf, inf, ply); } - std::cout << nodes << " nodes " << int(nodes/sw.GetElapsedSec()) << " nps\n"; + std::cout << nodes << " nodes " << int(nodes/sw.GetElapsedSec()) << " nps" << std::endl; nodes = 0; TT.Clear(); benchStarted = false; From 9e9319667216e94a337006f98fe3c3071210d5ad Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 25 Mar 2025 04:39:58 +0100 Subject: [PATCH 12/14] not checking for captures in QS | bench 904927 --- source/search.cpp | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index 15e2178..b44d797 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -138,22 +138,20 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) { for (int i = 0; i < board.currentMoveIndex; i++) { if (!board.IsLegal(board.moveList[i])) continue; - if (board.moveList[i].IsCapture()) { - Board copy = board; - copy.MakeMove(board.moveList[i], false); - int score = -Quiescence(copy, -beta, -alpha, ply + 1).score; - positionIndex--; + Board copy = board; + copy.MakeMove(board.moveList[i], false); + int score = -Quiescence(copy, -beta, -alpha, ply + 1).score; + positionIndex--; - if (score >= beta) { - return score; - } + if (score >= beta) { + return score; + } - bestScore = std::max(score, bestScore); + bestScore = std::max(score, bestScore); - if (score > alpha) { - alpha = score; - results.bestMove = board.moveList[i]; - } + if (score > alpha) { + alpha = score; + results.bestMove = board.moveList[i]; } } From 030c4213640061f8af04bcd217c231bb5102339b Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 25 Mar 2025 08:32:04 +0100 Subject: [PATCH 13/14] added the capture if back --- source/search.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index b44d797..15e2178 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -138,20 +138,22 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) { for (int i = 0; i < board.currentMoveIndex; i++) { if (!board.IsLegal(board.moveList[i])) continue; - Board copy = board; - copy.MakeMove(board.moveList[i], false); - int score = -Quiescence(copy, -beta, -alpha, ply + 1).score; - positionIndex--; + if (board.moveList[i].IsCapture()) { + Board copy = board; + copy.MakeMove(board.moveList[i], false); + int score = -Quiescence(copy, -beta, -alpha, ply + 1).score; + positionIndex--; - if (score >= beta) { - return score; - } + if (score >= beta) { + return score; + } - bestScore = std::max(score, bestScore); + bestScore = std::max(score, bestScore); - if (score > alpha) { - alpha = score; - results.bestMove = board.moveList[i]; + if (score > alpha) { + alpha = score; + results.bestMove = board.moveList[i]; + } } } From 12c600e1c445a6f3dfeaeacf108a8bd725f06014 Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 25 Mar 2025 11:52:36 +0100 Subject: [PATCH 14/14] was making every move twice | bench 888803 --- source/board.cpp | 26 +++++++++++--------------- source/board.h | 3 +-- source/perft.cpp | 8 ++++---- source/search.cpp | 13 +++++++------ source/uci.cpp | 2 +- 5 files changed, 24 insertions(+), 28 deletions(-) diff --git a/source/board.cpp b/source/board.cpp index 125757f..60ac881 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -164,15 +164,6 @@ bool Board::InCheck() { return GetThreatMaps(!sideToMove) & myKingSquare; } -bool Board::IsLegal(Move move) { - Board copy = *this; - copy.MakeMove(move, true); - copy.sideToMove = sideToMove; - positionIndex--; - - return !copy.InCheck(); -} - void Board::SetPiece(int piece, int square, bool color) { pieces[piece].SetBit(square); colors[color].SetBit(square); @@ -225,10 +216,7 @@ void Board::Promote(int square, int pieceType, int color, bool isCapture) { SetPiece(pieceType, square, color); } -void Board::MakeMove(Move move, bool legalityCheck) { - if (!legalityCheck) { - SEARCH::nodes++; - } +bool Board::MakeMove(Move move) { // Null Move if (!move) { int newEpTarget = noEPTarget; @@ -246,7 +234,7 @@ void Board::MakeMove(Move move, bool legalityCheck) { enPassantTarget = newEpTarget; - return; + return true; } int newEpTarget = noEPTarget; @@ -350,10 +338,18 @@ void Board::MakeMove(Move move, bool legalityCheck) { enPassantTarget = newEpTarget; + + MOVEGEN::GenThreatMaps(*this); + + sideToMove = !sideToMove; + if (InCheck()) return false; + sideToMove = !sideToMove; + positionIndex++; positionHistory[positionIndex] = hashKey; - MOVEGEN::GenThreatMaps(*this); + SEARCH::nodes++; + return true; } bool Board::InPossibleZug() { diff --git a/source/board.h b/source/board.h index 11f9d12..6a06bae 100644 --- a/source/board.h +++ b/source/board.h @@ -47,13 +47,12 @@ public: int GetPieceColor(int square); bool InCheck(); - bool IsLegal(Move move); void SetPiece(int piece, int square, bool color); void RemovePiece(int piece, int square, bool color); void Promote(int square, int pieceType, int color, bool isCapture); - void MakeMove(Move move, bool legalityCheck); + bool MakeMove(Move move); bool InPossibleZug(); }; diff --git a/source/perft.cpp b/source/perft.cpp index 47e5706..9f0ca50 100644 --- a/source/perft.cpp +++ b/source/perft.cpp @@ -10,9 +10,9 @@ static U64 HelperPerft(Board board, int depth) { U64 nodes = 0; for (int i = 0; i < board.currentMoveIndex; i++) { - if (!board.IsLegal(board.moveList[i])) continue; Board copy = board; - copy.MakeMove(board.moveList[i], false); + bool isLegal = copy.MakeMove(board.moveList[i]); + if (!isLegal) continue; nodes += HelperPerft(copy, depth - 1); positionIndex--; } @@ -26,9 +26,9 @@ void Perft(Board &board, int depth) { Stopwatch sw; for (int i = 0; i < board.currentMoveIndex; i++) { - if (!board.IsLegal(board.moveList[i])) continue; Board copy = board; - copy.MakeMove(board.moveList[i], false); + bool isLegal = copy.MakeMove(board.moveList[i]); + if (!isLegal) continue; U64 nodeCount = HelperPerft(copy, depth - 1); positionIndex--; totalNodes += nodeCount; diff --git a/source/search.cpp b/source/search.cpp index 15e2178..9570f12 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -137,10 +137,10 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) { SearchResults results; for (int i = 0; i < board.currentMoveIndex; i++) { - if (!board.IsLegal(board.moveList[i])) continue; if (board.moveList[i].IsCapture()) { Board copy = board; - copy.MakeMove(board.moveList[i], false); + int isLegal = copy.MakeMove(board.moveList[i]); + if (!isLegal) continue; int score = -Quiescence(copy, -beta, -alpha, ply + 1).score; positionIndex--; @@ -201,7 +201,8 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { if (!doingNullMove && staticEval >= beta) { if (depth >= 3 && !board.InPossibleZug()) { Board copy = board; - copy.MakeMove(Move(), false); + bool isLegal = copy.MakeMove(Move()); + // Always legal so we dont check it doingNullMove = true; int score = -PVS(copy, depth - 3, -beta, -beta + 1, ply + 1).score; @@ -228,10 +229,10 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { for (int i = 0; i < board.currentMoveIndex; i++) { Move currMove = board.moveList[i]; - if (!board.IsLegal(currMove)) continue; - Board copy = board; - copy.MakeMove(currMove, false); + bool isLegal = copy.MakeMove(currMove); + + if (!isLegal) continue; int reductions = GetReductions(board, currMove, depth, moveSeen, ply); diff --git a/source/uci.cpp b/source/uci.cpp index 21c2f3f..bd80bd6 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -32,7 +32,7 @@ static void ParsePosition(Board &board, std::string_view command) { std::vector moves = UTILS::split(command.substr(movesIndex + 6, command.length() - (movesIndex + 5)), ' '); for (std::string_view move : moves) { - board.MakeMove(UTILS::parseMove(board, move), false); + bool isLegal = board.MakeMove(UTILS::parseMove(board, move)); } }