Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main(string[] args)
- {
- Battleground battleground = new Battleground();
- battleground.ConductBattle();
- }
- }
- public class Soldier
- {
- public int Health { get; protected set; }
- public int Damage { get; protected set; }
- public int Armor { get; protected set; }
- public bool IsAlive { get; protected set; }
- public void TakeDamage(int damage)
- {
- Health = Math.Max(0, Health - Math.Max(0, damage - Armor));
- if (Health <= 0)
- {
- IsAlive = false;
- }
- }
- public virtual void Attack(List<Soldier> enemySoldiers)
- {
- int minSoldiersCount = 0;
- if (enemySoldiers.Count > minSoldiersCount)
- {
- int targetIndex = UserUtils.GenerateRandom(enemySoldiers.Count - 1);
- Soldier enemy = enemySoldiers[targetIndex];
- enemy.TakeDamage(Damage);
- if (enemy.IsAlive == false)
- {
- Console.WriteLine($"Вражеский солдат {enemy.GetType().Name} убит!");
- }
- }
- }
- }
- public class Warrior : Soldier
- {
- public Warrior(int health, int damage, int armor)
- {
- Health = health;
- Damage = damage;
- Armor = armor;
- }
- }
- public class Juggernaut : Soldier
- {
- public Juggernaut(int health, int damage, int armor)
- {
- Health = health;
- Damage = damage;
- Armor = armor;
- }
- public override void Attack(List<Soldier> enemySoldiers)
- {
- Console.WriteLine("Атака нескольких вражеских солдат единожды...");
- int numAttacks = enemySoldiers.Count;
- while (numAttacks > 0)
- {
- int targetIndex = UserUtils.GenerateRandom(enemySoldiers.Count - 1);
- Soldier enemy = enemySoldiers[targetIndex];
- enemy.TakeDamage(Damage);
- if (enemy.IsAlive == false)
- {
- Console.WriteLine($"Вражеский солдат {enemy.GetType().Name} убит!");
- enemySoldiers.RemoveAt(targetIndex);
- }
- numAttacks--;
- }
- }
- }
- public class Assassin : Soldier
- {
- public Assassin(int health, int damage, int armor)
- {
- Health = health;
- Damage = damage;
- Armor = armor;
- }
- public override void Attack(List<Soldier> enemySoldiers)
- {
- Console.WriteLine("Атака нескольких вражеских солдат повторно...");
- foreach (Soldier enemy in enemySoldiers)
- {
- enemy.TakeDamage(Damage);
- }
- }
- }
- public class Squad
- {
- private readonly List<Soldier> _soldiers;
- public Squad(List<Soldier> soldiers)
- {
- _soldiers = soldiers;
- }
- public bool IsAlive => ActiveSoldiers.Count > 0;
- public void RemoveDeadSoldiers()
- {
- _soldiers.RemoveAll(soldier => soldier.Health <= 0);
- }
- public IReadOnlyList<Soldier> ActiveSoldiers => _soldiers.Where(soldier => soldier.Health > 0).ToList();
- }
- public class Battleground
- {
- private readonly Squad _firstSquad;
- private readonly Squad _secondSquad;
- private bool _battleIsOver;
- public Battleground()
- {
- _firstSquad = CreateSquad(new Soldier[]{
- new Warrior(100, 10, 5),
- new Warrior(100, 10, 5),
- new Juggernaut(100, 15, 5),
- new Assassin(100, 4, 10)});
- _secondSquad = CreateSquad(new Soldier[]{
- new Warrior(100, 10, 5),
- new Assassin(100, 4, 10),
- new Juggernaut(100, 4, 2),
- new Juggernaut(100, 4, 2)});
- }
- private Squad CreateSquad(Soldier[] soldiers)
- {
- return new Squad(soldiers.ToList());
- }
- public void ConductBattle()
- {
- while (_battleIsOver == false)
- {
- Console.WriteLine("Первый отряд атакует второй отряд...");
- AttackEnemySquad(_firstSquad, _secondSquad);
- Console.WriteLine("Второй отряд атакует первый отряд...");
- AttackEnemySquad(_secondSquad, _firstSquad);
- }
- DetermineWinner();
- }
- private void AttackEnemySquad(Squad attackingSquad, Squad defendingSquad)
- {
- IReadOnlyList<Soldier> activeSoldiers = attackingSquad.ActiveSoldiers;
- IReadOnlyList<Soldier> enemySoldiers = defendingSquad.ActiveSoldiers;
- foreach (Soldier soldier in activeSoldiers)
- {
- soldier.Attack(enemySoldiers.ToList());
- }
- defendingSquad.RemoveDeadSoldiers();
- CheckBattleStatus();
- }
- private void CheckBattleStatus()
- {
- if (_firstSquad.IsAlive == false || _secondSquad.IsAlive == false)
- {
- _battleIsOver = true;
- }
- }
- private void DetermineWinner()
- {
- if (_firstSquad.IsAlive)
- {
- Console.WriteLine("Первая армия выиграла!");
- }
- else if (_secondSquad.IsAlive)
- {
- Console.WriteLine("Вторая армия выиграла!");
- }
- else
- {
- Console.WriteLine("Ничья!");
- }
- }
- }
- class UserUtils
- {
- private static Random s_random = new Random();
- public static int GenerateRandom(int maxValue, int minValue = 0)
- {
- return s_random.Next(minValue, maxValue + 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement