Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace OOPTask10War
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- War war = new War();
- war.Initiate();
- }
- }
- public class War
- {
- private Squad _squad1 = new Squad();
- private Squad _squad2 = new Squad();
- public void Initiate()
- {
- ShowStartInfo();
- Fight();
- ShowFinishInfo();
- }
- private void Fight()
- {
- while (_squad1.IsAlive && _squad2.IsAlive)
- {
- Console.Clear();
- Console.WriteLine("Взвод 1 атакует!\n");
- _squad1.Attack(_squad2);
- Console.WriteLine("\nВзвод 2 атакует!\n");
- _squad2.Attack(_squad1);
- _squad1.BuryDiedWarriors();
- _squad2.BuryDiedWarriors();
- Console.Write("\nзвод 1: ");
- _squad1.ShowShortInfo();
- Console.Write("\nзвод 2: ");
- _squad2.ShowShortInfo();
- Console.ReadKey();
- }
- }
- private void ShowStartInfo()
- {
- Console.WriteLine("Столкновение неизбежно...");
- Console.Write("\nВзвод 1: ");
- _squad1.ShowInfo();
- Console.Write("\nВзвод 2: ");
- _squad2.ShowInfo();
- Console.WriteLine("\nДля начала и продолжения столкновения нажимайте любую клавишу");
- Console.ReadKey();
- }
- private void ShowFinishInfo()
- {
- Console.Clear();
- Console.WriteLine("Схватка окончена...");
- if(_squad1.IsAlive == false && _squad2.IsAlive == false)
- {
- Console.WriteLine("\nВ войне нет победителей...\nСдохли все...\n");
- }
- else if (_squad1.IsAlive == false)
- {
- Console.WriteLine("Победил взвод 2: ");
- _squad2.ShowInfo();
- }
- else if (_squad2.IsAlive == false)
- {
- Console.WriteLine("Победил взвод 1: ");
- _squad1.ShowInfo();
- }
- Console.WriteLine();
- Console.ReadKey();
- }
- }
- public interface IDamagableWarrior
- {
- void TakeDamage(int damage);
- }
- public interface IDamagableSquad
- {
- IDamagableWarrior GetRandomWarrior();
- List<IDamagableWarrior> GetRandomWarriors(int count);
- }
- public class Squad : IDamagableSquad
- {
- private const int MinimumWarriorsCount = 10;
- private const int MaximumWarriorsCount = 30;
- private const int MinimumWarriorHealth = 1000;
- private const int MaximumWarriorHealth = 2000;
- private const int MinimumWarriorArmor = 30;
- private const int MaximumWarriorArmor = 100;
- private const int MinimumWarriorDamage = 70;
- private const int MaximumWarriorDamage = 150;
- private List<Warrior> _warriors = new List<Warrior>();
- public Squad()
- {
- CreateWarriors();
- }
- public bool IsAlive => _warriors.Count > 0;
- public void BuryDiedWarriors()
- {
- List<Warrior> diedWarriors = new List<Warrior>();
- foreach(Warrior warrior in _warriors)
- {
- if (warrior.IsAlive == false)
- diedWarriors.Add(warrior);
- }
- foreach(Warrior warrior in diedWarriors)
- {
- _warriors.Remove(warrior);
- }
- }
- private void CreateWarriors()
- {
- List<Warrior> warriorTypes = new List<Warrior>();
- warriorTypes.Add(new Warrior(0, 0, 0));
- warriorTypes.Add(new StrongWarrior(0, 0, 0));
- warriorTypes.Add(new MultipleWarrior(0, 0, 0));
- warriorTypes.Add(new FastWarrior(0, 0, 0));
- int warriorsCount = UserUtils.GenerateRandomNumber(MinimumWarriorsCount, MaximumWarriorsCount);
- for (int i = 0; i < warriorsCount; i++)
- {
- _warriors.Add(warriorTypes[UserUtils.GenerateRandomNumber(0, warriorTypes.Count)].
- CreateNewWarrior(UserUtils.GenerateRandomNumber(MinimumWarriorHealth, MaximumWarriorHealth),
- UserUtils.GenerateRandomNumber(MinimumWarriorArmor, MaximumWarriorArmor),
- UserUtils.GenerateRandomNumber(MinimumWarriorDamage, MaximumWarriorDamage)
- ));
- }
- }
- public IDamagableWarrior GetRandomWarrior()
- {
- return _warriors[UserUtils.GenerateRandomNumber(0, _warriors.Count)];
- }
- public List<IDamagableWarrior> GetRandomWarriors(int count)
- {
- List<IDamagableWarrior> randomWarriors = new List<IDamagableWarrior>();
- if (count > _warriors.Count)
- count = _warriors.Count;
- while (randomWarriors.Count < count)
- {
- IDamagableWarrior warrior = GetRandomWarrior();
- if (randomWarriors.Contains(warrior) == false)
- randomWarriors.Add(warrior);
- }
- return randomWarriors;
- }
- public void ShowShortInfo()
- {
- Console.WriteLine($"во взводе {_warriors.Count} воинов");
- }
- public void ShowInfo()
- {
- ShowShortInfo();
- Console.WriteLine();
- foreach (Warrior warrior in _warriors)
- {
- warrior.ShowInfo();
- }
- }
- public void Attack(IDamagableSquad squad)
- {
- foreach(Warrior warrior in _warriors)
- {
- warrior.Attack(squad);
- }
- }
- }
- public class Warrior: IDamagableWarrior
- {
- protected string Title;
- protected string Description;
- protected int Health;
- protected int Armor;
- protected int Damage;
- public Warrior(int health, int armor, int damage)
- {
- Health = health;
- Armor = armor;
- Damage = damage;
- Title = "простой воин";
- Description = "атакует одного противника обычной атакой";
- }
- public bool IsAlive => Health > 0;
- public void ShowInfo()
- {
- Console.WriteLine($"{Title} - {Description}, Здоровье: {Health}, Броня: {Armor}, Атака: {Damage}");
- }
- public virtual void TakeDamage(int damage)
- {
- int takenDamage = GetTakenDamage(damage);
- Console.WriteLine($"\"{Title}\" защищается и получает урон: {takenDamage}");
- Health -= takenDamage;
- }
- private int GetTakenDamage(int damage)
- {
- int takenDamage = damage - Armor;
- if (takenDamage < 0)
- takenDamage = 0;
- return takenDamage;
- }
- public virtual void Attack(IDamagableSquad squad)
- {
- Console.WriteLine($"Боец \"{Title}\" наносит урон: {Damage}");
- squad.GetRandomWarrior().TakeDamage(Damage);
- }
- public virtual Warrior CreateNewWarrior(int health, int armor, int damage)
- {
- return new Warrior(health, armor, damage);
- }
- }
- public class StrongWarrior : Warrior
- {
- private const int MinimumDamagePercent = 110;
- private const int MaximumDamagePercent = 200;
- private const int PercentScale = 100;
- public StrongWarrior(int health, int armor, int damage): base(health, armor, damage)
- {
- Title = "сильный воин";
- Description = "атакует одного противника мощной атакой";
- }
- public override void Attack(IDamagableSquad squad)
- {
- int damage = Damage * UserUtils.GenerateRandomNumber(MinimumDamagePercent, MaximumDamagePercent) / PercentScale;
- Console.WriteLine($"\"{Title}\" наносит увеличенный урон: {damage}");
- squad.GetRandomWarrior().TakeDamage(damage);
- }
- public override Warrior CreateNewWarrior(int health, int armor, int damage)
- {
- return new StrongWarrior(health, armor, damage);
- }
- }
- public class MultipleWarrior : Warrior
- {
- private const int MinimumTargetCount = 2;
- private const int MaximumTargetCount = 6;
- public MultipleWarrior(int health, int armor, int damage) : base(health, armor, damage)
- {
- Title = "массовый воин";
- Description = "атакует нескольких противников обычной атакой";
- }
- public override void Attack(IDamagableSquad squad)
- {
- List<IDamagableWarrior> warriors = squad.GetRandomWarriors(UserUtils.GenerateRandomNumber(MinimumTargetCount, MaximumTargetCount));
- Console.WriteLine($"\"{Title}\" наносит {warriors.Count} врагам обычный урон: {Damage}");
- foreach (IDamagableWarrior warrior in warriors)
- {
- warrior.TakeDamage(Damage);
- }
- }
- public override Warrior CreateNewWarrior(int health, int armor, int damage)
- {
- return new MultipleWarrior(health, armor, damage);
- }
- }
- public class FastWarrior : Warrior
- {
- private const int MinimumAttackCount = 2;
- private const int MaximumAttackCount = 6;
- public FastWarrior(int health, int armor, int damage) : base(health, armor, damage)
- {
- Title = "быстрый воин";
- Description = "успевает атаковать несколько раз обычной атакой";
- }
- public override void Attack(IDamagableSquad squad)
- {
- int attackCount = UserUtils.GenerateRandomNumber(MinimumAttackCount, MaximumAttackCount);
- Console.WriteLine($"\"{Title}\" проводит {attackCount} атак врагам с обычным уроном: {Damage}");
- for(int i = 0; i < attackCount; i++)
- {
- squad.GetRandomWarrior().TakeDamage(Damage);
- }
- }
- public override Warrior CreateNewWarrior(int health, int armor, int damage)
- {
- return new FastWarrior(health, armor, damage);
- }
- }
- public class UserUtils
- {
- private static Random s_random = new Random();
- public static int GenerateRandomNumber(int minimumNumber, int maximumNumber)
- {
- return s_random.Next(minimumNumber, maximumNumber);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement