Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Classes
- {
- internal class Program
- {
- static void Main()
- {
- Battle battle = new Battle();
- battle.Fight();
- }
- }
- class Battle
- {
- private Platoon[] _platoons;
- private List<Soldier> _soldiers;
- public Battle()
- {
- _soldiers = new List<Soldier>()
- {
- new SimpleSoldier(),
- new SimpleMultipleSoldier(),
- new ShotgunSoldier(),
- new MachineGunSoldier()
- };
- _platoons = new Platoon[2]
- {
- new Platoon(_soldiers),
- new Platoon(_soldiers)
- };
- }
- public void Fight()
- {
- int indexFirstPlat = 0;
- int indexSecondPlat = 1;
- _platoons[indexFirstPlat].ShowAlive(indexFirstPlat);
- _platoons[indexSecondPlat].ShowAlive(indexSecondPlat);
- Console.WriteLine("Press something...");
- Console.ReadKey();
- bool haveAlive = true;
- int turnCount = 1;
- int maxTurnCount = 20;
- while (haveAlive)
- {
- List<Soldier> survivorsPlatoon0 = _platoons[indexFirstPlat].GetAlive();
- List<Soldier> survivorsPlatoon1 = _platoons[indexSecondPlat].GetAlive();
- Attack(survivorsPlatoon0, survivorsPlatoon1);
- Attack(survivorsPlatoon1, survivorsPlatoon0);
- Console.Clear();
- _platoons[indexFirstPlat].ShowAlive(indexFirstPlat);
- _platoons[indexSecondPlat].ShowAlive(indexSecondPlat);
- int leftPosition = 0;
- Console.SetCursorPosition(leftPosition, Math.Max(_platoons[indexFirstPlat].GetAlive().Count, _platoons[indexSecondPlat].GetAlive().Count));
- Console.WriteLine("Press something...");
- Console.ReadKey();
- haveAlive = _platoons[indexFirstPlat].HaveAlive() && _platoons[indexSecondPlat].HaveAlive();
- if (turnCount++ > maxTurnCount)
- {
- haveAlive = false;
- }
- }
- DecideWinner(_platoons);
- }
- private void DecideWinner(Platoon[] platoons)
- {
- Console.ForegroundColor = ConsoleColor.DarkYellow;
- if (platoons[0].HaveAlive() && platoons[1].HaveAlive())
- {
- Console.WriteLine($"\nDraw!");
- }
- else if (platoons[0].HaveAlive() == false && platoons[1].HaveAlive() == false)
- {
- Console.WriteLine($"\nDraw!");
- }
- else if (platoons[0].HaveAlive())
- {
- Console.WriteLine($"\nLeft platoon wins!");
- }
- else if (platoons[1].HaveAlive())
- {
- Console.WriteLine($"\nRight platoon wins!");
- }
- Console.ResetColor();
- }
- private void Attack(List<Soldier> attackPlatoon, List<Soldier> opponentPlatoon)
- {
- foreach (var soldier in attackPlatoon)
- {
- {
- soldier.Attack(opponentPlatoon);
- }
- }
- }
- }
- class Platoon
- {
- private List<Soldier> _soldiers;
- public Platoon(List<Soldier> soldiers)
- {
- Create(soldiers);
- }
- public List<Soldier> GetAlive()
- {
- List<Soldier> aliveSoldiers = new List<Soldier>();
- foreach (var soldier in _soldiers)
- {
- if (soldier.IsAlive)
- {
- aliveSoldiers.Add(soldier);
- }
- }
- return aliveSoldiers;
- }
- public bool HaveAlive()
- {
- bool haveAlive = _soldiers.Any(soldiers => soldiers.IsAlive);
- return haveAlive;
- }
- public void ShowAlive(int index)
- {
- var aliveSoldiers = _soldiers.Where(soldiers => soldiers.IsAlive).OrderBy(soldiers => soldiers.Type).ThenBy(soldiers => soldiers.Health);
- int topPosition = 0;
- int leftIndent = 35;
- int leftIndentDefault = 0;
- int leftPosition = index == 1 ? leftIndent : leftIndentDefault;
- foreach (var soldier in aliveSoldiers)
- {
- switch (soldier.Type)
- {
- case Type.Simple:
- Console.ForegroundColor = ConsoleColor.DarkRed;
- break;
- case Type.SimpleMultiple:
- Console.ForegroundColor = ConsoleColor.DarkGreen;
- break;
- case Type.Shotgun:
- Console.ForegroundColor = ConsoleColor.DarkMagenta;
- break;
- case Type.MachineGun:
- Console.ForegroundColor = ConsoleColor.DarkYellow;
- break;
- }
- Console.SetCursorPosition(leftPosition, topPosition++);
- Console.WriteLine($"{soldier.Type} - health: {soldier.Health}");
- Console.ResetColor();
- }
- }
- private void Create(List<Soldier> soldiers)
- {
- _soldiers = new List<Soldier>();
- int platoonSize = 28;
- // для корректного отображения макс = 29 по объему буфера
- for (int i = 0; i < platoonSize; i++)
- {
- int typeSoldier = UserUtil.GenerateRandomNumber(soldiers.Count);
- _soldiers.Add(soldiers[typeSoldier].Clone());
- }
- }
- }
- interface IDamageable
- {
- void TakeDamage(int damage);
- }
- abstract class Soldier : IDamageable
- {
- public Soldier()
- {
- Initialize();
- }
- public Type Type { get; protected set; }
- public int Health { get; protected set; }
- public int Damage { get; protected set; }
- public int Armor { get; protected set; }
- public bool IsAlive => Health > 0;
- public virtual void Attack(List<Soldier> damageable)
- {
- List<IDamageable> damageableSoldiers = new List<IDamageable>();
- Soldier damageableSoldier = damageable[UserUtil.GenerateRandomNumber(damageable.Count)];
- damageableSoldiers.Add(damageableSoldier);
- foreach (var soldier in damageableSoldiers)
- {
- soldier.TakeDamage(Damage);
- }
- }
- public virtual void TakeDamage(int damage)
- {
- Health -= damage - Armor;
- }
- abstract public Soldier Clone();
- private void Initialize()
- {
- int minHealth = 8;
- int maxHealth = 11;
- int minDamage = 3;
- int maxDamage = 5;
- int minArmor = 1;
- int maxArmor = 3;
- Health = UserUtil.GenerateRandomNumber(minHealth, maxHealth + 1);
- Damage = UserUtil.GenerateRandomNumber(minDamage, maxDamage + 1);
- Armor = UserUtil.GenerateRandomNumber(minArmor, maxArmor + 1);
- }
- }
- class SimpleSoldier : Soldier
- {
- public SimpleSoldier()
- {
- Type = Type.Simple;
- }
- public override Soldier Clone()
- {
- return new SimpleSoldier();
- }
- }
- class SimpleMultipleSoldier : Soldier
- {
- public SimpleMultipleSoldier()
- {
- Type = Type.SimpleMultiple;
- }
- public override void TakeDamage(int damage)
- {
- int damageModifier = 2;
- Health -= (damage - Armor) * damageModifier;
- }
- public override Soldier Clone()
- {
- return new SimpleMultipleSoldier();
- }
- }
- class ShotgunSoldier : Soldier
- {
- public ShotgunSoldier()
- {
- Type = Type.Shotgun;
- }
- public override void Attack(List<Soldier> damageable)
- {
- int damageableCount = 3;
- if (damageable.Count < damageableCount)
- {
- damageableCount = damageable.Count;
- }
- List<IDamageable> damageableSoldiers = new List<IDamageable>();
- for (int i = 0; i < damageableCount; i++)
- {
- Soldier damageableSoldier = damageable[UserUtil.GenerateRandomNumber(damageable.Count)];
- if (damageableSoldiers.Contains(damageableSoldier))
- {
- i--;
- }
- else
- {
- damageableSoldiers.Add(damageableSoldier);
- }
- }
- foreach (var soldier in damageableSoldiers)
- {
- soldier.TakeDamage(Damage);
- }
- }
- public override Soldier Clone()
- {
- return new ShotgunSoldier();
- }
- }
- class MachineGunSoldier : Soldier
- {
- public MachineGunSoldier()
- {
- Type = Type.MachineGun;
- }
- public override void Attack(List<Soldier> damageable)
- {
- int damageableCount = 5;
- List<IDamageable> damageableSoldiers = new List<IDamageable>();
- for (int i = 0; i < damageableCount; i++)
- {
- Soldier damageableSoldier = damageable[UserUtil.GenerateRandomNumber(damageable.Count)];
- damageableSoldiers.Add(damageableSoldier);
- }
- foreach (var soldier in damageableSoldiers)
- {
- soldier.TakeDamage(Damage);
- }
- }
- public override Soldier Clone()
- {
- return new MachineGunSoldier();
- }
- }
- enum Type
- {
- Simple,
- SimpleMultiple,
- Shotgun,
- MachineGun
- }
- class UserUtil
- {
- private static Random s_random = new Random();
- public static int GenerateRandomNumber(int min, int max)
- {
- return s_random.Next(min, max);
- }
- public static int GenerateRandomNumber(int max)
- {
- return s_random.Next(max);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement