/// <summary> /// Event handler for defender's Defend event. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// <exception cref="InvalidOperationException">The chosen card is not a valid defense for the current game state.</exception> private void OnNewDefend(object sender, GameActionEventArgs e) { if ((sender as Player).Hand[e.Action].CanDefendAgainst(AttackCardsPlayed.Last(), this.Game.TrumpSuit)) { // Add the defense card to the pile. DefenseCardsPlayed.Add((sender as Player).Hand[e.Action]); } else { throw new InvalidOperationException((sender as Player).Hand[e.Action] + " is not a valid defense!"); } }
/// <summary> /// Progresses the game loop within the bout. /// </summary> public virtual void Continue() { // Decide whose turn it is, if not already decided if (ActingPlayer == null) { // Attacker's "turn". if (AttackCardsPlayed.Count <= DefenseCardsPlayed.Count) { ActingPlayer = Attacker; OnReport(new GameLogEventArgs(string.Format("Attacker's turn - {0}", Attacker.Name))); // Rebuild AI player decision matrix based on current game state if (Attacker is AIPlayer) { (Attacker as AIPlayer).InitializeMatrix(AttackCardsPlayed.Count == 0); // The player MUST play at least one attack List <int> validAttacks = new List <int>(); foreach (Card card in Attacker.Hand) { if (IsValidAttack(card)) { validAttacks.Add(Attacker.Hand.IndexOf(card)); } } (Attacker as AIPlayer).AddDecisionsToMatrix(validAttacks.ToArray()); } // Prompt attacker to select an option. //Attacker.PromptAction(); } // Defender's "turn". else { ActingPlayer = Defender; OnReport(new GameLogEventArgs(string.Format("Defender's turn - {0}", Defender.Name))); // Rebuild AI player decision matrix based on current game state if (Defender is AIPlayer) { (Defender as AIPlayer).InitializeMatrix(); List <int> validDefends = new List <int>(); foreach (Card card in Defender.Hand) { if (card.CanDefendAgainst(AttackCardsPlayed.Last(), this.Game.TrumpSuit)) { validDefends.Add(Defender.Hand.IndexOf(card)); } } (Defender as AIPlayer).AddDecisionsToMatrix(validDefends.ToArray()); } //Defender.PromptAction(); } } // If the acting player was already known, prompt them to act. else { ActingPlayer.PromptAction(); ActingPlayer = null; } // Check if "end of bout" conditions have been met // If the defender has defended up to the maximum number of attacks, they win if (DefenseCardsPlayed.Count >= MaximumAttacks) { Winner = Defender; } // If a winner has been declared, the bout is concluded. if (Winner != null) { OnEnd(new GameLogEventArgs(string.Format("{0} won the bout!", Winner.Name))); // If the defender lost, they have to pick up all of the cards. if (Winner == Attacker) { Defender.TakeCards(AttackCardsPlayed.Union(DefenseCardsPlayed).ToArray(), "the table"); } // Remove all cards from the table. AttackCardsPlayed.Clear(); DefenseCardsPlayed.Clear(); } }