/// <summary> /// Simulates the battle between the attacking army and the defending army. /// </summary> /// <returns>A <see cref="BattleResult"/> representing the result of the battle.</returns> public BattleResult Simulate() { // Instantiate the list of round results. var rounds = new List <RoundResult>(); // Simulate battle rounds until: // 1) A winner has been declared (attacker, defender, or tie), or // 2) Neither side is able to hit the other, at which point the simulation is stopped with no winner declared. while (Winner() == BattleWinner.None) { // Simulate a battle round. var result = DoRound(); rounds.Add(result); // Check if neither side was able to hit the other. If so, stop the simulation. if (result.AttackerResult.TotalEffectiveHits == 0 && result.DefenderResult.TotalEffectiveHits == 0 && FireResult.Safe(result.AttackerSurpriseResult).TotalEffectiveHits == 0 && FireResult.Safe(result.DefenderSurpriseResult).TotalEffectiveHits == 0) { break; } } // Return an appropriate battle result. return(new BattleResult(rounds, Winner(), Attacker.Clone(), Defender.Clone())); }
/// <summary> /// Simulates a battle round. /// </summary> /// <returns>A <see cref="RoundResult"/> representing the result of the round.</returns> private RoundResult DoRound() { // Clone and store the armies for this round. var roundAttacker = Attacker.Clone(); var roundDefender = Defender.Clone(); // Clone the defending army so that it can make hits before causualties are taken. var tempDefender = Defender.Clone(); // Simulate the surprise strikes, capturing the results. var attackerSurpriseResult = SurpriseStrike(Attacker, Defender, true); var defenderSurpriseResult = SurpriseStrike(tempDefender, Attacker, false); // Clone the defending army so that it can make hits before causualties are taken. tempDefender = Defender.Clone(); // Simulate the general firing rounds, capturing the results. // Note that whether or not to fire the submarines depends upon whether or not the surprise strikes were executed. var attackerResult = Fire(Attacker, Defender, true, attackerSurpriseResult == null); var defenderResult = Fire(tempDefender, Attacker, false, defenderSurpriseResult == null); return(new RoundResult(roundAttacker, roundDefender, attackerSurpriseResult, defenderSurpriseResult, attackerResult, defenderResult)); }