From 70179b953da1bb5fb9ce3ed6c00808377843bd74 Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 8 Apr 2024 09:19:23 +0200 Subject: [PATCH] bugfix: Checkers function, added: helper functions to detect partial pins in the future --- source/movegen.cpp | 49 +++++++++++++++++++++++++++------------------- source/types.h | 2 +- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/source/movegen.cpp b/source/movegen.cpp index a1ce498..843609b 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -309,7 +309,7 @@ static Bitboard Checkers(Board &board, bool color) { for (int type = Pawn; type < King; type++) { Bitboard targets = getPieceAttacks(kingSquare, type, color, board.occupied.GetBoard()); - checkerBoard |= targets & board.pieces[type]; + checkerBoard |= targets & board.pieces[type] & board.colors[!color]; } return checkerBoard; } @@ -357,30 +357,40 @@ static Bitboard SliderRaysToSquare(int attacker, int target) { return ray; } +static Bitboard GetPinningRay(Board &board, int square, int direction) { + Bitboard ray; + + if (direction) { + while (square < 64 && square >= 0) { + ray.SetBit(square); + if (board.occupied.IsSet(square)) { + int attackerColor = board.GetPieceColor(square); + int attackerType = board.GetPieceType(square); + + if (attackerColor == !board.sideToMove && (attackerType == Rook + || attackerType == Bishop || attackerType == Queen)) { + return ray; + } + return 0ULL; + } + square += direction; + } + } + + return 0ULL; +} + static bool IsPinned(Board &board, int square, int color) { int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex(); int directionToKing = GetDirection(square, kingSquare); - int currentSquare = square - directionToKing; - bool pinner = false; - - if (directionToKing) { - while (currentSquare < 64 && currentSquare >= 0) { - if (board.occupied.IsSet(currentSquare)) { - int attackerColor = board.GetPieceColor(currentSquare); - int attackerType = board.GetPieceType(currentSquare); - - if (attackerColor == !color && (attackerType == Rook - || attackerType == Bishop || attackerType == Queen)) { - pinner = true; - } - break; - } - currentSquare += -directionToKing; - } + if (SliderRaysToSquare(square, kingSquare).PopCount() != 2) { + return false; } - return pinner; + int currentSquare = square - directionToKing; + + return GetPinningRay(board, currentSquare, -directionToKing).GetBoard(); } static void GenPawnMoves(Board &board, bool color) { @@ -437,7 +447,6 @@ static void GenCheckEvasions(Board &board, bool color) { 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); diff --git a/source/types.h b/source/types.h index 266a8aa..08cb954 100644 --- a/source/types.h +++ b/source/types.h @@ -4,7 +4,7 @@ using U64 = uint64_t; -constexpr char StartingFen[] = "8/8/8/8/r2K4/2P5/8/b6k w - - 0 1"; +constexpr char StartingFen[] = "8/8/8/8/r2K4/2P5/8zz/b6k w - - 0 1"; enum COLOR { White,