Advertisement
OwlyOwl

worldWar

Jul 15th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection.PortableExecutable;
  5. using System.Runtime.CompilerServices;
  6.  
  7. namespace WorldWar
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Army army1 = new Army("Kingdom");
  14.             army1.CreateUnit(10);
  15.             Army army2 = new Army("NeKingdom");
  16.             army2.CreateUnit(10);
  17.             Fight(army1, army2);
  18.         }
  19.         public static void Fight(Army army1, Army army2)
  20.         {
  21.             while (army1.CheckDeaths() && army2.CheckDeaths())
  22.             {
  23.                 army2.GotShot(army1.Shoot());
  24.                 Console.WriteLine("Pif paf!");
  25.                 army2.CheckDeaths();
  26.                 army1.GotShot(army2.Shoot());
  27.                 Console.WriteLine("Pif paf!");
  28.                 army1.CheckDeaths();
  29.             }
  30.             if (army1.CheckDeaths() == false)
  31.             {
  32.                 Console.WriteLine("Страна " + army1.Name + " повержена");
  33.             }
  34.             else
  35.             {
  36.                 Console.WriteLine("Страна " + army2.Name + " повержена");
  37.             }
  38.         }
  39.     }
  40.  
  41.     class Army
  42.     {
  43.         private List<Soldier> _unit = new List<Soldier>();
  44.  
  45.         public string Name { get; private set; }
  46.  
  47.         public Army(string name)
  48.         {
  49.             List<Soldier> _unit = new List<Soldier>();
  50.             Name = name;
  51.         }
  52.  
  53.         public void CreateUnit(int amount)
  54.         {
  55.             for (int i = 0; i < amount; i++)
  56.             {
  57.                 Random rand = new Random();
  58.                 Soldier newSoldier = new Soldier(rand.Next(200, 500), rand.Next(10, 30));
  59.                 _unit.Add(newSoldier);
  60.             }
  61.         }
  62.  
  63.         public List<int> Shoot()
  64.         {
  65.             List<int> unitDamage = new List<int>();
  66.             for (int i = 0; i < _unit.Count; i++)
  67.             {
  68.                 unitDamage.Add(_unit[i].Damage);
  69.             }
  70.             return unitDamage;
  71.         }
  72.  
  73.         public void GotShot(List<int> damage)
  74.         {
  75.             Random rand = new Random();
  76.             for (int i = 0; i < _unit.Count; i++)
  77.             {
  78.                 _unit[rand.Next(0, _unit.Count)].TakeDamage(damage[i]);
  79.             }
  80.         }
  81.  
  82.         public bool CheckDeaths()
  83.         {
  84.             bool stillFighting;
  85.             foreach (var item in _unit)
  86.             {
  87.                 if (item.Health <= 0)
  88.                 {
  89.                     item.Die();
  90.                 }
  91.             }
  92.             int corps = 0;
  93.             foreach (var item in _unit)
  94.             {
  95.                 if (item.isAlive == false)
  96.                 {
  97.                     corps++;
  98.                 }
  99.             }
  100.             if (corps < 10)
  101.             {
  102.                 stillFighting = true;
  103.                 return stillFighting;
  104.             }
  105.             else
  106.             {
  107.                 stillFighting = false;
  108.                 return stillFighting;
  109.             }
  110.         }
  111.     }
  112.  
  113.     class Soldier
  114.     {
  115.         public int Health { get; private set; }
  116.         public int Damage { get; private set; }
  117.  
  118.         public bool isAlive { get; private set; }
  119.  
  120.         public Soldier(int health, int damage)
  121.         {
  122.             Health = health;
  123.             Damage = damage;
  124.             isAlive = true;
  125.         }
  126.  
  127.         public void TakeDamage(int amount)
  128.         {
  129.             Health -= amount;
  130.         }
  131.  
  132.         public void Die()
  133.         {
  134.             Damage = 0;
  135.             isAlive = false;
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement