コード例 #1
0
        /// <summary>
        /// Faster function to evaluate all the possible movements in the movement phase
        /// </summary>
        /// <param name="gm"></param>
        /// <returns> Best movement action</returns>
        private KeyValuePair <Double, PlayerAction> ThinkToMove(GameManager gm)
        {
            GameManager                toUse       = CreateGameManagerAndStuff(gm);
            Player                     me          = toUse.UserDict[CharacterEnum.AMARU].Player;
            LimitedList <Card>         myCards     = me.Hand;
            LimitedList <CreatureCard> myWarZone   = me.Outer;
            LimitedList <CreatureCard> myInnerZone = me.Inner;

            List <KeyValuePair <Double, PlayerAction> > listPossibleActions = new List <KeyValuePair <double, PlayerAction> >();

            foreach (CreatureCard cd in myWarZone.Concat(myInnerZone))
            {
                try
                {
                    //   Log("TESTO la carta "+ cd.Name + "   in war zone? " + myWarZone.Contains(cd));
                    //   Log(me.IsShieldMaidenProtected.ToString());
                    GameManager        toUseTemp   = CreateGameManagerAndStuff(toUse);
                    MoveCreatureAction myIntention = new MoveCreatureAction(CharacterEnum.AMARU, cd.Id, (myInnerZone.Contains(cd) ? Place.OUTER : Place.INNER), 0);
                    myIntention.Visit(toUseTemp.ValidationVisitor);
                    myIntention.Visit(toUseTemp.ExecutionVisitor);
                    Double valueOfGoal = ValueMyField(toUseTemp);
                    listPossibleActions.Add(new KeyValuePair <Double, PlayerAction>(valueOfGoal, myIntention));
                }
                catch (Exception e)
                {
                    Log("Eccezione " + e.ToString());
                    Log(cd.Name);
                }
            }
            listPossibleActions = listPossibleActions.OrderByDescending(x => x.Key).ToList();
            if (listPossibleActions.Count > 0)
            {
                Log(listPossibleActions[0].ToString());
                return(listPossibleActions[0]);
            }
            else
            {
                return(new KeyValuePair <double, PlayerAction>(Double.MinValue, new EndTurnAction(CharacterEnum.AMARU, -1, false)));
            }
        }
コード例 #2
0
ファイル: AIUser.cs プロジェクト: AndreaMilanta/AmaruServer
        /// <summary>
        /// Main Function: Iterate over all the possible action in the main turn, evaluating them with each possible target
        /// </summary>
        /// <param name="gm"></param>
        /// <returns> Returns the best action possible</returns>
        private KeyValuePair <Double, PlayerAction> Think(GameManager gm)
        {
            GameManager                toUse       = CreateGameManagerAndStuff(gm);
            Player                     me          = toUse.UserDict[CharacterEnum.AMARU].Player;
            LimitedList <Card>         myCards     = me.Hand;
            LimitedList <CreatureCard> myWarZone   = me.Outer;
            LimitedList <CreatureCard> myInnerZone = me.Inner;

            //Initializing all the actions doable
            List <KeyValuePair <Double, PlayerAction> > listPossibleActions = new List <KeyValuePair <double, PlayerAction> >();

            foreach (Card c in myCards)
            {
                try
                {
                    if (c.Cost > me.Mana)
                    {
                        continue;
                    }

                    if (c is CreatureCard && myWarZone.Count < AmaruConstants.OUTER_MAX_SIZE)
                    {
                        PlayACreatureFromHandAction myIntention = new PlayACreatureFromHandAction(CharacterEnum.AMARU, c.Id, Place.OUTER, Player.Outer.Count);
                        Double valueOfGoal = SimulateAndEvaluate(toUse, myIntention);
                        listPossibleActions.Add(new KeyValuePair <Double, PlayerAction>(valueOfGoal, myIntention));
                    }
                    else if (c is CreatureCard && myInnerZone.Count < AmaruConstants.INNER_MAX_SIZE)
                    {
                        PlayACreatureFromHandAction myIntention = new PlayACreatureFromHandAction(CharacterEnum.AMARU, c.Id, Place.INNER, Player.Inner.Count);
                        Double valueOfGoal = SimulateAndEvaluate(toUse, myIntention);
                        listPossibleActions.Add(new KeyValuePair <Double, PlayerAction>(valueOfGoal, myIntention));
                    }
                    else if (c is SpellCard)
                    {
                        //Amaru hasn't spell cards requiring targets
                        PlayASpellFromHandAction myIntention = new PlayASpellFromHandAction(CharacterEnum.AMARU, c.Id, null);
                        Double valueOfGoal = SimulateAndEvaluate(toUse, myIntention);
                        listPossibleActions.Add(new KeyValuePair <Double, PlayerAction>(valueOfGoal, myIntention));
                    }
                }
                catch (Exception e)
                {
                    Log("Eccezione " + e.ToString());
                    Log(c.Name);
                }
            }

            //Data Structure containing all the possible targets for an attack
            List <CardTarget>   allAcceptableTargets      = new List <CardTarget>();
            List <PlayerTarget> allAcceptablePlayerTarget = new List <PlayerTarget>();

            //Data Structure containing all the possible target for a generic ability
            List <Target> abilityTarget = new List <Target>();

            foreach (KeyValuePair <CharacterEnum, User> pair in toUse.UserDict.ToArray())
            {
                Player player = pair.Value.Player;
                foreach (CreatureCard cd in player.Outer)
                {
                    allAcceptableTargets.Add(new CardTarget(pair.Key, cd));
                    abilityTarget.Add(new CardTarget(pair.Key, cd));
                }
                foreach (CreatureCard cd in player.Inner)
                {
                    abilityTarget.Add(new CardTarget(pair.Key, cd));

                    if (!pair.Value.Player.IsShieldMaidenProtected)
                    {
                        allAcceptableTargets.Add(new CardTarget(pair.Key, cd));
                    }
                }
                if (!player.IsImmune)
                {
                    abilityTarget.Add(new PlayerTarget(pair.Key));
                    if (!player.IsShieldUpProtected)
                    {
                        allAcceptablePlayerTarget.Add(new PlayerTarget(pair.Key));
                    }
                }
            }

            //All the possible attacks
            foreach (CreatureCard c in myWarZone)
            {
                //some pruning
                if (c.Energy == 0 || c.Attack is null)
                {
                    continue;
                }

                foreach (CardTarget cTarget in allAcceptableTargets)
                {
                    try
                    {
                        AttackCreatureAction myIntention = new AttackCreatureAction(CharacterEnum.AMARU, c.Id, Property.ATTACK, cTarget);
                        Double valueOfGoal = SimulateAndEvaluate(toUse, myIntention);
                        listPossibleActions.Add(new KeyValuePair <Double, PlayerAction>(valueOfGoal, myIntention));
                    }
                    catch (Exception e)
                    {
                        Log("Eccezione " + e.ToString());
                        Log("ATTACCANTE " + c.Name);
                        Log(cTarget.Card.Name);
                    }
                }
                foreach (PlayerTarget pTarget in allAcceptablePlayerTarget)
                {
                    try
                    {
                        AttackPlayerAction myIntention = new AttackPlayerAction(CharacterEnum.AMARU, c.Id, Property.ATTACK, pTarget);
                        Double             valueOfGoal = SimulateAndEvaluate(toUse, myIntention);
                        listPossibleActions.Add(new KeyValuePair <Double, PlayerAction>(valueOfGoal, myIntention));
                    }
                    catch (Exception e)
                    {
                        Log("Eccezione Player" + e.ToString());
                        Log("ATTACCANTE " + c.Name);
                    }
                }
            }

            //All the possible abilities with their own specific targets combinations
            foreach (CreatureCard cd in myWarZone.Concat(myInnerZone))
            {
                //Some Pruning
                if (cd.Ability is null || cd.Ability.Cost > cd.Energy)
                {
                    continue;
                }

                int numTarget = cd.Ability.NumTarget;

                //Avoid searching for targets if numtarget == 0
                if (numTarget == 0)
                {
                    try
                    {
                        UseAbilityAction myIntention = new UseAbilityAction(CharacterEnum.AMARU, cd.Id, target: null);
                        Double           valueOfGoal = SimulateAndEvaluate(toUse, myIntention);
                        listPossibleActions.Add(new KeyValuePair <Double, PlayerAction>(valueOfGoal, myIntention));
                    }
                    catch (Exception e)
                    {
                        Log("Eccezione Abilità" + e.ToString());
                        Log(cd.Name);
                    }
                    continue;
                }

                //Looking for all the proper targets for the ability
                List <Target> targetDiscerned = new List <Target>();
                foreach (Target t in abilityTarget)
                {
                    targetVisitor.Target = t;
                    if (cd.Ability.Visit(targetVisitor) >= 0)
                    {
                        targetDiscerned.Add(t);
                    }
                }

                //I have to generate all the possible combinations of available targets. Following the rules of the game i have to do it without repetition.
                List <Combinations <Target> > targetsCombined = new List <Combinations <Target> >();
                for (int i = 1; i <= numTarget; i++)
                {
                    targetsCombined.Add(new Combinations <Target>(targetDiscerned, i, GenerateOption.WithoutRepetition));
                }

                //iterate all the possible combinations to evaluate the ability on that particular target
                foreach (List <Target> lt in targetsCombined.SelectMany(x => x))
                {
                    try
                    {
                        UseAbilityAction myIntention = new UseAbilityAction(CharacterEnum.AMARU, cd.Id, lt);
                        double           valueOfGoal = SimulateAndEvaluate(toUse, myIntention);
                        listPossibleActions.Add(new KeyValuePair <Double, PlayerAction>(valueOfGoal, myIntention));
                    }
                    catch (Exception e)
                    {
                        Log("Eccezione Player" + e.ToString());
                        Log(cd.Name);
                    }
                }
            }
            //order the possible actions depending on their value
            listPossibleActions = listPossibleActions.OrderByDescending(x => x.Key).ToList();
            if (listPossibleActions.Count > 0)
            {
//                Log("Best Choice");
//                Log(listPossibleActions[0].ToString());
                return(listPossibleActions[0]);
            }
            else
            {
                return(new KeyValuePair <double, PlayerAction>(Double.MinValue, new EndTurnAction(CharacterEnum.AMARU, -1, false)));
            }
        }