From a9b51e384f957956f4906ff3a270cdcbbc1958ef Mon Sep 17 00:00:00 2001 From: rektdie Date: Thu, 19 Feb 2026 15:06:57 +0100 Subject: [PATCH] isLegal function | bench 1812688 --- source/board.cpp | 96 ++++++++++++++++++++++++++++++++++++++++++++--- source/board.h | 2 + source/move.h | 4 ++ source/perft.cpp | 10 +++-- source/search.cpp | 14 +++---- 5 files changed, 109 insertions(+), 17 deletions(-) diff --git a/source/board.cpp b/source/board.cpp index 4f0e51c..50e4a8c 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "utils.h" void Board::Reset() { @@ -348,7 +349,6 @@ void Board::Promote(int square, int pieceType, int color, bool isCapture) { } bool Board::MakeMove(Move move) { - Board save = *this; // Null Move if (!move) { int newEpTarget = noEPTarget; @@ -487,13 +487,15 @@ bool Board::MakeMove(Move move) { MOVEGEN::GenThreatMaps(*this); pinned = {CalcPinned(White), CalcPinned(Black)}; + checkers = CalcCheckers(); + /* sideToMove = !sideToMove; if (InCheck()) { - *this = save; return false; } sideToMove = !sideToMove; + */ if (attackerColor == Black) fullMoves++; if (attackerPiece == Pawn || move.IsCapture()) { @@ -612,6 +614,36 @@ static Bitboard rayBetween(int sq1, int sq2) { return ray; } +// Returns the full ray from `from` in the direction of `to`, to the edge of the board +static Bitboard fullRay(int from, int to) { + int df = (to % 8) - (from % 8); + int dr = (to / 8) - (from / 8); + + if (df != 0) df /= std::abs(df); + if (dr != 0) dr /= std::abs(dr); + + Bitboard ray = 0ULL; + + int f = from % 8 + df; + int r = from / 8 + dr; + while (f >= 0 && f < 8 && r >= 0 && r < 8) { + ray |= 1ULL << (r * 8 + f); + f += df; + r += dr; + } + + f = from % 8 - df; + r = from / 8 - dr; + while (f >= 0 && f < 8 && r >= 0 && r < 8) { + ray |= 1ULL << (r * 8 + f); + f -= df; + r -= dr; + } + + ray |= 1ULL << from; + return ray; +} + bool Board::IsSquareThreatened(bool side, int square) { return colorThreats[!side].IsSet(square); } @@ -623,19 +655,21 @@ Bitboard Board::CalcCheckers() { Bitboard Board::CalcPinned(bool color) { Bitboard pinned; - Bitboard kingSquare = (pieces[King] & colors[color]).getLS1BIndex(); + int kingSquare = (pieces[King] & colors[color]).getLS1BIndex(); + + assert(kingSquare != -1); Bitboard oppQueens = pieces[Queen] & colors[!color]; - Bitboard potentialAttackers = MOVEGEN::getPieceAttacks(kingSquare, Bishop, !color, colors[!color]) & (oppQueens | (pieces[Bishop] & colors[!color])) - | MOVEGEN::getPieceAttacks(kingSquare, Rook, !color, colors[!color]) & (oppQueens | (pieces[Rook] & colors[!color])); + Bitboard potentialAttackers = MOVEGEN::getBishopAttack(kingSquare, colors[!color]) & (oppQueens | (pieces[Bishop] & colors[!color])) + | MOVEGEN::getRookAttack(kingSquare, colors[!color]) & (oppQueens | (pieces[Rook] & colors[!color])); while (potentialAttackers) { int attackerSquare = potentialAttackers.getLS1BIndex(); Bitboard isPinned = colors[color] & rayBetween(attackerSquare, kingSquare); - if (isPinned) + if (isPinned.PopCount() == 1) pinned |= isPinned; potentialAttackers.PopBit(attackerSquare); @@ -643,3 +677,53 @@ Bitboard Board::CalcPinned(bool color) { return pinned; } + +bool Board::IsLegal(Move &move) { + assert(move != 0); + + int us = sideToMove; + int them = !us; + + int from = move.MoveFrom(); + int to = move.MoveTo(); + + Bitboard king = pieces[King] & colors[us]; + int kingSquare = king.getLS1BIndex(); + + int moveType = move.GetFlags(); + + if (moveType == epCapture) { + Bitboard occAfterEP = occupied ^ Bitboard::GetSquare(from) ^ Bitboard::GetSquare(to) + ^ Bitboard::GetSquare(enPassantTarget); + + Bitboard theirQueens = pieces[Queen] & colors[them]; + + return (MOVEGEN::getPieceAttacks(kingSquare, Bishop, them, occAfterEP) & (theirQueens | (pieces[Bishop] & colors[them]))).PopCount() < 1 + && (MOVEGEN::getPieceAttacks(kingSquare, Rook, them, occAfterEP) & (theirQueens | (pieces[Rook] & colors[them]))).PopCount() < 1; + } + + int movingPiece = GetPieceType(from); + + if (movingPiece == King) { + Bitboard kinglessOcc = occupied ^ king; + Bitboard theirQueens = pieces[Queen] & colors[them]; + + return !colorThreats[them].IsSet(to) + && (MOVEGEN::getBishopAttack(to, kinglessOcc) & (theirQueens | (pieces[Bishop] & colors[them]))).PopCount() == 0 + && (MOVEGEN::getRookAttack(to, kinglessOcc) & (theirQueens | (pieces[Rook] & colors[them]))).PopCount() == 0; + } + + if (checkers.PopCount() > 1) { + return false; + } + + if (pinned[us].IsSet(from) && !fullRay(kingSquare, from).IsSet(to)) + return false; + + if (checkers.PopCount() < 1) + return true; + + int checkerSquare = checkers.getLS1BIndex(); + + return (rayBetween(kingSquare, checkerSquare) | checkers).IsSet(to); +} diff --git a/source/board.h b/source/board.h index 14fb0ca..4916472 100644 --- a/source/board.h +++ b/source/board.h @@ -78,4 +78,6 @@ public: Bitboard CalcCheckers(); Bitboard CalcPinned(bool color); + + bool IsLegal(Move &move); }; diff --git a/source/move.h b/source/move.h index 5d8ac1b..6c046ad 100644 --- a/source/move.h +++ b/source/move.h @@ -64,6 +64,10 @@ public: return (GetFlags() >= knightPromotion); } + bool IsCastle() { + return (GetFlags() == kingCastle || GetFlags() == queenCastle); + } + void PrintMove() { std::cout << squareCoords[MoveFrom()] << squareCoords[MoveTo()]; diff --git a/source/perft.cpp b/source/perft.cpp index 263fd2a..fba548f 100644 --- a/source/perft.cpp +++ b/source/perft.cpp @@ -1,8 +1,9 @@ #include "perft.h" #include "stopwatch.h" #include "movegen.h" +#include -static U64 HelperPerft(Board board, int depth) { +static U64 HelperPerft(Board &board, int depth) { MOVEGEN::GenerateMoves(board, true); if (depth == 0) return 1ULL; @@ -10,15 +11,18 @@ 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; - bool isLegal = copy.MakeMove(board.moveList[i]); - if (!isLegal) continue; + copy.MakeMove(board.moveList[i]); + nodes += HelperPerft(copy, depth - 1); } return nodes; } void Perft(Board &board, int depth) { + board.SetByFen(StartingFen); MOVEGEN::GenerateMoves(board, true); U64 totalNodes = 0; diff --git a/source/search.cpp b/source/search.cpp index 455a2c1..d0c36b4 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -358,9 +358,9 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear continue; } + if (!board.IsLegal(board.moveList[i])) continue; Board copy = board; - int isLegal = copy.MakeMove(board.moveList[i]); - if (!isLegal) continue; + copy.MakeMove(board.moveList[i]); ctx->ss[ply].pieceType = board.GetPieceType(board.moveList[i].MoveFrom()); ctx->ss[ply].moveTo = board.moveList[i].MoveTo(); @@ -523,10 +523,9 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC if (!SEE(board, currMove, seeThreshold)) continue; + if (!board.IsLegal(currMove)) continue; Board copy = board; - bool isLegal = copy.MakeMove(currMove); - - if (!isLegal) continue; + copy.MakeMove(currMove); if (copy.positionIndex >= ctx->positionHistory.size()) { ctx->positionHistory.resize(copy.positionIndex + 100); @@ -610,10 +609,9 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC continue; } + if (!board.IsLegal(currMove)) continue; Board copy = board; - bool isLegal = copy.MakeMove(currMove); - - if (!isLegal) continue; + copy.MakeMove(currMove); ctx->ss[ply].pieceType = board.GetPieceType(currMove.MoveFrom()); ctx->ss[ply].moveTo = currMove.MoveTo();