private static void Perft(BoardState boardState, int depth, AdvancedPerftResult result) { Span <Move> moves = stackalloc Move[SearchConstants.MaxMovesCount]; var movesCount = boardState.GetAllMoves(moves); if (depth <= 0) { result.Leafs++; return; } if (depth == 1) { UpdateResult(boardState, moves, movesCount, result); return; } for (var i = 0; i < movesCount; i++) { boardState.MakeMove(moves[i]); if (!boardState.IsKingChecked(ColorOperations.Invert(boardState.ColorToMove))) { Perft(boardState, depth - 1, result); } boardState.UndoMove(moves[i]); } }
public static AdvancedPerftResult Run(BoardState boardState, int depth) { var result = new AdvancedPerftResult(); var stopwatch = Stopwatch.StartNew(); Perft(boardState, depth, result); result.Time = stopwatch.Elapsed.TotalSeconds; return(result); }
private static void UpdateResult(BoardState boardState, Span <Move> moves, int movesCount, AdvancedPerftResult result) { var legalMoveFound = false; for (var i = 0; i < movesCount; i++) { boardState.MakeMove(moves[i]); if (!boardState.IsKingChecked(ColorOperations.Invert(boardState.ColorToMove))) { if (moves[i].IsCapture()) { result.Captures++; } if (moves[i].Flags == MoveFlags.KingCastle || moves[i].Flags == MoveFlags.QueenCastle) { result.Castles++; } if (moves[i].Flags == MoveFlags.EnPassant) { result.EnPassants++; result.Captures++; } if (boardState.IsKingChecked(boardState.ColorToMove)) { result.Checks++; } result.Leafs++; legalMoveFound = true; } boardState.UndoMove(moves[i]); } if (!legalMoveFound) { result.Checkmates++; } }