Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.9 kB
1
Indexable
Never
public class FightingLoop
{
    public int RoundCounter { get; set; } = 0;

    private int maxRounds = 50;
    public void Fight(MonsterBase _monster1, MonsterBase _monster2)
    {
        while (_monster1.IsAlive() && _monster2.IsAlive() && RoundCounter <= maxRounds)  //Check, if both monsters are still alive, and prevents an infinite fight by setting a round limit
        {
            if (_monster1.isFaster(_monster2))   // If monster1 is faster than monster2, it attacks first. If not, monster2 attacks even with equal speed first. 
            {
                _monster1.Attack(_monster2);
                _monster2.Attack(_monster1);
                RoundCounter++;
            }
            else
            {
                _monster2.Attack(_monster1);
                _monster1.Attack(_monster2);
                RoundCounter++;
            }
        }

        FightResults(_monster1, _monster2);
    }

    private void FightResults(MonsterBase _monster1, MonsterBase _monster2)   //Checks which monster is dead and prints the winner and the rounds it needed to win. Also resets round counter for next fight.
    {
        Console.Clear();
        if (!_monster1.IsAlive())
        {
            Console.WriteLine($"The {_monster2.Name} wins after {RoundCounter} rounds!");
            ResetRoundCounter();
        }
        else if (!_monster2.IsAlive())
        {
            Console.WriteLine($"The {_monster1.Name} wins after {RoundCounter} rounds!");
            ResetRoundCounter();
        }
        else
        {
            Console.WriteLine($"The fight ended in a tie after {RoundCounter} rounds!");
            ResetRoundCounter();
        }
    }

    private void ResetRoundCounter()   // Resets round counter for the next fight
    {
        RoundCounter = 0;
    }
}
Leave a Comment