Advertisement
Askor

Hw35

Dec 15th, 2021 (edited)
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.14 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 Battle
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Battle battle = new Battle();
  14.             battle.Fight();
  15.             Console.ReadKey();
  16.         }
  17.     }
  18.  
  19.     class Squad
  20.     {
  21.         protected List<Soldier> Soldiers { get; private set; }
  22.  
  23.         public bool IsAlive => Soldiers.Count > 0;
  24.  
  25.         public Squad(int randomCount)
  26.         {
  27.             int SquadSize = 5;
  28.             Soldiers = new List<Soldier>();
  29.             Create(SquadSize, randomCount);
  30.         }
  31.  
  32.         public void ShowInfo()
  33.         {
  34.             foreach (Soldier soldier in Soldiers)
  35.             {
  36.                 Console.WriteLine($"HP: {soldier.Health} | DMG: {soldier.Damage}");
  37.             }
  38.         }
  39.  
  40.         public void TakeAim(Soldier enemy)
  41.         {
  42.             Random random = new Random();
  43.  
  44.             foreach (Soldier soldier in Soldiers)
  45.             {
  46.                 soldier.ChooseTarget(enemy);
  47.             }
  48.         }
  49.  
  50.         public void RemoveSoldier()
  51.         {
  52.             for (int i = 0; i < Soldiers.Count; i++)
  53.             {
  54.                 if (Soldiers[i].IsDead)
  55.                 {
  56.                     Soldiers.RemoveAt(i);
  57.                 }
  58.             }
  59.         }
  60.  
  61.         public void GetAttack()
  62.         {
  63.             foreach (Soldier soldier in Soldiers)
  64.             {
  65.                 soldier.DealDamage();
  66.             }
  67.         }
  68.  
  69.         public Soldier GetSoldiers()
  70.         {
  71.             foreach (var soldier in Soldiers)
  72.             {
  73.                 return soldier;
  74.             }
  75.  
  76.             return null;
  77.         }
  78.  
  79.         private void Create(int count, int randomCount)
  80.         {
  81.             Random random = new Random(randomCount);
  82.             int highDamage = 9;
  83.             int avarageDamage = 7;
  84.             int lowDamage = 5;
  85.             int highHealth = 15;
  86.             int avarageHealth = 13;
  87.             int lowHealth = 10;
  88.  
  89.             int[] listOfHealth = { lowHealth, avarageHealth, highHealth };
  90.             int[] listOfDamage = { lowDamage, avarageDamage, highDamage };
  91.  
  92.             Soldier soldier;
  93.  
  94.             for (int i = 0; i < count; i++)
  95.             {
  96.                 int healthIndex = random.Next(listOfHealth.Length);
  97.                 int damageIndex = random.Next(listOfDamage.Length);
  98.  
  99.                 soldier = new Soldier(listOfHealth[healthIndex], listOfDamage[damageIndex]);
  100.  
  101.                 Soldiers.Add(soldier);
  102.             }
  103.         }
  104.     }
  105.  
  106.     class Soldier
  107.     {
  108.         public int Health { get; private set; }
  109.  
  110.         public int Damage { get; private set; }
  111.  
  112.         public Soldier Target { get; private set; }
  113.  
  114.         public bool IsDead => Health <= 0;
  115.  
  116.         public Soldier(int health, int damage)
  117.         {
  118.             Health = health;
  119.             Damage = damage;
  120.         }
  121.  
  122.         public void ChooseTarget(Soldier enemy)
  123.         {
  124.             if(Target != null || enemy.IsDead == false)
  125.             {
  126.                 Target = enemy;
  127.             }
  128.         }
  129.  
  130.         public void DealDamage()
  131.         {
  132.             if(Target != null && Target.IsDead == false)
  133.             {
  134.                 Target.TakeDamage(Damage);
  135.  
  136.                 if (Target.IsDead)
  137.                 {
  138.                     Target = null;
  139.                 }
  140.             }
  141.         }
  142.  
  143.         private void TakeDamage(int damage)
  144.         {
  145.             Health -= damage;
  146.         }
  147.     }
  148.  
  149.     class Battle
  150.     {
  151.         private Squad _squad1;
  152.         private Squad _squad2;
  153.  
  154.         public Battle()
  155.         {
  156.             _squad1 = new Squad(1);
  157.             _squad2 = new Squad(2);
  158.         }
  159.  
  160.         public void ShowInfo()
  161.         {
  162.             Console.WriteLine("\n[ -----------Squad 1-----------]");
  163.             _squad1.ShowInfo();            
  164.             Console.WriteLine("\n[ -----------Squad 2-----------]");
  165.             _squad2.ShowInfo();
  166.         }
  167.  
  168.         public void Fight()
  169.         {
  170.             while (IsWinner() == false)
  171.             {
  172.                 ShowInfo();
  173.  
  174.                 _squad1.TakeAim(_squad2.GetSoldiers());
  175.                 _squad2.TakeAim(_squad1.GetSoldiers());
  176.  
  177.                 _squad1.GetAttack();
  178.                 _squad2.GetAttack();
  179.  
  180.                 _squad1.RemoveSoldier();
  181.                 _squad2.RemoveSoldier();
  182.  
  183.                 Console.ReadKey();
  184.             }
  185.         }
  186.  
  187.         private bool IsWinner()
  188.         {
  189.             if (_squad1.IsAlive && _squad2.IsAlive)
  190.             {
  191.                 return false;
  192.             }
  193.             else if (_squad1.IsAlive && _squad2.IsAlive == false)
  194.             {
  195.                 Console.WriteLine("\nSquad 1 Win");
  196.                 return true;
  197.             }
  198.             else if (_squad1.IsAlive == false && _squad2.IsAlive)
  199.             {
  200.                 Console.WriteLine("\nSquad 2 Win");
  201.                 return true;
  202.             }
  203.             else
  204.             {
  205.                 Console.WriteLine("\nthere is no winner");
  206.                 return true;
  207.             }
  208.         }
  209.     }
  210. }
  211.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement