GPbl3YH

CSLight #35

Feb 9th, 2021 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.97 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 CSLight
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Battle battle = new Battle();
  14.             battle.Start();
  15.             Console.ReadKey();
  16.         }
  17.     }
  18.  
  19.     class Battle
  20.     {
  21.         private Random _rand;
  22.         private Troop _firstTroop;
  23.         private Troop _secondTroop;
  24.        
  25.         public Battle()
  26.         {
  27.             _rand = new Random();
  28.             _firstTroop = new Troop();
  29.             _secondTroop = new Troop();
  30.         }
  31.  
  32.         public void Start()
  33.         {
  34.             while (GetTroopStatus(_firstTroop) && GetTroopStatus(_secondTroop))
  35.             {
  36.                 Console.WriteLine("\n\nПервый отряд:");
  37.  
  38.                 _firstTroop.ShowAllStats();
  39.  
  40.                 Simulate(_secondTroop, _firstTroop);
  41.  
  42.                 _firstTroop.CleanBattleground();
  43.  
  44.                 Console.WriteLine("\n\nВторой отряд:");
  45.  
  46.                 _secondTroop.ShowAllStats();
  47.  
  48.                 Simulate(_firstTroop, _secondTroop);
  49.  
  50.                 _secondTroop.CleanBattleground();
  51.             }
  52.  
  53.             Console.WriteLine($"\n\nКонец симуляции");
  54.         }
  55.  
  56.         private bool GetTroopStatus(Troop troop)
  57.         {
  58.             return troop.Count() > 0;
  59.         }
  60.  
  61.         private void Simulate(Troop attackTroops, Troop defenceTroops)
  62.         {
  63.             int targetId;
  64.  
  65.             for (int i = 0; i < attackTroops.Count(); i++)
  66.             {
  67.                 targetId = _rand.Next(0, defenceTroops.Count());
  68.                 defenceTroops.TakeDamage(targetId, attackTroops.GetDamageThroughId(i));
  69.             }
  70.         }
  71.     }
  72.  
  73.     class Troop
  74.     {
  75.         private Random _rand = new Random();
  76.         private List<Warrior> _troops = new List<Warrior>();
  77.  
  78.         public Troop()
  79.         {
  80.             int warriorsCount = _rand.Next(25, 36);
  81.  
  82.             for (int i = 0; i < warriorsCount; i++)
  83.             {
  84.                 if (i % 2 == 0)
  85.                     _troops.Add(new ShotGunner(90, 20, 5));
  86.  
  87.                 if (i % 4 == 0)
  88.                     _troops.Add(new MachineGunner(200, 8, 1.2));
  89.  
  90.                 if (i % 5 == 0)
  91.                     _troops.Add(new Sniper(70, 30));
  92.             }
  93.         }
  94.  
  95.         public void CleanBattleground()
  96.         {
  97.             for (int i = 0; i < _troops.Count; i++)
  98.                 if (_troops[i].Health <= 0)
  99.                 {
  100.                     _troops.RemoveAt(i);
  101.                     i -= 1;
  102.                 }      
  103.         }
  104.  
  105.         public void TakeDamage(int id, double damage)
  106.         {
  107.             _troops[id].TakeDamage(damage);
  108.         }
  109.  
  110.         public int Count()
  111.         {
  112.             return _troops.Count;
  113.         }
  114.  
  115.         public double GetDamageThroughId(int id)
  116.         {
  117.             return _troops[id].Damage;
  118.         }
  119.  
  120.         public double GetHealthThroughId(int id)
  121.         {
  122.             return _troops[id].Health;
  123.         }
  124.  
  125.         public void ShowAllStats()
  126.         {
  127.             for (int i = 0; i < 3; i++)
  128.             {
  129.                 if (i == 0)
  130.                     ShowTroopStats(_troops, "shortgun");
  131.  
  132.                 if (i == 1)
  133.                     ShowTroopStats(_troops, "machinegun");
  134.  
  135.                 if (i == 2)
  136.                     ShowTroopStats(_troops, "sniper");
  137.             }
  138.         }
  139.  
  140.         private void ShowTroopStats(List<Warrior> troops, string classId)
  141.         {
  142.             Console.WriteLine();
  143.             foreach (var warrior in troops)
  144.                 if (warrior.ClassId == classId)
  145.                     Console.WriteLine($"Здоровье - {(int)warrior.Health}, Урон - {(int)warrior.Damage}");
  146.             Console.WriteLine();
  147.         }
  148.     }
  149.  
  150.     class Warrior
  151.     {
  152.         protected Random Rand = new Random();
  153.         public string ClassId { get; protected set; }
  154.         public double Health { get; protected set; }
  155.         public double Damage { get; protected set; }
  156.  
  157.         public Warrior(double health, double damage)
  158.         {
  159.             Health = health;
  160.             Damage = damage;
  161.         }
  162.  
  163.         public virtual void TakeDamage(double damage)
  164.         {
  165.             Health -= damage;
  166.         }
  167.     }
  168.  
  169.     class MachineGunner : Warrior
  170.     {
  171.         private double _fireRate;
  172.         public MachineGunner(double health, double damage, double fireRate) : base(health, damage)
  173.         {
  174.             _fireRate = fireRate;
  175.             ClassId = "machinegun";
  176.         }
  177.  
  178.         public override void TakeDamage(double damage)
  179.         {
  180.             base.TakeDamage(damage);
  181.             ChangeDamage();
  182.         }
  183.  
  184.         public void ChangeDamage()
  185.         {
  186.             Damage *= _fireRate;
  187.         }
  188.     }
  189.  
  190.     class Sniper : Warrior
  191.     {
  192.         private double _baseDamage;
  193.  
  194.         public Sniper(double health, double damage) : base (health, damage)
  195.         {
  196.             _baseDamage = damage;
  197.             ClassId = "sniper";
  198.         }
  199.  
  200.         public override void TakeDamage(double damage)
  201.         {
  202.             base.TakeDamage(damage);
  203.             ChangeDamage();
  204.         }
  205.  
  206.         private void ChangeDamage()
  207.         {
  208.             if (Damage == _baseDamage)
  209.             {
  210.                 int probability = Rand.Next(0, 3);
  211.  
  212.                 if (probability == 1)
  213.                     Damage *= 3;
  214.             }
  215.             else
  216.             {
  217.                 Damage /= 3;
  218.             }
  219.         }
  220.     }
  221.  
  222.     class ShotGunner : Warrior
  223.     {
  224.         private double _cureSpeed;
  225.  
  226.         public ShotGunner(double health, double damage, double cureSpeed) : base(health, damage)
  227.         {
  228.             _cureSpeed = cureSpeed;
  229.             ClassId = "shortgun";
  230.         }
  231.  
  232.         public override void TakeDamage(double damage)
  233.         {
  234.             base.TakeDamage(damage);
  235.             Health += _cureSpeed;
  236.         }
  237.     }
  238. }
Add Comment
Please, Sign In to add comment