Torgach

protoWar

Apr 14th, 2021
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.84 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 war
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Battle battle = new Battle();
  14.             battle.RunMenu();
  15.  
  16.             Squad squad = new Squad();
  17.  
  18.         }
  19.     }
  20.  
  21.     class Battle
  22.     {
  23.         private int Raund { get; set; }
  24.         private Squad squadLeft;
  25.         private Squad squadRight;
  26.         private static Random rand;
  27.         public Battle()
  28.         {
  29.             Raund = 0;
  30.             rand = new Random();
  31.             squadLeft = new Squad();
  32.             squadRight = new Squad();
  33.         }
  34.  
  35.         public void RunMenu()
  36.         {
  37.             bool isRun = true;
  38.             while (isRun)
  39.             {
  40.                 Console.WriteLine("[1] - Создать отряд\n[2] - Запустить бой\n[3] - Выход");
  41.  
  42.                 switch (Console.ReadLine())
  43.                 {
  44.                     case "1":
  45.                         squadLeft.CreateSquad();
  46.                         squadRight.CreateSquad();
  47.                         break;
  48.                     case "2":
  49.                         Play(squadLeft, squadRight);
  50.                         break;
  51.                     case "3":
  52.                         isRun = false;
  53.                         break;
  54.                 }
  55.             }
  56.         }
  57.  
  58.         public void Play(Squad squadLeft, Squad squadRight)
  59.         {
  60.             if(IsGameReady() == false)
  61.             {
  62.                 return;
  63.             }
  64.  
  65.             while(IsGameover() == false)
  66.             {
  67.                 Console.WriteLine("Раунд: " + ++Raund);
  68.  
  69.                 Console.WriteLine("Западный отряд атакует!");
  70.                 squadLeft.GetSoldier().Attack(squadRight.GetSoldier());
  71.  
  72.                 Console.WriteLine("Восточный отряд атакует!");
  73.                 squadRight.GetSoldier().Attack(squadLeft.GetSoldier());
  74.  
  75.  
  76.             }
  77.         }
  78.  
  79.         public bool IsGameover()
  80.         {
  81.             if(squadLeft.GetHealthSquad() <= 0)
  82.             {
  83.                 Console.WriteLine("Западный отряд умер!");
  84.                 return true;
  85.             }
  86.             else if(squadRight.GetHealthSquad() <= 0)
  87.             {
  88.                 Console.WriteLine("Восточный отряд умер!");
  89.                 return true;
  90.             }
  91.  
  92.             Console.WriteLine("\nСилы западного отряда: " + squadLeft.GetHealthSquad());
  93.             Console.WriteLine("Силы восточного отряда: " + squadRight.GetHealthSquad());
  94.             return false;
  95.         }
  96.  
  97.         public bool IsGameReady()
  98.         {
  99.             if (squadLeft.GetSquadSize() < 0)
  100.             {
  101.                 Console.WriteLine("Левый отряд не создан!");
  102.                 return false;
  103.             }
  104.             else if(squadRight.GetSquadSize() < 0)
  105.             {
  106.                 Console.WriteLine("Правый отряд не создан!");
  107.                 return false;
  108.             }
  109.             return true;
  110.         }
  111.  
  112.     }
  113.  
  114.     class Squad
  115.     {
  116.         private List<Soldier> _soldiers;
  117.         private static Random rand = new Random();
  118.  
  119.         public Squad()
  120.         {
  121.             _soldiers = new List<Soldier>();
  122.         }
  123.  
  124.         public void CreateSquad()
  125.         {
  126.             //if (_soldiers.Count > 2)
  127.             //{
  128.             //    Console.WriteLine("Нельзя создать больше 2 отрядов!");
  129.             //    return;
  130.             //}
  131.             Console.WriteLine("Создаем отряд...");
  132.  
  133.             for (int i = 0; i < 20; i++)
  134.             {
  135.                 int number = rand.Next(0, 2);
  136.  
  137.                 if (number == 0)
  138.                 {
  139.                     Medic medic = new Medic(i, rand.Next(50, 100), rand.Next(25, 50));
  140.                     _soldiers.Add(medic);
  141.                 }
  142.                 else if (number == 1)
  143.                 {
  144.                     Infantryman infantryman = new Infantryman(i, rand.Next(70, 100), rand.Next(50, 80));
  145.                     _soldiers.Add(infantryman);
  146.                 }
  147.                 else if (number == 2)
  148.                 {
  149.                     Sniper sniper = new Sniper(i, rand.Next(50, 70), rand.Next(85, 105));
  150.                     _soldiers.Add(sniper);
  151.                 }
  152.             }
  153.         }
  154.  
  155.         public int GetHealthSquad()
  156.         {
  157.             int healthSquad = 0;
  158.  
  159.             foreach (var soldier in _soldiers)
  160.             {
  161.                 healthSquad += soldier.Health;
  162.             }
  163.  
  164.             return healthSquad;
  165.         }
  166.  
  167.         public int GetSquadSize()
  168.         {
  169.             return _soldiers.Count;
  170.         }
  171.  
  172.         public List<Soldier> GetSoldiers()
  173.         {
  174.             return _soldiers;
  175.         }
  176.  
  177.         public Soldier GetSoldier()
  178.         {
  179.             return _soldiers[rand.Next(0, _soldiers.Count)];
  180.         }
  181.  
  182.     }
  183.  
  184.     abstract class Soldier
  185.     {
  186.         public bool IsShoot { get; protected set; }
  187.         public bool IsAvoid { get; protected set; }
  188.         public int Number { get; protected set; }
  189.         public int Health { get; protected set; }
  190.         public int Damage { get; protected set; }
  191.  
  192.         public virtual void UseAbillity()
  193.         {
  194.             IsAvoid = true;
  195.         }
  196.  
  197.         public void Attack(Soldier enemy)
  198.         {
  199.             if(IsShoot)
  200.             {
  201.                 IsShoot = false;
  202.                 return;
  203.             }
  204.  
  205.             if (enemy.IsAvoid)
  206.             {
  207.                 return;
  208.             }
  209.  
  210.             IsShoot = true;
  211.             enemy.Health -= Damage;
  212.         }
  213.     }
  214.  
  215.     class Medic : Soldier
  216.     {
  217.         public Medic(int number, int health, int damage)
  218.         {
  219.             IsShoot = false;
  220.             IsAvoid = false;
  221.             Number = number;
  222.             Health = health;
  223.             Damage = damage;
  224.         }
  225.  
  226.  
  227.         //public override void UseAbillity(List<Soldier> soldiers)
  228.         //{
  229.            
  230.         //}
  231.     }
  232.  
  233.     class Infantryman : Soldier
  234.     {
  235.         public Infantryman(int number, int health, int damage)
  236.         {
  237.             IsShoot = false;
  238.             IsAvoid = false;
  239.             Number = number;
  240.             Health = health;
  241.             Damage = damage;
  242.         }
  243.  
  244.         //public override void UseAbillity(Random rand)
  245.         //{
  246.  
  247.         //}
  248.  
  249.     }
  250.  
  251.     class Sniper : Soldier
  252.     {
  253.         public Sniper(int number, int health, int damage)
  254.         {
  255.             IsShoot = true;
  256.             IsAvoid = false; //
  257.             Number = number;
  258.             Health = health;
  259.             Damage = damage;
  260.         }
  261.  
  262.         //public override void UseAbillity(Random rand)
  263.         //{
  264.  
  265.         //}
  266.  
  267.     }
  268.  
  269. }
  270.  
Advertisement
Add Comment
Please, Sign In to add comment