RedFlys

Home work - war

Jan 14th, 2022 (edited)
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Home_Work
  8. {
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             War war = new War();
  14.             war.Battle();
  15.  
  16.             Console.ReadKey();
  17.         }
  18.     }
  19.  
  20.     class War
  21.     {
  22.         private Squad _firstSquad;
  23.         private Squad _secondSquad;
  24.  
  25.         public War()
  26.         {
  27.             _firstSquad = new Squad("Дельта");
  28.             _secondSquad = new Squad("GIGN");
  29.         }
  30.        
  31.         public void Battle()
  32.         {            
  33.             Fight();
  34.             ShowWinner();
  35.  
  36.             Console.ReadKey();
  37.         }
  38.  
  39.         private void ShowInfo()
  40.         {
  41.             int leftPosition = 0;
  42.             int rightPosition = 50;
  43.  
  44.             _firstSquad.ShowInfo(leftPosition);
  45.             _secondSquad.ShowInfo(rightPosition);
  46.         }
  47.  
  48.         private void Fight()
  49.         {
  50.             int topPosition = GetMaxCountSoilders() + 1;
  51.  
  52.             while (_firstSquad.IsAlive == true && _secondSquad.IsAlive == true)
  53.             {
  54.                 ShowInfo();
  55.                 Console.SetCursorPosition(0, topPosition);
  56.  
  57.                 MakeAttack(_firstSquad, _secondSquad);
  58.                 MakeAttack(_secondSquad, _firstSquad);
  59.  
  60.                 Console.ReadKey();
  61.                 Console.Clear();
  62.             }
  63.         }
  64.  
  65.         private void MakeAttack(Squad attackSquad, Squad defendingSquad)
  66.         {
  67.             if (attackSquad.IsAlive == true)
  68.             {
  69.                 Soilder soilder = attackSquad.GetRandomSoilder();
  70.                 List<Soilder> targets = soilder.ChooseTargets(defendingSquad.Soilders);
  71.  
  72.                 soilder.Attack(targets);
  73.                 defendingSquad.RemoveDeadSoilders();
  74.             }
  75.         }
  76.  
  77.         private void ShowWinner()
  78.         {
  79.             if (_firstSquad.IsAlive == false && _secondSquad.IsAlive == false)
  80.             {
  81.                 Console.WriteLine("Ниья.");
  82.             }
  83.             else if (_firstSquad.IsAlive == false)
  84.             {
  85.                 Console.WriteLine($"Победли отряд {_firstSquad.Name}.");
  86.             }
  87.             else if (_secondSquad.IsAlive == false)
  88.             {
  89.                 Console.WriteLine($"Победли отряд {_secondSquad.Name}.");
  90.             }
  91.         }
  92.  
  93.         private int GetMaxCountSoilders()
  94.         {
  95.             int maxCount = _firstSquad.CountSoilders;
  96.  
  97.             if (_firstSquad.CountSoilders < _secondSquad.CountSoilders)
  98.             {
  99.                 maxCount = _secondSquad.CountSoilders;
  100.             }
  101.  
  102.             return maxCount;
  103.         }
  104.     }
  105.  
  106.     class Squad
  107.     {
  108.         private static readonly Random _random;
  109.         private static readonly int _maxCountSoilder;
  110.         private List<Soilder> _soilders;
  111.         private string _name;
  112.  
  113.         static Squad()
  114.         {
  115.             _random = new Random();
  116.             _maxCountSoilder = 5;
  117.         }
  118.  
  119.         public Squad(string name)
  120.         {
  121.             _soilders = new List<Soilder>();
  122.             Form();
  123.             _name = name;
  124.         }
  125.  
  126.         public IReadOnlyList<Soilder> Soilders => _soilders;
  127.         public bool IsAlive => _soilders.Count > 0;
  128.         public string Name => _name;
  129.         public int CountSoilders => _soilders.Count;
  130.  
  131.         public void ShowInfo(int leftPosition)
  132.         {
  133.             int topPosition = 0;
  134.  
  135.             foreach (Soilder soilder in _soilders)
  136.             {
  137.                 Console.SetCursorPosition(leftPosition, topPosition++);
  138.                 soilder.ShowInfo();
  139.             }
  140.         }
  141.  
  142.         public Soilder GetRandomSoilder()
  143.         {
  144.             return _soilders[_random.Next(_soilders.Count)];
  145.         }
  146.  
  147.         public void RemoveDeadSoilders()
  148.         {
  149.             for(int i = 0; i < _soilders.Count; i++)
  150.             {
  151.                 if (_soilders[i].IsAlive == false)
  152.                 {
  153.                     _soilders.RemoveAt(i);
  154.  
  155.                     i--;
  156.                 }
  157.             }
  158.         }                  
  159.  
  160.         private void Form()
  161.         {
  162.             for (int i = 0; i < _maxCountSoilder; i++)
  163.             {
  164.                 List<Soilder> soilders = new List<Soilder>();
  165.                 soilders.Add(new Commando());
  166.                 soilders.Add(new Sniper());
  167.                 soilders.Add(new RocketLauncher());
  168.                 soilders.Add(new AssaultMan());
  169.  
  170.                 int index = _random.Next(soilders.Count);
  171.                 _soilders.Add(soilders[index]);
  172.             }
  173.         }
  174.     }
  175.  
  176.     abstract class Soilder
  177.     {
  178.         protected static readonly Random Random;
  179.         protected static readonly int PresetMaximumPercentage;                    
  180.  
  181.         static Soilder()
  182.         {
  183.             Random = new Random();
  184.             PresetMaximumPercentage = 101;
  185.         }
  186.  
  187.         public string TypeName { get; protected set; }
  188.         public int Damage { get; protected set; }
  189.         public int Armor { get; protected set; }
  190.         public int ChanceDodge { get; protected set; }
  191.         public double Health { get; protected set; }
  192.         public int CountTarget { get; protected set; }
  193.         public string Name => TypeName;        
  194.         public bool IsAlive => Health > 0;
  195.  
  196.         public void TakeDamage(double damage)
  197.         {            
  198.             if (ChanceDodge > Random.Next(PresetMaximumPercentage))
  199.             {
  200.                 Console.WriteLine($"{TypeName} уклонился от атаки.");
  201.             }
  202.             else
  203.             {
  204.                 double realDamage = Math.Round(damage / Armor, 2);
  205.                 Health -= realDamage;
  206.  
  207.                 Console.WriteLine($"{TypeName} получает {realDamage} урона.");
  208.             }
  209.         }
  210.  
  211.         public void Attack(List<Soilder> targets)
  212.         {
  213.             Console.Write($"{TypeName} атакует: ");
  214.  
  215.             foreach(Soilder soilder in targets)
  216.             {
  217.                 Console.Write(soilder.Name + " ");
  218.             }
  219.  
  220.             Console.WriteLine();
  221.  
  222.             for (int i = 0; i < CountTarget; i++)
  223.             {
  224.                 targets[i].TakeDamage(Damage);
  225.             }
  226.             Console.WriteLine();
  227.         }
  228.  
  229.         public void ShowInfo()
  230.         {
  231.             Console.WriteLine($"{TypeName}. Здоровье: {Health}.");
  232.         }
  233.  
  234.         public virtual List<Soilder> ChooseTargets(IReadOnlyList<Soilder> enemySoilders)
  235.         {
  236.             List<Soilder> targets = new List<Soilder>();
  237.  
  238.             for (int i = 0; i < CountTarget; i++)
  239.             {
  240.                 targets.Add(enemySoilders[Random.Next(enemySoilders.Count)]);
  241.             }
  242.  
  243.             return targets;
  244.         }
  245.     }
  246.  
  247.     class Commando : Soilder
  248.     {
  249.         public Commando() : base()
  250.         {
  251.             TypeName = "Спецназовец";
  252.             Damage = 20;
  253.             Armor = 3;
  254.             ChanceDodge = 80;
  255.             CountTarget = 3;
  256.             Health = 80;
  257.         }  
  258.        
  259.         public override List<Soilder> ChooseTargets(IReadOnlyList<Soilder> enemySoilders)
  260.         {
  261.             List<Soilder> targets = new List<Soilder>();
  262.  
  263.             for (int i = 0; i < CountTarget; i++)
  264.             {
  265.                 targets.Add(enemySoilders[Random.Next(enemySoilders.Count)]);
  266.             }
  267.  
  268.             return targets;
  269.         }
  270.     }
  271.  
  272.     class RocketLauncher : Soilder
  273.     {
  274.         public RocketLauncher() : base()
  275.         {
  276.             TypeName = "Гранатомётчик";
  277.             Damage = 50;
  278.             Armor = 1;
  279.             ChanceDodge = 20;
  280.             CountTarget = 5;
  281.             Health = 60;
  282.         }
  283.  
  284.         public override List<Soilder> ChooseTargets(IReadOnlyList<Soilder> enemySoilders)
  285.         {
  286.             List<Soilder> targets = new List<Soilder>();
  287.  
  288.             for (int i = 0; i < CountTarget && i < enemySoilders.Count; i++)
  289.             {
  290.                 targets.Add(enemySoilders[i]);
  291.             }
  292.  
  293.             return targets;
  294.         }
  295.     }
  296.  
  297.     class AssaultMan : Soilder
  298.     {
  299.         public AssaultMan() : base()
  300.         {
  301.             TypeName = "Штурмовик";
  302.             Damage = 40;
  303.             Armor = 6;
  304.             ChanceDodge = 50;
  305.             CountTarget = 2;
  306.             Health = 100;
  307.         }
  308.  
  309.         public override List<Soilder> ChooseTargets(IReadOnlyList<Soilder> enemySoilders)
  310.         {
  311.             List<Soilder> targets = new List<Soilder>();
  312.             Soilder target = enemySoilders[Random.Next(enemySoilders.Count)];
  313.  
  314.             for (int i = 0; i < CountTarget; i++)
  315.             {
  316.                 targets.Add(target);
  317.             }
  318.  
  319.             return targets;
  320.         }
  321.     }
  322.  
  323.     class Sniper : Soilder
  324.     {
  325.         public Sniper() : base()
  326.         {
  327.             TypeName = "Снайпер";
  328.             Damage = 100;
  329.             Armor = 2;
  330.             ChanceDodge = 20;
  331.             CountTarget = 1;
  332.             Health = 70;
  333.         }
  334.     }
  335. }
Add Comment
Please, Sign In to add comment