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.Runtime.CompilerServices;
- class Program
- {
- static void Main(string[] args)
- {
- BattelField battelField = new BattelField();
- battelField.Game();
- }
- }
- class BattelField
- {
- private List<Warrior> _defaultWarriors = new List<Warrior>();
- public BattelField()
- {
- FillDefaultWarriors();
- }
- public void Game()
- {
- Squad firstSquad;
- Squad secondSquad;
- string squadName;
- bool isEndFight = false;
- Console.WriteLine("\tДобро пожаловать на войну \"Team Fortress 2\".");
- Console.Write("\nВведи как называется первое отделение? ");
- squadName = Console.ReadLine();
- firstSquad = CreateSquad(squadName);
- Console.Write("\nВведи как называется второе отделение? ");
- squadName = Console.ReadLine();
- secondSquad = CreateSquad(squadName);
- Console.WriteLine("Бой начинается!\n");
- System.Threading.Thread.Sleep(1000);
- while (isEndFight == false)
- {
- StartFight(firstSquad, secondSquad);
- if (firstSquad.IsAlive == false && secondSquad.IsAlive == false)
- isEndFight = true;
- }
- TryPickingWinner(firstSquad, secondSquad);
- Console.WriteLine("Игра завершена.");
- }
- private void FillDefaultWarriors()
- {
- _defaultWarriors.Add(new Soldier("Солдат", 200, 50, 0.2f));
- _defaultWarriors.Add(new HeavyWarrior("Хейви", 300, 25, 0.4f));
- _defaultWarriors.Add(new Sniper("Снайпер", 125, 105, 0));
- _defaultWarriors.Add(new Demoman("Демоман", 185, 70, 0.2f));
- _defaultWarriors.Add(new Scout("Скаут", 125, 10, 0.4f));
- }
- private Squad CreateSquad(string squadName)
- {
- int сountWarriors;
- Warrior userSelectWarrior;
- Console.Write($"\nВыберите из каких солдатов будет состоять {squadName} отделение:");
- userSelectWarrior = SelectWarrior();
- Console.Write($"\nСколько единиц будет в {squadName} отделении? ");
- сountWarriors = SelectCount();
- return new Squad(userSelectWarrior, сountWarriors, squadName);
- }
- private Warrior SelectWarrior()
- {
- bool isSelectWarrior = false;
- Warrior selectWarrior = null;
- ShowAllWarriors();
- while (isSelectWarrior == false)
- {
- int.TryParse(Console.ReadLine(), out int userWarriorSellected);
- isSelectWarrior = userWarriorSellected > 0 && userWarriorSellected <= _defaultWarriors.Count;
- if (isSelectWarrior)
- {
- selectWarrior = _defaultWarriors[userWarriorSellected - 1];
- }
- else
- {
- Console.Write("\aТакого бойца в списке нет, или нужно вводить число. Попробуйте еще раз: ");
- }
- }
- return new Warrior(selectWarrior.Name, selectWarrior.Health, selectWarrior.Damage, selectWarrior.Defence);
- }
- private void ShowAllWarriors()
- {
- int counter = 1;
- Console.WriteLine("\n Список воинов:");
- foreach (Warrior warrior in _defaultWarriors)
- {
- Console.Write(counter + " ");
- warrior.ShowStats();
- counter++;
- }
- }
- private void TryPickingWinner(Squad firstSquad, Squad secondSquad)
- {
- if (firstSquad.IsAlive == false && secondSquad.IsAlive == false)
- {
- Console.WriteLine("Погибли оба отделения. Победителей нет.");
- }
- else if (firstSquad.IsAlive == false)
- {
- Console.WriteLine($"\nПобедил {secondSquad.Name}! Поздравляем победителя!");
- }
- else if (secondSquad.IsAlive == false)
- {
- Console.WriteLine($"\nПобедил {firstSquad.Name}! Поздравляем победителя!");
- }
- }
- private int SelectCount()
- {
- bool isCorrectInput = false;
- int result = 0;
- while (isCorrectInput == false)
- {
- isCorrectInput = int.TryParse(Console.ReadLine(), out result);
- if (result <= 0)
- {
- isCorrectInput = false;
- Console.WriteLine("\aВведите положительное число!");
- }
- }
- return result;
- }
- private void StartFight(Squad firstSquad, Squad secondSquad)
- {
- firstSquad.Attack(secondSquad);
- secondSquad.Attack(firstSquad);
- firstSquad.TryRemoveDeadSoldiers();
- secondSquad.TryRemoveDeadSoldiers();
- firstSquad.ShowCountWarrior();
- secondSquad.ShowCountWarrior();
- System.Threading.Thread.Sleep(100);
- Console.WriteLine();
- }
- }
- class Warrior
- {
- private Random _random = new Random();
- public Warrior(string name, int health, int damage, float defence)
- {
- Name = name;
- Health = health;
- Damage = damage;
- Defence = defence;
- }
- public string Name { get; protected set; }
- public int Health { get; protected set; }
- public int Damage { get; protected set; }
- public float Defence { get; protected set; }
- public virtual void TakeDamage(int damage)
- {
- if ((damage - Defence) > 0)
- Health -= damage * Convert.ToInt32((1 - Defence));
- }
- public virtual void Attack(Warrior entity)
- {
- entity.TakeDamage(Damage);
- }
- public void ShowCurrentHealth()
- {
- Console.WriteLine($"{Name} здоровье: {Health}");
- }
- public void ShowStats()
- {
- Console.WriteLine($"{Name}: Здоровье - {Health}, Урон - {Damage}, Броня - {Defence}");
- }
- protected bool CanToUseSkill(int deltaChanceSuccess = 0)
- {
- int minimumValue = 0;
- int maximumValue = 101;
- int defaultChanceSuccess = 20;
- return _random.Next(minimumValue, maximumValue) <= defaultChanceSuccess + deltaChanceSuccess;
- }
- }
- class Soldier : Warrior
- {
- public Soldier(string name, int health, int damage, float defence) : base(name, health, damage, defence) { }
- public override void Attack(Warrior warrior)
- {
- float multiplyingFactor = 2.5F;
- int deltaChanceSuccess = 10;
- int takeDamageFromYourShots = 50;
- if (CanToUseSkill(deltaChanceSuccess))
- {
- warrior.TakeDamage(Convert.ToInt32(Damage * multiplyingFactor));
- Health -= takeDamageFromYourShots;
- }
- else
- {
- base.Attack(warrior);
- }
- }
- }
- class HeavyWarrior : Warrior
- {
- public HeavyWarrior(string name, int health, int damage, float defence) : base(name, health, damage, defence) { }
- public override void Attack(Warrior warrior)
- {
- int defaultChanceSuccess = -10;
- int recoveryHealth = 50;
- if (CanToUseSkill(defaultChanceSuccess))
- Health += recoveryHealth;
- base.Attack(warrior);
- }
- }
- class Sniper : Warrior
- {
- public Sniper(string name, int health, int damage, float defence) : base(name, health, damage, defence) { }
- public override void Attack(Warrior warrior)
- {
- float multiplyingFactor = 1.8F;
- int deltaChanceSuccess = 5;
- if (CanToUseSkill(deltaChanceSuccess))
- warrior.TakeDamage(Convert.ToInt32(Damage * multiplyingFactor));
- else
- base.Attack(warrior);
- }
- }
- class Demoman : Warrior
- {
- private int _gunReloading;
- public Demoman(string name, int health, int damage, float defence, int _gunReloading = 0) : base(name, health, damage, defence) { }
- public override void Attack(Warrior warrior)
- {
- int deltaChanceSuccess = 30;
- int coldownGunReloadingInTurns = 3;
- if (_gunReloading > 0)
- _gunReloading--;
- if (CanToUseSkill(deltaChanceSuccess) && _gunReloading == 0)
- {
- warrior.TakeDamage(Damage);
- warrior.TakeDamage(Damage);
- _gunReloading = coldownGunReloadingInTurns;
- }
- else
- {
- Console.WriteLine(Name + " не смог выстрелить, кроме того осталось еще " + _gunReloading + " ходов перезарядки.");
- }
- }
- }
- class Scout : Warrior
- {
- public Scout(string name, int health, int damage, float defence) : base(name, health, damage, defence) { }
- public override void TakeDamage(int damage)
- {
- int deltaChanceSuccess = 40;
- if (CanToUseSkill(deltaChanceSuccess))
- {
- Console.WriteLine(Name + " увернулся от удара.");
- }
- else
- {
- base.TakeDamage(damage);
- }
- }
- }
- class Squad
- {
- private Random _random = new Random();
- private List<Warrior> _soldiers = new List<Warrior>();
- public Squad(Warrior warrior, int warriorsCount, string name)
- {
- Name = name;
- for (int i = 0; i < warriorsCount; i++)
- {
- _soldiers.Add(new Warrior(warrior.Name, warrior.Health, warrior.Damage, warrior.Defence));
- }
- }
- public string Name { get; private set; }
- public bool IsAlive
- {
- get
- {
- return _soldiers.Count > 0;
- }
- }
- public Warrior GetWarrior()
- {
- return _soldiers[_random.Next(0, _soldiers.Count())];
- }
- public void Attack(Squad enemy)
- {
- GetWarrior().Attack(enemy.GetWarrior());
- }
- public void TryRemoveDeadSoldiers()
- {
- for (int i = _soldiers.Count - 1; i >= 0; i--)
- {
- if (_soldiers[i].Health <= 0)
- {
- _soldiers.RemoveAt(i);
- i--;
- }
- }
- }
- public void ShowCountWarrior()
- {
- Console.WriteLine($"Количество бойцов в отделении {Name}: {_soldiers.Count()} ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement