Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection.PortableExecutable;
- using System.Runtime.CompilerServices;
- namespace WorldWar
- {
- class Program
- {
- static void Main(string[] args)
- {
- Army army1 = new Army("Kingdom");
- army1.CreateUnit(10);
- Army army2 = new Army("NeKingdom");
- army2.CreateUnit(10);
- Fight(army1, army2);
- }
- public static void Fight(Army army1, Army army2)
- {
- while (army1.CheckDeaths() && army2.CheckDeaths())
- {
- army2.GotShot(army1.Shoot());
- Console.WriteLine("Pif paf!");
- army2.CheckDeaths();
- army1.GotShot(army2.Shoot());
- Console.WriteLine("Pif paf!");
- army1.CheckDeaths();
- }
- if (army1.CheckDeaths() == false)
- {
- Console.WriteLine("Страна " + army1.Name + " повержена");
- }
- else
- {
- Console.WriteLine("Страна " + army2.Name + " повержена");
- }
- }
- }
- class Army
- {
- private List<Soldier> _unit = new List<Soldier>();
- public string Name { get; private set; }
- public Army(string name)
- {
- List<Soldier> _unit = new List<Soldier>();
- Name = name;
- }
- public void CreateUnit(int amount)
- {
- for (int i = 0; i < amount; i++)
- {
- Random rand = new Random();
- Soldier newSoldier = new Soldier(rand.Next(200, 500), rand.Next(10, 30));
- _unit.Add(newSoldier);
- }
- }
- public List<int> Shoot()
- {
- List<int> unitDamage = new List<int>();
- for (int i = 0; i < _unit.Count; i++)
- {
- unitDamage.Add(_unit[i].Damage);
- }
- return unitDamage;
- }
- public void GotShot(List<int> damage)
- {
- Random rand = new Random();
- for (int i = 0; i < _unit.Count; i++)
- {
- _unit[rand.Next(0, _unit.Count)].TakeDamage(damage[i]);
- }
- }
- public bool CheckDeaths()
- {
- bool stillFighting;
- foreach (var item in _unit)
- {
- if (item.Health <= 0)
- {
- item.Die();
- }
- }
- int corps = 0;
- foreach (var item in _unit)
- {
- if (item.isAlive == false)
- {
- corps++;
- }
- }
- if (corps < 10)
- {
- stillFighting = true;
- return stillFighting;
- }
- else
- {
- stillFighting = false;
- return stillFighting;
- }
- }
- }
- class Soldier
- {
- public int Health { get; private set; }
- public int Damage { get; private set; }
- public bool isAlive { get; private set; }
- public Soldier(int health, int damage)
- {
- Health = health;
- Damage = damage;
- isAlive = true;
- }
- public void TakeDamage(int amount)
- {
- Health -= amount;
- }
- public void Die()
- {
- Damage = 0;
- isAlive = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement