From fd5f070172b21c99eed31ed4beefcc3a5c07eada Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 12 Apr 2024 08:55:57 +0200 Subject: [PATCH] bugfix: GetPinningRay function didn't start the ray from the king --- source/main.cpp | 2 +- source/movegen.cpp | 25 +++++++++++++++---------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/source/main.cpp b/source/main.cpp index 87fde29..4b969c3 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -10,7 +10,7 @@ int main() { Board board; board.SetByFen("rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8"); - Perft(board, 2); + Perft(board, 3); return 0; } \ No newline at end of file diff --git a/source/movegen.cpp b/source/movegen.cpp index dcaa9a5..175200a 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -357,11 +357,12 @@ static Bitboard SliderRayToSquare(int attacker, int target) { static Bitboard GetPinningRay(Board &board, int square, int direction) { Bitboard ray; - int previousRank = square / 8; + int currentSquare = (board.pieces[King] & board.colors[board.sideToMove]).getLS1BIndex(); + int previousRank = currentSquare / 8; - square += direction; - while (square < 64 && square >= 0) { - int currentRank = square / 8; + currentSquare += direction; + while (currentSquare < 64 && currentSquare >= 0) { + int currentRank = currentSquare / 8; int difference = abs(currentRank - previousRank); @@ -373,10 +374,14 @@ static Bitboard GetPinningRay(Board &board, int square, int direction) { } previousRank = currentRank; - ray.SetBit(square); - if (board.occupied.IsSet(square)) { - int attackerType = board.GetPieceType(square); - int attackerColor = board.GetPieceColor(square); + ray.SetBit(currentSquare); + if (board.occupied.IsSet(currentSquare)) { + if (currentSquare == square) { + currentSquare += direction; + continue; + } + int attackerType = board.GetPieceType(currentSquare); + int attackerColor = board.GetPieceColor(currentSquare); if (attackerColor != board.sideToMove) { // Pinned by rook or queen @@ -393,7 +398,7 @@ static Bitboard GetPinningRay(Board &board, int square, int direction) { } break; } - square += direction; + currentSquare += direction; } return 0ULL; @@ -409,7 +414,7 @@ static bool IsPinned(Board &board, int square) { & board.occupied).PopCount() != 2) { return false; } - + return GetPinningRay(board, square, -directionToKing).PopCount(); }