public int Minimax(int depth, int turn, int alpha, int beta, int player) // turn - 1 for max, 2 for min { if (beta <= alpha) { return(turn == 1 ? int.MaxValue : int.MinValue); } var gameResult = GameResult(_b); switch (gameResult) { case 1: return(int.MaxValue / 2); case 2: return(int.MinValue / 2); case 0: return(0); } if (depth == _maxDepth) { return(EvaluateBoard(_b, player)); } int maxScore = int.MinValue, minScore = int.MaxValue; for (var j = 0; j <= 6; ++j) { var currentScore = 0; if (!_b.IsLegalMove(j)) { continue; } if (turn == 1) { _b.PlaceMove(j, 1); currentScore = Minimax(depth + 1, 2, alpha, beta, player); if (depth == 0) { if (currentScore > maxScore) { _nextMoveLocation = j; } if (currentScore == int.MaxValue / 2) { _b.UndoMove(j); break; } } maxScore = Math.Max(currentScore, maxScore); alpha = Math.Max(currentScore, alpha); } else if (turn == 2) { _b.PlaceMove(j, 2); currentScore = Minimax(depth + 1, 1, alpha, beta, player); minScore = Math.Min(currentScore, minScore); beta = Math.Min(currentScore, beta); } _b.UndoMove(j); if (currentScore == int.MaxValue || currentScore == int.MinValue) { break; } } return(turn == 1 ? maxScore : minScore); }