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)); } }