Example #1
0
        public static int GetBestMove(int player, int alpha, int deep, Game game)
        {
            if (deep > MAX_DEEP || game.ComputerScore + game.PlayerScore == Game.SIZE * Game.SIZE)
            {
                //игра или глубина окончена, считаем оценку
                return game.ComputeRating();
            }
            List<int[]> possibleMoves = game.GetPossibleMoves(player);

            //если у компьютера нет ходов, то все плохо
            if (possibleMoves.Count == 0 && player == COMPUTER)
                return -Int32.MaxValue;

            int maxRating = -Int32.MaxValue;
            foreach (int[] move in possibleMoves)
            {
                //выполняем отсечение
                if (AlphaBeta(alpha, maxRating, deep))
                {
                    return alpha;
                }
                //создаем копию игры и делаем ход
                Game extraGame = game.Copy();
                extraGame.MakeMove(move[1], move[0], player, true);

                //вычисляем оценку после этого хода, меняя игрока
                int result = GetBestMove(-player, alpha, deep + 1, extraGame);

                //вычисляем новую оценку
                maxRating = MinMax(result, maxRating, deep);
            }
            return maxRating;
        }