Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Home_Work
- {
- class Program
- {
- static void Main()
- {
- War war = new War();
- war.Battle();
- Console.ReadKey();
- }
- }
- class War
- {
- private Squad _firstSquad;
- private Squad _secondSquad;
- public War()
- {
- _firstSquad = new Squad("Дельта");
- _secondSquad = new Squad("GIGN");
- }
- public void Battle()
- {
- Fight();
- ShowWinner();
- Console.ReadKey();
- }
- private void ShowInfo()
- {
- int leftPosition = 0;
- int rightPosition = 50;
- _firstSquad.ShowInfo(leftPosition);
- _secondSquad.ShowInfo(rightPosition);
- }
- private void Fight()
- {
- int topPosition = GetMaxCountSoilders() + 1;
- while (_firstSquad.IsAlive == true && _secondSquad.IsAlive == true)
- {
- ShowInfo();
- Console.SetCursorPosition(0, topPosition);
- MakeAttack(_firstSquad, _secondSquad);
- MakeAttack(_secondSquad, _firstSquad);
- Console.ReadKey();
- Console.Clear();
- }
- }
- private void MakeAttack(Squad attackSquad, Squad defendingSquad)
- {
- if (attackSquad.IsAlive == true)
- {
- Soilder soilder = attackSquad.GetRandomSoilder();
- List<Soilder> targets = soilder.ChooseTargets(defendingSquad.Soilders);
- soilder.Attack(targets);
- defendingSquad.RemoveDeadSoilders();
- }
- }
- private void ShowWinner()
- {
- if (_firstSquad.IsAlive == false && _secondSquad.IsAlive == false)
- {
- Console.WriteLine("Ниья.");
- }
- else if (_firstSquad.IsAlive == false)
- {
- Console.WriteLine($"Победли отряд {_firstSquad.Name}.");
- }
- else if (_secondSquad.IsAlive == false)
- {
- Console.WriteLine($"Победли отряд {_secondSquad.Name}.");
- }
- }
- private int GetMaxCountSoilders()
- {
- int maxCount = _firstSquad.CountSoilders;
- if (_firstSquad.CountSoilders < _secondSquad.CountSoilders)
- {
- maxCount = _secondSquad.CountSoilders;
- }
- return maxCount;
- }
- }
- class Squad
- {
- private static readonly Random _random;
- private static readonly int _maxCountSoilder;
- private List<Soilder> _soilders;
- private string _name;
- static Squad()
- {
- _random = new Random();
- _maxCountSoilder = 5;
- }
- public Squad(string name)
- {
- _soilders = new List<Soilder>();
- Form();
- _name = name;
- }
- public IReadOnlyList<Soilder> Soilders => _soilders;
- public bool IsAlive => _soilders.Count > 0;
- public string Name => _name;
- public int CountSoilders => _soilders.Count;
- public void ShowInfo(int leftPosition)
- {
- int topPosition = 0;
- foreach (Soilder soilder in _soilders)
- {
- Console.SetCursorPosition(leftPosition, topPosition++);
- soilder.ShowInfo();
- }
- }
- public Soilder GetRandomSoilder()
- {
- return _soilders[_random.Next(_soilders.Count)];
- }
- public void RemoveDeadSoilders()
- {
- for(int i = 0; i < _soilders.Count; i++)
- {
- if (_soilders[i].IsAlive == false)
- {
- _soilders.RemoveAt(i);
- i--;
- }
- }
- }
- private void Form()
- {
- for (int i = 0; i < _maxCountSoilder; i++)
- {
- List<Soilder> soilders = new List<Soilder>();
- soilders.Add(new Commando());
- soilders.Add(new Sniper());
- soilders.Add(new RocketLauncher());
- soilders.Add(new AssaultMan());
- int index = _random.Next(soilders.Count);
- _soilders.Add(soilders[index]);
- }
- }
- }
- abstract class Soilder
- {
- protected static readonly Random Random;
- protected static readonly int PresetMaximumPercentage;
- static Soilder()
- {
- Random = new Random();
- PresetMaximumPercentage = 101;
- }
- public string TypeName { get; protected set; }
- public int Damage { get; protected set; }
- public int Armor { get; protected set; }
- public int ChanceDodge { get; protected set; }
- public double Health { get; protected set; }
- public int CountTarget { get; protected set; }
- public string Name => TypeName;
- public bool IsAlive => Health > 0;
- public void TakeDamage(double damage)
- {
- if (ChanceDodge > Random.Next(PresetMaximumPercentage))
- {
- Console.WriteLine($"{TypeName} уклонился от атаки.");
- }
- else
- {
- double realDamage = Math.Round(damage / Armor, 2);
- Health -= realDamage;
- Console.WriteLine($"{TypeName} получает {realDamage} урона.");
- }
- }
- public void Attack(List<Soilder> targets)
- {
- Console.Write($"{TypeName} атакует: ");
- foreach(Soilder soilder in targets)
- {
- Console.Write(soilder.Name + " ");
- }
- Console.WriteLine();
- for (int i = 0; i < CountTarget; i++)
- {
- targets[i].TakeDamage(Damage);
- }
- Console.WriteLine();
- }
- public void ShowInfo()
- {
- Console.WriteLine($"{TypeName}. Здоровье: {Health}.");
- }
- public virtual List<Soilder> ChooseTargets(IReadOnlyList<Soilder> enemySoilders)
- {
- List<Soilder> targets = new List<Soilder>();
- for (int i = 0; i < CountTarget; i++)
- {
- targets.Add(enemySoilders[Random.Next(enemySoilders.Count)]);
- }
- return targets;
- }
- }
- class Commando : Soilder
- {
- public Commando() : base()
- {
- TypeName = "Спецназовец";
- Damage = 20;
- Armor = 3;
- ChanceDodge = 80;
- CountTarget = 3;
- Health = 80;
- }
- public override List<Soilder> ChooseTargets(IReadOnlyList<Soilder> enemySoilders)
- {
- List<Soilder> targets = new List<Soilder>();
- for (int i = 0; i < CountTarget; i++)
- {
- targets.Add(enemySoilders[Random.Next(enemySoilders.Count)]);
- }
- return targets;
- }
- }
- class RocketLauncher : Soilder
- {
- public RocketLauncher() : base()
- {
- TypeName = "Гранатомётчик";
- Damage = 50;
- Armor = 1;
- ChanceDodge = 20;
- CountTarget = 5;
- Health = 60;
- }
- public override List<Soilder> ChooseTargets(IReadOnlyList<Soilder> enemySoilders)
- {
- List<Soilder> targets = new List<Soilder>();
- for (int i = 0; i < CountTarget && i < enemySoilders.Count; i++)
- {
- targets.Add(enemySoilders[i]);
- }
- return targets;
- }
- }
- class AssaultMan : Soilder
- {
- public AssaultMan() : base()
- {
- TypeName = "Штурмовик";
- Damage = 40;
- Armor = 6;
- ChanceDodge = 50;
- CountTarget = 2;
- Health = 100;
- }
- public override List<Soilder> ChooseTargets(IReadOnlyList<Soilder> enemySoilders)
- {
- List<Soilder> targets = new List<Soilder>();
- Soilder target = enemySoilders[Random.Next(enemySoilders.Count)];
- for (int i = 0; i < CountTarget; i++)
- {
- targets.Add(target);
- }
- return targets;
- }
- }
- class Sniper : Soilder
- {
- public Sniper() : base()
- {
- TypeName = "Снайпер";
- Damage = 100;
- Armor = 2;
- ChanceDodge = 20;
- CountTarget = 1;
- Health = 70;
- }
- }
- }
Add Comment
Please, Sign In to add comment