Advertisement
Vapio

task35

Jun 1st, 2021 (edited)
992
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4.  
  5. public class Program
  6. {
  7.     public static void Main()
  8.     {
  9.         War war = new War();
  10.         bool isFighting = true;
  11.         long amountTurns = 1;
  12.         int timeSleepAmount = 500;
  13.  
  14.         while (isFighting)
  15.         {
  16.             war.Update();
  17.             Console.WriteLine($"Turn is : {amountTurns}");
  18.             isFighting = war.IsFighting();
  19.             ++amountTurns;
  20.             Thread.Sleep(timeSleepAmount);
  21.         }
  22.     }
  23. }
  24.  
  25. public class War
  26. {
  27.     private Random _random;
  28.     private Country _countryFirst;
  29.     private Country _countrySecond;
  30.     private bool _isFirstCountry;
  31.  
  32.     private int _percentRangeRandom = 100;
  33.     private int _percentFirstCountry = 50;
  34.  
  35.     public War()
  36.     {
  37.         _random = new Random();
  38.         _isFirstCountry = true;
  39.         _countryFirst = new Country(_random);
  40.         _countrySecond = new Country(_random);
  41.     }
  42.  
  43.     public void Update()
  44.     {
  45.         Country countryAttack = null;
  46.         Country countryDefend = null;
  47.  
  48.         Console.Clear();
  49.  
  50.         if (_isFirstCountry)
  51.         {
  52.             countryAttack = _countryFirst;
  53.             countryDefend = _countrySecond;
  54.             Console.WriteLine("First country turn.");
  55.         }
  56.         else
  57.         {
  58.             countryAttack = _countrySecond;
  59.             countryDefend = _countryFirst;
  60.             Console.WriteLine("Second country turn.");
  61.         }
  62.  
  63.         Console.WriteLine($"Country attack : {countryAttack}");
  64.         Console.WriteLine($"Country deffender : {countryDefend}");
  65.  
  66.         int trooperAttack = _random.Next(countryAttack.TroopersAmount);
  67.         int trooperDefend = _random.Next(countryDefend.TroopersAmount);
  68.  
  69.         if (countryAttack.IsMedic(trooperAttack))
  70.         {
  71.             int trooperHeal = _random.Next(countryAttack.TroopersAmount);
  72.             countryAttack.AttackMedic(trooperAttack, countryDefend.GetTrooper(trooperDefend),
  73.                                       countryAttack.GetTrooper(trooperHeal));
  74.         }
  75.         else
  76.         {
  77.             countryAttack.Attack(trooperAttack, countryDefend.GetTrooper(trooperDefend));
  78.         }
  79.  
  80.         countryDefend.CheckHealth(trooperDefend);
  81.         _isFirstCountry = ( _random.Next(_percentRangeRandom) / _percentFirstCountry ) == 1;
  82.     }
  83.  
  84.     public bool IsFighting()
  85.     {
  86.         if (_countryFirst.isAllDead())
  87.         {
  88.             Console.WriteLine("Second country win!");
  89.             Console.WriteLine($"{_countrySecond}");
  90.             return false;
  91.         }
  92.         else if (_countrySecond.isAllDead())
  93.         {
  94.             Console.WriteLine("First country win!");
  95.             Console.WriteLine($"{_countryFirst}");
  96.             return false;
  97.         }
  98.         else
  99.         {
  100.             return true;
  101.         }
  102.     }
  103. }
  104.  
  105. public class Country
  106. {
  107.     private List<Trooper> _troopers;
  108.     private Random _random;
  109.  
  110.     private int _troopersAmountMinimum = 20;
  111.     private int _troopersAmountMaximum = 100;
  112.     private int _trooperTypeAmount = 3;
  113.  
  114.     public int TroopersAmount => _troopers.Count;
  115.  
  116.     public Country(Random random)
  117.     {
  118.         _random = random;
  119.         _troopers = new List<Trooper>();
  120.         CreateTroopers();
  121.     }
  122.  
  123.     public bool IsMedic(int index)
  124.     {
  125.         return _troopers[index] is Medic;
  126.     }
  127.  
  128.     public bool isAllDead()
  129.     {
  130.         return _troopers.Count <= 0;
  131.     }
  132.  
  133.     public Trooper GetTrooper(int index)
  134.     {
  135.         return _troopers[index];
  136.     }
  137.  
  138.     public void Attack(int trooperAttack, Trooper enemy)
  139.     {
  140.         _troopers[trooperAttack].Attack(enemy);
  141.     }
  142.  
  143.     public void AttackMedic(int trooperAttack, Trooper enemy, Trooper heal)
  144.     {
  145.         Medic medic = (Medic) _troopers[trooperAttack];
  146.         medic.Attack(enemy);
  147.         medic.IncreaseHealth(heal);
  148.     }
  149.  
  150.     public void CheckHealth(int trooper)
  151.     {
  152.         if (_troopers[trooper].GetHealth() <= 0)
  153.             _troopers.RemoveAt(trooper);
  154.     }
  155.  
  156.     public override string ToString()
  157.     {
  158.         string result = "";
  159.         result += "Amount of troopers : " + TroopersAmount + ".\n";
  160.         return result;
  161.     }
  162.  
  163.     private void CreateTroopers()
  164.     {
  165.         int trooperAmount = _random.Next(_troopersAmountMinimum, _troopersAmountMaximum);
  166.         Trooper trooper = null;
  167.         int trooperType;
  168.  
  169.         for (int i = 0; i < trooperAmount; ++i)
  170.         {
  171.             trooperType = _random.Next(_trooperTypeAmount);
  172.            
  173.             switch(trooperType)
  174.             {
  175.                 case 0:
  176.                     trooper = new Striker(_random);
  177.                     break;
  178.                 case 1:
  179.                     trooper = new Sniper(_random);
  180.                     break;
  181.                 case 2:
  182.                     trooper = new Medic(_random);
  183.                     break;
  184.             }
  185.  
  186.             _troopers.Add(trooper);
  187.         }
  188.     }
  189.  
  190. }
  191.  
  192. public abstract class Trooper
  193. {
  194.     protected int Health;
  195.     protected int Damage;
  196.    
  197.     protected Trooper()
  198.     {
  199.     }
  200.  
  201.     public void TakeDamage(int damage)
  202.     {
  203.         Health -= damage;
  204.     }
  205.  
  206.     public void Attack(Trooper trooper)
  207.     {
  208.         trooper.TakeDamage(Damage);
  209.     }
  210.  
  211.     public void IncreaseHealth(int health)
  212.     {
  213.         Health += health;
  214.     }
  215.  
  216.     public int GetHealth()
  217.     {
  218.         return Health;
  219.     }
  220. }
  221.  
  222. public class Sniper : Trooper
  223. {
  224.     private int _healthMinimum = 100;
  225.     private int _healthMaximum = 200;
  226.     private int _damageMinimum = 300;
  227.     private int _damageMaximum = 500;
  228.  
  229.     public Sniper(Random random)
  230.     {
  231.         Health = random.Next(_healthMinimum, _healthMaximum);
  232.         Damage = random.Next(_damageMinimum, _damageMaximum);
  233.     }
  234. }
  235.  
  236. public class Striker : Trooper
  237. {
  238.     private int _healthMinimum = 150;
  239.     private int _healthMaximum = 250;
  240.     private int _damageMinimum = 200;
  241.     private int _damageMaximum = 350;
  242.  
  243.     public Striker(Random random)
  244.     {
  245.         Health = random.Next(_healthMinimum, _healthMaximum);
  246.         Damage = random.Next(_damageMinimum, _damageMaximum);
  247.     }
  248. }
  249.  
  250. public class Medic : Trooper
  251. {
  252.     private int _healthMinimum = 75;
  253.     private int _healthMaximum = 125;
  254.     private int _damageMinimum = 150;
  255.     private int _damageMaximum = 200;
  256.  
  257.     public Medic(Random random)
  258.     {
  259.         Health = random.Next(_healthMinimum, _healthMaximum);
  260.         Damage = random.Next(_damageMinimum, _damageMaximum);
  261.     }
  262.  
  263.     public void IncreaseHealth(Trooper ally)
  264.     {
  265.         ally.IncreaseHealth(Damage);
  266.     }
  267. }
  268.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement