public override void Visit(PlayASpellFromHandAction action) { GameManager.PlayersAliveBeforeAction = GameManager.UserDict.Keys.ToList().Where(c => GameManager.GetPlayer(c).IsAlive).ToList(); OnCardPlayedVisitor visitor = new OnCardPlayedVisitor(GameManager); Player p = GameManager.GetPlayer(action.Caller); SpellCard spell = p.PlayASpellFromHand(action.PlayedCardId); visitor.Targets = action.Targets; p.PlayedSpell.Add(spell); spell.Visit(visitor, p.Character); foreach (CharacterEnum target in GameManager.UserDict.Keys.ToList()) { GameManager.UserDict[target].Write(new ResponseMessage(new PlayASpellResponse(action.Caller, spell, action.Targets))); } foreach (KeyValuePair <CharacterEnum, Response> kvp in visitor.SuccessiveResponse) { GameManager.UserDict[kvp.Key].Write(new ResponseMessage(kvp.Value)); } // visitor must take care of players which he kills foreach (CharacterEnum ch in GameManager.PlayersAliveBeforeAction) { if (!GameManager.GetPlayer(ch).IsAlive) { GameManager.KillPlayer(p.Character, ch); } } }
public override void Visit(PlayASpellFromHandAction action) { // Ignore check if caller not AMARU // TODO: Remove if (action.Caller != CharacterEnum.AMARU) { return; } // 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 (cardPlaying.Cost > caller.Mana) { throw new NotEnoughManaAvailableException(); } //check if the Card is a Creature if (!(cardPlaying is SpellCard)) { throw new InvalidCardTypeException(); } //Check if target is alive, if the spell has a target or more than one target List <Target> target = action.Targets; SpellAbility effect = ((SpellCard)cardPlaying).Effect; int numTarget = effect.NumTarget; KindOfTarget acceptableTypeOfTarget = effect.kindOfTarget; if (target is null) { return; } if (numTarget != 0 || target.Count > numTarget) { //Check targets are not immune, and that the right number of target has been chosen. BUT it depends on the card!ù throw new InvalidTargetException(); } foreach (Target t in target) { if (t is PlayerTarget && acceptableTypeOfTarget != KindOfTarget.PLAYER && acceptableTypeOfTarget != KindOfTarget.MIXED) { throw new InvalidTargetException(); } if (t is CardTarget && acceptableTypeOfTarget != KindOfTarget.MIXED && acceptableTypeOfTarget != KindOfTarget.CREATURE) { throw new InvalidTargetException(); } if (t is PlayerTarget && GameManager.UserDict[((PlayerTarget)t).Character].Player.IsImmune) { throw new InvalidTargetException(); } if (t is CardTarget) { CardTarget cardTarget = (CardTarget)t; Card cardOuter = GameManager.UserDict[((CardTarget)t).Character].Player.GetCardFromId(cardTarget.CardId, Place.OUTER); Card cardInner = GameManager.UserDict[((CardTarget)t).Character].Player.GetCardFromId(cardTarget.CardId, Place.INNER); if (cardOuter != null && cardOuter is CreatureCard) { if (((CreatureCard)cardOuter).creatureEffect is ImmunityCreatureEffect) { throw new InvalidTargetException(); } } if (cardInner != null && cardInner is CreatureCard) { if (((CreatureCard)cardInner).creatureEffect is ImmunityCreatureEffect) { throw new InvalidTargetException(); } } } }//*/ }
/// <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))); } }