Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Threading;
- public class Program
- {
- public static void Main()
- {
- War war = new War();
- bool isFighting = true;
- long amountTurns = 1;
- int timeSleepAmount = 500;
- while (isFighting)
- {
- war.Update();
- Console.WriteLine($"Turn is : {amountTurns}");
- isFighting = war.IsFighting();
- ++amountTurns;
- Thread.Sleep(timeSleepAmount);
- }
- }
- }
- public class War
- {
- private Random _random;
- private Country _countryFirst;
- private Country _countrySecond;
- private bool _isFirstCountry;
- private int _percentRangeRandom = 100;
- private int _percentFirstCountry = 50;
- public War()
- {
- _random = new Random();
- _isFirstCountry = true;
- _countryFirst = new Country(_random);
- _countrySecond = new Country(_random);
- }
- public void Update()
- {
- Country countryAttack = null;
- Country countryDefend = null;
- Console.Clear();
- if (_isFirstCountry)
- {
- countryAttack = _countryFirst;
- countryDefend = _countrySecond;
- Console.WriteLine("First country turn.");
- }
- else
- {
- countryAttack = _countrySecond;
- countryDefend = _countryFirst;
- Console.WriteLine("Second country turn.");
- }
- Console.WriteLine($"Country attack : {countryAttack}");
- Console.WriteLine($"Country deffender : {countryDefend}");
- int trooperAttack = _random.Next(countryAttack.TroopersAmount);
- int trooperDefend = _random.Next(countryDefend.TroopersAmount);
- if (countryAttack.IsMedic(trooperAttack))
- {
- int trooperHeal = _random.Next(countryAttack.TroopersAmount);
- countryAttack.AttackMedic(trooperAttack, countryDefend.GetTrooper(trooperDefend),
- countryAttack.GetTrooper(trooperHeal));
- }
- else
- {
- countryAttack.Attack(trooperAttack, countryDefend.GetTrooper(trooperDefend));
- }
- countryDefend.CheckHealth(trooperDefend);
- _isFirstCountry = ( _random.Next(_percentRangeRandom) / _percentFirstCountry ) == 1;
- }
- public bool IsFighting()
- {
- if (_countryFirst.isAllDead())
- {
- Console.WriteLine("Second country win!");
- Console.WriteLine($"{_countrySecond}");
- return false;
- }
- else if (_countrySecond.isAllDead())
- {
- Console.WriteLine("First country win!");
- Console.WriteLine($"{_countryFirst}");
- return false;
- }
- else
- {
- return true;
- }
- }
- }
- public class Country
- {
- private List<Trooper> _troopers;
- private Random _random;
- private int _troopersAmountMinimum = 20;
- private int _troopersAmountMaximum = 100;
- private int _trooperTypeAmount = 3;
- public int TroopersAmount => _troopers.Count;
- public Country(Random random)
- {
- _random = random;
- _troopers = new List<Trooper>();
- CreateTroopers();
- }
- public bool IsMedic(int index)
- {
- return _troopers[index] is Medic;
- }
- public bool isAllDead()
- {
- return _troopers.Count <= 0;
- }
- public Trooper GetTrooper(int index)
- {
- return _troopers[index];
- }
- public void Attack(int trooperAttack, Trooper enemy)
- {
- _troopers[trooperAttack].Attack(enemy);
- }
- public void AttackMedic(int trooperAttack, Trooper enemy, Trooper heal)
- {
- Medic medic = (Medic) _troopers[trooperAttack];
- medic.Attack(enemy);
- medic.IncreaseHealth(heal);
- }
- public void CheckHealth(int trooper)
- {
- if (_troopers[trooper].GetHealth() <= 0)
- _troopers.RemoveAt(trooper);
- }
- public override string ToString()
- {
- string result = "";
- result += "Amount of troopers : " + TroopersAmount + ".\n";
- return result;
- }
- private void CreateTroopers()
- {
- int trooperAmount = _random.Next(_troopersAmountMinimum, _troopersAmountMaximum);
- Trooper trooper = null;
- int trooperType;
- for (int i = 0; i < trooperAmount; ++i)
- {
- trooperType = _random.Next(_trooperTypeAmount);
- switch(trooperType)
- {
- case 0:
- trooper = new Striker(_random);
- break;
- case 1:
- trooper = new Sniper(_random);
- break;
- case 2:
- trooper = new Medic(_random);
- break;
- }
- _troopers.Add(trooper);
- }
- }
- }
- public abstract class Trooper
- {
- protected int Health;
- protected int Damage;
- protected Trooper()
- {
- }
- public void TakeDamage(int damage)
- {
- Health -= damage;
- }
- public void Attack(Trooper trooper)
- {
- trooper.TakeDamage(Damage);
- }
- public void IncreaseHealth(int health)
- {
- Health += health;
- }
- public int GetHealth()
- {
- return Health;
- }
- }
- public class Sniper : Trooper
- {
- private int _healthMinimum = 100;
- private int _healthMaximum = 200;
- private int _damageMinimum = 300;
- private int _damageMaximum = 500;
- public Sniper(Random random)
- {
- Health = random.Next(_healthMinimum, _healthMaximum);
- Damage = random.Next(_damageMinimum, _damageMaximum);
- }
- }
- public class Striker : Trooper
- {
- private int _healthMinimum = 150;
- private int _healthMaximum = 250;
- private int _damageMinimum = 200;
- private int _damageMaximum = 350;
- public Striker(Random random)
- {
- Health = random.Next(_healthMinimum, _healthMaximum);
- Damage = random.Next(_damageMinimum, _damageMaximum);
- }
- }
- public class Medic : Trooper
- {
- private int _healthMinimum = 75;
- private int _healthMaximum = 125;
- private int _damageMinimum = 150;
- private int _damageMaximum = 200;
- public Medic(Random random)
- {
- Health = random.Next(_healthMinimum, _healthMaximum);
- Damage = random.Next(_damageMinimum, _damageMaximum);
- }
- public void IncreaseHealth(Trooper ally)
- {
- ally.IncreaseHealth(Damage);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement