private async Task <double> Negamax(NegamaxContext context, double alpha, double beta, int depth, int color, CancellationToken aiCancelToken) { if (depth == 0 || !context.Board.IsPlayable) { if (context.ScoreCalculated) { return(context.Score * color); } else { return(_analyzer.Analyze(context.Board).player1Advantage *color); } } IEnumerable <NegamaxContext> orderedAnalysis = GetSortedMoves(context.Board, color, aiCancelToken); double bestScore = MinValue; foreach (var nextContext in orderedAnalysis) { double score = -(await Negamax(nextContext, -beta, -alpha, depth - 1, -color, aiCancelToken)); bestScore = Math.Max(bestScore, score); alpha = Math.Max(alpha, score); if (alpha >= beta) { break; } if (aiCancelToken.IsCancellationRequested) { return(bestScore); } } ; return(bestScore); }