lovelyvook

Unit_46

Aug 7th, 2024 (edited)
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Ijunior
  6. {
  7.     internal class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Game game = new Game();
  12.             game.Work();
  13.         }
  14.     }
  15.  
  16.     class Game
  17.     {
  18.         public void Work()
  19.         {
  20.             const string CommandPlay = "1";
  21.             const string CommandExit = "2";
  22.  
  23.             bool isWork = true;
  24.  
  25.             while (isWork)
  26.             {
  27.                 Console.Write($"{CommandPlay} - играть" +
  28.                               $"\n{CommandExit} - выйти" +
  29.                               $"\nВведите номер: ");
  30.  
  31.                 switch (Console.ReadLine())
  32.                 {
  33.                     case CommandPlay:
  34.                         CreateNewGame();
  35.                         break;
  36.  
  37.                     case CommandExit:
  38.                         isWork = false;
  39.                         break;
  40.  
  41.                     default:
  42.                         Console.WriteLine("Некорректный ввод");
  43.                         break;
  44.                 }
  45.             }
  46.         }
  47.  
  48.         private void CreateNewGame()
  49.         {
  50.             new Battlefield().Play();
  51.         }
  52.     }
  53.  
  54.     class Battlefield
  55.     {
  56.         private Platoon _firstPlatoon;
  57.         private Platoon _secondPlatoon;
  58.  
  59.         public Battlefield()
  60.         {
  61.             CreatePlatoons();
  62.         }
  63.  
  64.         public void Play()
  65.         {
  66.             Console.Clear();
  67.             Fight();
  68.             ShowResult();
  69.         }
  70.  
  71.         private void CreatePlatoons()
  72.         {
  73.             _firstPlatoon = new Platoon("Снежинки");
  74.             _secondPlatoon = new Platoon("Звездочки");
  75.         }
  76.  
  77.         private void Fight()
  78.         {
  79.             int round = 1;
  80.  
  81.             while (_firstPlatoon.ServicemenCount > 0 && _secondPlatoon.ServicemenCount > 0)
  82.             {
  83.                 Console.WriteLine("\nРаунд " + round);
  84.                 round++;
  85.  
  86.                 Console.WriteLine("\nАтакует отряд " + _firstPlatoon.Name);
  87.                 _firstPlatoon.Attack(_secondPlatoon.Servicemen);
  88.  
  89.                 Console.WriteLine("\nАтакует отряд " + _secondPlatoon.Name);
  90.                 _secondPlatoon.Attack(_firstPlatoon.Servicemen);
  91.  
  92.                 Console.WriteLine($"\nПотери отряда " + _firstPlatoon.Name);
  93.                 _firstPlatoon.RemoveDeadFighters();
  94.  
  95.                 Console.WriteLine($"\nПотери отряда " + _secondPlatoon.Name);
  96.                 _secondPlatoon.RemoveDeadFighters();
  97.             }
  98.         }
  99.  
  100.         private void ShowResult()
  101.         {
  102.             string separator = new string('*', 5);
  103.  
  104.             Console.Write("\n" + separator + " ");
  105.  
  106.             if (_firstPlatoon.ServicemenCount <= 0 && _secondPlatoon.ServicemenCount <= 0)
  107.             {
  108.                 Console.Write("Ничья. Оба отряда умерли");
  109.             }
  110.             else if (_firstPlatoon.ServicemenCount > _secondPlatoon.ServicemenCount)
  111.             {
  112.                 Console.Write(_firstPlatoon.Name + " выиграли");
  113.             }
  114.             else
  115.             {
  116.                 Console.Write(_secondPlatoon.Name + " выиграли");
  117.             }
  118.  
  119.             Console.WriteLine(" " + separator + "\n");
  120.         }
  121.     }
  122.  
  123.     class Platoon
  124.     {
  125.         private List<Serviceman> _servicemen;
  126.  
  127.         public Platoon(string name)
  128.         {
  129.             _servicemen = new List<Serviceman>();
  130.             CreateArmy();
  131.             Name = name;
  132.         }
  133.  
  134.         public string Name { get; }
  135.  
  136.         public int ServicemenCount => _servicemen.Count;
  137.  
  138.         public List<Serviceman> Servicemen => _servicemen.ToList();
  139.  
  140.         public void Attack(List<Serviceman> attackedPlatoon)
  141.         {
  142.             foreach (Serviceman serviceman in _servicemen)
  143.             {
  144.                 serviceman.Attack(attackedPlatoon);
  145.             }
  146.         }
  147.  
  148.         public void RemoveDeadFighters()
  149.         {
  150.             for (int i = _servicemen.Count - 1; i >= 0; i--)
  151.             {
  152.                 if (_servicemen[i].Health <= 0)
  153.                 {
  154.                     Console.WriteLine($"{_servicemen[i].Name} умер");
  155.                     _servicemen.Remove(_servicemen[i]);
  156.                 }
  157.             }
  158.         }
  159.  
  160.         private void CreateArmy()
  161.         {
  162.             _servicemen.Add(new Soldier("Солдат", GenerateHealth(20, 50), 5, 1));
  163.             _servicemen.Add(new Soldier("Солдат", GenerateHealth(20, 50), 5, 1));
  164.             _servicemen.Add(new Sergeant("Сержант", GenerateHealth(30, 50), 5, 2));
  165.             _servicemen.Add(new Lieutenant("Лейтенант", GenerateHealth(40, 50), 10, 3));
  166.             _servicemen.Add(new Captain("Капитан", GenerateHealth(40, 50), 10, 3));
  167.         }
  168.  
  169.         private int GenerateHealth(int minValue, int maxValue)
  170.         {
  171.             return Utils.GetRandomNumber(minValue, maxValue);
  172.         }
  173.     }
  174.  
  175.     class Serviceman
  176.     {
  177.         private int _armor;
  178.  
  179.         public Serviceman(string name, int health, int damage, int armor)
  180.         {
  181.             Name = name;
  182.             Health = health;
  183.             Damage = damage;
  184.             _armor = armor;
  185.         }
  186.  
  187.         public string Name { get; }
  188.         public int Health { get; private set; }
  189.         public int Damage { get; }
  190.  
  191.         public virtual void Attack(List<Serviceman> attackedPlatoon)
  192.         {
  193.             Serviceman attackedServiceman = attackedPlatoon[Utils.GetRandomNumber(0, attackedPlatoon.Count - 1)];
  194.             Console.WriteLine($"{Name} наносит {attackedServiceman.Name} урон: {Damage}");
  195.             attackedServiceman.TakeDamage(Damage);
  196.         }
  197.  
  198.         public virtual void TakeDamage(int damage)
  199.         {
  200.             int totalDamage = damage - _armor;
  201.             Health -= totalDamage;
  202.             Console.WriteLine($"{Name} получает урон: {totalDamage}. Здоровье: {Health}");
  203.         }
  204.     }
  205.  
  206.     class Soldier : Serviceman
  207.     {
  208.         public Soldier(string name, int health, int damage, int armor) : base(name, health, damage, armor) { }
  209.     }
  210.  
  211.     class Sergeant : Serviceman
  212.     {
  213.         public Sergeant(string name, int health, int damage, int armor) : base(name, health, damage, armor) { }
  214.  
  215.         public override void Attack(List<Serviceman> attackedPlatoon)
  216.         {
  217.             int damageMultiplier = 2;
  218.             int increasedDamage = Damage * damageMultiplier;
  219.  
  220.             Serviceman attackedServiceman = attackedPlatoon[Utils.GetRandomNumber(0, attackedPlatoon.Count - 1)];
  221.             Console.WriteLine($"{Name} наносит {attackedServiceman.Name} урон: {increasedDamage}");
  222.             attackedServiceman.TakeDamage(increasedDamage);
  223.         }
  224.     }
  225.  
  226.     class Lieutenant : Serviceman
  227.     {
  228.         public Lieutenant(string name, int health, int damage, int armor) : base(name, health, damage, armor) { }
  229.  
  230.         public override void Attack(List<Serviceman> attackedPlatoon)
  231.         {
  232.             List<int> indexes = new List<int>();
  233.             GenerateUniqueIndex(attackedPlatoon, indexes);
  234.  
  235.             foreach (int index in indexes)
  236.             {
  237.                 Serviceman attackedServiceman = attackedPlatoon[index];
  238.                 Console.WriteLine($"{Name} наносит {attackedServiceman.Name} урон: {Damage}");
  239.                 attackedServiceman.TakeDamage(Damage);
  240.             }
  241.         }
  242.  
  243.         private void GenerateUniqueIndex(List<Serviceman> attackedPlatoon, List<int> indexes)
  244.         {
  245.             int countIndexes = 3;
  246.  
  247.             if (attackedPlatoon.Count > countIndexes)
  248.             {
  249.                 while (countIndexes > 0)
  250.                 {
  251.                     int tempIndex = Utils.GetRandomNumber(0, attackedPlatoon.Count - 1);
  252.  
  253.                     if (indexes.Contains(tempIndex) == false)
  254.                     {
  255.                         indexes.Add(tempIndex);
  256.                         countIndexes--;
  257.                     }
  258.                 }
  259.             }
  260.             else
  261.             {
  262.                 for (int i = 0; i < attackedPlatoon.Count; i++)
  263.                 {
  264.                     indexes.Add(i);
  265.                 }
  266.             }
  267.         }
  268.     }
  269.  
  270.     class Captain : Serviceman
  271.     {
  272.         public Captain(string name, int health, int damage, int armor) : base(name, health, damage, armor) { }
  273.  
  274.         public override void Attack(List<Serviceman> attackedPlatoon)
  275.         {
  276.             int coutnAttack = 3;
  277.  
  278.             for (int i = 0; i < coutnAttack; i++)
  279.             {
  280.                 base.Attack(attackedPlatoon);
  281.             }
  282.         }
  283.     }
  284.  
  285.     class Utils
  286.     {
  287.         private static Random s_random = new Random();
  288.  
  289.         public static int GetRandomNumber(int minValue, int maxValue)
  290.         {
  291.             return s_random.Next(minValue, maxValue);
  292.         }
  293.     }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment