diff --git a/source/board.cpp b/source/board.cpp index bd68172..125757f 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -159,20 +159,14 @@ int Board::GetPieceColor(int square) { } bool Board::InCheck() { - int kingSquare = (pieces[King] & colors[sideToMove]).getLS1BIndex(); + Bitboard myKingSquare = colors[sideToMove] & pieces[King]; - 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; + return GetThreatMaps(!sideToMove) & myKingSquare; } bool Board::IsLegal(Move move) { Board copy = *this; - copy.MakeMove(move); + copy.MakeMove(move, true); copy.sideToMove = sideToMove; positionIndex--; @@ -231,9 +225,10 @@ void Board::Promote(int square, int pieceType, int color, bool isCapture) { SetPiece(pieceType, square, color); } -void Board::MakeMove(Move move) { - SEARCH::nodes++; - +void Board::MakeMove(Move move, bool legalityCheck) { + if (!legalityCheck) { + SEARCH::nodes++; + } // Null Move if (!move) { int newEpTarget = noEPTarget; @@ -357,6 +352,8 @@ void Board::MakeMove(Move move) { positionIndex++; positionHistory[positionIndex] = hashKey; + + MOVEGEN::GenThreatMaps(*this); } bool Board::InPossibleZug() { diff --git a/source/board.h b/source/board.h index eddadb6..11f9d12 100644 --- a/source/board.h +++ b/source/board.h @@ -53,7 +53,7 @@ public: void RemovePiece(int piece, int square, bool color); void Promote(int square, int pieceType, int color, bool isCapture); - void MakeMove(Move move); + void MakeMove(Move move, bool legalityCheck); bool InPossibleZug(); }; diff --git a/source/perft.cpp b/source/perft.cpp index 852c229..47e5706 100644 --- a/source/perft.cpp +++ b/source/perft.cpp @@ -12,7 +12,7 @@ static U64 HelperPerft(Board board, int depth) { for (int i = 0; i < board.currentMoveIndex; i++) { if (!board.IsLegal(board.moveList[i])) continue; Board copy = board; - copy.MakeMove(board.moveList[i]); + copy.MakeMove(board.moveList[i], false); nodes += HelperPerft(copy, depth - 1); positionIndex--; } @@ -28,7 +28,7 @@ void Perft(Board &board, int depth) { for (int i = 0; i < board.currentMoveIndex; i++) { if (!board.IsLegal(board.moveList[i])) continue; Board copy = board; - copy.MakeMove(board.moveList[i]); + copy.MakeMove(board.moveList[i], false); U64 nodeCount = HelperPerft(copy, depth - 1); positionIndex--; totalNodes += nodeCount; diff --git a/source/search.cpp b/source/search.cpp index 5ee046b..15e2178 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -140,7 +140,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) { if (!board.IsLegal(board.moveList[i])) continue; if (board.moveList[i].IsCapture()) { Board copy = board; - copy.MakeMove(board.moveList[i]); + copy.MakeMove(board.moveList[i], false); int score = -Quiescence(copy, -beta, -alpha, ply + 1).score; positionIndex--; @@ -201,7 +201,7 @@ 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()); + copy.MakeMove(Move(), false); doingNullMove = true; int score = -PVS(copy, depth - 3, -beta, -beta + 1, ply + 1).score; @@ -216,14 +216,6 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { MOVEGEN::GenerateMoves(board); - if (board.currentMoveIndex == 0) { - if (board.InCheck()) { // checkmate - return -99000 + ply; - } else { // stalemate - return 0; - } - } - SortMoves(board, ply); int score = -inf; @@ -239,7 +231,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { if (!board.IsLegal(currMove)) continue; Board copy = board; - copy.MakeMove(currMove); + copy.MakeMove(currMove, false); int reductions = GetReductions(board, currMove, depth, moveSeen, ply); @@ -291,6 +283,14 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { } } + if (moveSeen == 0) { + if (board.InCheck()) { // checkmate + return -99000 + ply; + } else { // stalemate + return 0; + } + } + TT.WriteEntry(board.hashKey, depth, results.score, nodeType, results.bestMove); if (searchStopped) return 0; return results; diff --git a/source/uci.cpp b/source/uci.cpp index 9d6e8a2..21c2f3f 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)); + board.MakeMove(UTILS::parseMove(board, move), false); } }