private static int Minimax(GameNode node, int depth, bool maximizingPlayer) { if (depth == 0 || node.IsTerminal) { return(node.GetHeuristic()); } if (maximizingPlayer) { var bestValue = int.MinValue; foreach (var child in node.Children.Keys) { var value = Minimax(child, depth - 1, !maximizingPlayer); bestValue = Max(bestValue, value); } node.SetHeuristic(bestValue); return(bestValue); } else { var bestValue = int.MaxValue; foreach (var child in node.Children.Keys) { var value = Minimax(child, depth - 1, !maximizingPlayer); bestValue = Min(bestValue, value); } node.SetHeuristic(bestValue); return(bestValue); } }