/// <summary> /// Simulates a fight between the specified wizard and opponent. /// </summary> /// <param name="wizard">The wizard.</param> /// <param name="opponent">The opponent.</param> /// <param name="difficulty">The difficulty to play with.</param> /// <param name="output">A delegate to a method to use to output messages.</param> /// <returns> /// The player which won the fight. /// </returns> internal static Player Fight(Wizard wizard, Player opponent, string difficulty, Action<string, object[]> output) { Player attacker = wizard; Player defender = opponent; bool isHardDifficulty = string.Equals("HARD", difficulty, StringComparison.OrdinalIgnoreCase); do { output("-- {0} turn --", new object[] { attacker.Name }); output("- {0} has {1} hit points, {2} armor, {3} mana", new object[] { wizard.Name, wizard.HitPoints, wizard.Armor, wizard.Mana }); output("- {0} has {1} hit points", new object[] { opponent.Name, opponent.HitPoints }); if (isHardDifficulty && attacker == wizard) { wizard.HitPoints--; if (wizard.HitPoints < 1) { break; } } wizard.CastSpells(opponent, output); if (attacker.HitPoints > 0) { attacker.Attack(defender, output); } var player = attacker; attacker = defender; defender = player; output(string.Empty, new object[0]); } while (wizard.HitPoints > 0 && opponent.HitPoints > 0); return wizard.HitPoints > 0 ? wizard : opponent; }
/// <summary> /// Simulates a fight between the specified wizard and opponent. /// </summary> /// <param name="wizard">The wizard.</param> /// <param name="opponent">The opponent.</param> /// <param name="difficulty">The difficulty to play with.</param> /// <param name="output">A delegate to a method to use to output messages.</param> /// <returns> /// The player which won the fight. /// </returns> internal static Player Fight(Wizard wizard, Player opponent, string difficulty, Action <string, object[]>?output) { Player attacker = wizard; Player defender = opponent; bool isHardDifficulty = string.Equals("HARD", difficulty, StringComparison.OrdinalIgnoreCase); do { output?.Invoke("-- {0} turn --", new object[] { attacker.Name }); output?.Invoke("- {0} has {1} hit points, {2} armor, {3} mana", new object[] { wizard.Name, wizard.HitPoints, wizard.Armor, wizard.Mana }); output?.Invoke("- {0} has {1} hit points", new object[] { opponent.Name, opponent.HitPoints }); if (isHardDifficulty && attacker == wizard) { wizard.HitPoints--; if (wizard.HitPoints < 1) { break; } } wizard.CastSpells(opponent, output); if (attacker.HitPoints > 0) { attacker.Attack(defender, output); } Player player = attacker; attacker = defender; defender = player; output?.Invoke(string.Empty, Array.Empty <object>()); }while (wizard.HitPoints > 0 && opponent.HitPoints > 0); return(wizard.HitPoints > 0 ? wizard : opponent); }