/// <summary> /// Prompt the player to take action. The logic for what "prompting" the player means must be defined in the external program. /// </summary> public override void PromptAction() { GameActionEventArgs choice = new GameActionEventArgs(); // Get input from human player OnAcceptInput(choice); // Use the same GameActionEventArgs when moving to the prompt, to maintain the Action value OnPrompt(choice); }
/// <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> /// Event handler for attacker's Attack event. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// <exception cref="InvalidOperationException">The chosen card is not a valid attack for the current game state.</exception> private void OnNewAttack(object sender, GameActionEventArgs e) { if (IsValidAttack((sender as Player).Hand[e.Action])) { // Add the attack card to the pile. AttackCardsPlayed.Add((sender as Player).Hand[e.Action]); } else { throw new InvalidOperationException((sender as Player).Hand[e.Action] + " is not a valid attack!"); } }
/// <summary> /// Event handler for when the defender is prompted to act. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// <exception cref="ArgumentException">The defender attempts to make an illegal defense.</exception> /// <exception cref="ArgumentOutOfRangeException">The defender attempts to select a card index that does not exist in their hand.</exception> private void OnPromptDefender(object sender, GameActionEventArgs e) { if (e.Action == Player.NO_ACTION) { (sender as Player).GiveUpDefense(); } else if (e.Action >= 0 && e.Action < (sender as Player).Hand.Count) { if (IsValidDefense((sender as Player).Hand[e.Action])) { (sender as Player).DefendWith(e.Action); } else { throw new ArgumentException(string.Format("Cannot defend with the {0}!", (sender as Player).Hand[e.Action].ToString())); } } else { throw new ArgumentOutOfRangeException(string.Format("Invalid hand index {0} for player {1}.", e.Action, (sender as Player).Name)); } }
protected virtual void OnAcceptInput(GameActionEventArgs a) { AcceptInput?.Invoke(this, a); }
static void _PlayerInput(object sender, GameActionEventArgs e) { Console.WriteLine("Please select an action:"); Console.WriteLine("\t0 - No action"); for (int i = 1; i <= (sender as Player).Hand.Count; i++) { Console.WriteLine("\t{0} - {1}", i, (sender as Player).Hand[i - 1]); } int action = 0; bool validInput = false; do { Console.Write("\nEnter input: "); string playerChoice = Console.ReadLine(); try { action = int.Parse(playerChoice); if (action < 0 || action > (sender as Player).Hand.Count) { throw new ArgumentOutOfRangeException(string.Format("Action must be between 0 and {0}.", (sender as Player).Hand.Count)); } if (action == 0) { // First attack is mandatory if (testGame_Human.CurrentBout.AttackCardsPlayed.Count == 0) { validInput = false; } else { validInput = true; } } else if (sender == testGame_Human.CurrentBout.Attacker) { if (testGame_Human.CurrentBout.IsValidAttack((sender as Player).Hand[action - 1])) { validInput = true; } } else if (sender == testGame_Human.CurrentBout.Defender) { if (testGame_Human.CurrentBout.IsValidDefense((sender as Player).Hand[action - 1])) { validInput = true; } } // Player-facing error prompts if (!validInput) { if (action == 0) { Console.WriteLine("You must attack!"); } else { Console.WriteLine("The {0} is not a valid {1}!", (sender as Player).Hand[action - 1], (sender == testGame_Human.CurrentBout.Attacker ? "attack" : "defense")); } } } catch (ArgumentOutOfRangeException oor) { Console.Error.WriteLine(oor.Message); } catch (FormatException) { Console.Error.WriteLine("Invalid input."); } catch (ArgumentException a) { Console.Error.WriteLine(a.Message); } } while (!validInput); // Once we've received valid input, write it to the Action property of the event args, and the library will handle the rest // NOTE: we need to subtract 1 in this case, since Hand is zero-based and "NO ACTION" is represented with -1 e.Action = action - 1; }
protected virtual void OnDefend(GameActionEventArgs a, GameLogEventArgs l) { Defend?.Invoke(this, a); DefendLog?.Invoke(this, l); }
protected virtual void OnAttack(GameActionEventArgs a, GameLogEventArgs l) { Attack?.Invoke(this, a); AttackLog?.Invoke(this, l); }
protected virtual void OnPrompt(GameActionEventArgs a) { Prompt?.Invoke(this, a); }