Пример #1
0
        public int MiniMax(GameState gs, int depth, Boolean maxPlayer)
        {
            //base case
            if (depth == 0 || gs.GetState().Equals(GameState.State.terminal))
            {
                return FindHeuristicValue(gs);
            }

            if(maxPlayer)
            {
                int bestValue = MIN_VALUE;

                foreach(GameState child in gs.GetChildren())
                {
                    int value = MiniMax(child, depth - 1, false);

                    if (value.CompareTo(bestValue) > 0)
                    {
                        gs.SetHeuristicValue(value);
                        bestValue = value;
                    }
                }

                return bestValue;
            }
            else
            {
                int bestValue = MAX_VALUE;

                foreach (GameState child in gs.GetChildren())
                {
                    int value = MiniMax(child, depth - 1, true);

                    if(value.CompareTo(bestValue) < 0)
                    {
                        gs.SetHeuristicValue(value);
                        bestValue = value;
                    }
                }

                return bestValue;
            }
        }