public override void Visit(PlayACreatureFromHandAction action) { // Check caller player is alive and it is its main turn Player caller = this.GameManager.GetPlayer(action.Caller); if (!caller.IsAlive || GameManager.ActiveCharacter != action.Caller || !GameManager.IsMainTurn) { throw new CallerCannotPlayException(); } //Check if caller player has enough CPs to play the card Card cardPlaying = caller.GetCardFromId(action.PlayedCardId, Place.HAND); if (((CreatureCard)cardPlaying).creatureEffect is CostLessForPDEffect) { int PDcount = 0; foreach (User u in GameManager.UserDict.Values) { foreach (CreatureCard c in u.Player.Inner) { PDcount += c.PoisonDamage; } foreach (CreatureCard c in u.Player.Outer) { PDcount += c.PoisonDamage; } } cardPlaying.Cost -= PDcount; if (cardPlaying.Cost < 0) { cardPlaying.Cost = 0; } } Log(action.Caller + " played " + cardPlaying.Name); if (cardPlaying.Cost > caller.Mana) { throw new NotEnoughManaAvailableException(); } //check if the Card is a Creature if (!(cardPlaying is CreatureCard)) { throw new InvalidCardTypeException(); } //Check that target place has enough room if ((action.Place == Place.INNER && caller.Inner.Count >= AmaruConstants.INNER_MAX_SIZE) || (action.Place == Place.OUTER && caller.Outer.Count >= AmaruConstants.OUTER_MAX_SIZE)) { throw new TargetPlaceFullException(action.Place); } }
public override void Visit(PlayACreatureFromHandAction action) { OnCardPlayedVisitor visitor = new OnCardPlayedVisitor(GameManager); Player p = GameManager.GetPlayer(action.Caller); CreatureCard creature = p.PlayACreatureFromHand(action.PlayedCardId, action.Place); creature.Visit(visitor, p.Character); foreach (CharacterEnum target in GameManager.UserDict.Keys.ToList()) { GameManager.UserDict[target].Write(new ResponseMessage(new PlayACreatureResponse(action.Caller, creature, action.Place, action.TablePos))); } }
/// <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))); } }