From 2943fccce642be67a9303dff22be64e7b89e338e Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 24 Mar 2025 16:45:27 +0100 Subject: [PATCH] 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);