Vapio

task32

May 21st, 2021 (edited)
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.14 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. public class Program
  5. {
  6.     public static void Main()
  7.     {
  8.         Arena arena = new Arena();
  9.         bool isFighting = true;
  10.         const int TimeWait = 800;
  11.         string name;
  12.  
  13.         while (isFighting)
  14.         {
  15.             arena.Update();
  16.  
  17.             if (arena.IsWin(out name))
  18.             {
  19.                 Console.WriteLine($"Player : {name} win.");
  20.                 isFighting = false;
  21.             }
  22.  
  23.             Thread.Sleep(TimeWait);
  24.         }
  25.     }
  26. }
  27.  
  28. public class Arena
  29. {
  30.     private Gladiator _playerFirst;
  31.     private Gladiator _playerSecond;
  32.  
  33.     private Gladiator[] _gladiators;
  34.     private readonly Random _random;
  35.     private bool _isFirstPlayerTurn;
  36.     private int _turnAmount;
  37.  
  38.     private int _healthMinimum = 15;
  39.     private int _healthMaximum = 300;
  40.     private int _damageMinimum = 40;
  41.     private int _damageMaximum = 150;
  42.     private int _archetypeMinimum = 1;
  43.     private int _archetypeMaximum = 3;
  44.  
  45.     private int _gladiatorsAmount = 5;
  46.     private int _turnsTimeReload = 3;
  47.     private int _turnsTimeStun = 2;
  48.  
  49.     public Arena()
  50.     {
  51.         _random = new Random();
  52.         _isFirstPlayerTurn = true;
  53.         _turnAmount = 1;
  54.         _playerFirst = ChooseGladiator();
  55.         _playerSecond = ChooseGladiator();
  56.     }
  57.  
  58.     public void Update()
  59.     {
  60.         Console.Clear();
  61.  
  62.         if (_isFirstPlayerTurn)
  63.         {
  64.             PrintPlayer(_playerFirst, _playerSecond);
  65.             AttackPlayer(_playerFirst, _playerSecond); ;
  66.             _isFirstPlayerTurn = false;
  67.         }
  68.         else
  69.         {
  70.             PrintPlayer(_playerSecond, _playerFirst);
  71.             AttackPlayer(_playerSecond, _playerFirst);
  72.             _isFirstPlayerTurn = true;
  73.         }
  74.  
  75.         CheckTurns(_playerSecond);
  76.         CheckTurns(_playerFirst);
  77.         ++_turnAmount;
  78.     }
  79.  
  80.     public bool IsWin(out string name)
  81.     {
  82.         name = "";
  83.  
  84.         if (_playerSecond.IsDead())
  85.         {
  86.             name = _playerFirst.Name;
  87.             return true;
  88.         }
  89.         else if (_playerFirst.IsDead())
  90.         {
  91.             name = _playerSecond.Name;
  92.             return true;
  93.         }
  94.  
  95.         return false;
  96.     }
  97.  
  98.     private void CreateGladiators()
  99.     {
  100.         int health;
  101.         int damage;
  102.         int archetypeNumber;
  103.         Gladiator gladiator = null;
  104.         _gladiators = new Gladiator[_gladiatorsAmount];
  105.  
  106.         for (int i = 0; i < _gladiatorsAmount; ++i)
  107.         {
  108.             health = _random.Next(_healthMinimum, _healthMaximum);
  109.             damage = _random.Next(_damageMinimum, _damageMaximum);
  110.             archetypeNumber = _random.Next(_archetypeMinimum, _archetypeMaximum);
  111.            
  112.             switch(archetypeNumber)
  113.             {
  114.                 case 1:
  115.                     gladiator = new Fighter(health, damage);
  116.                     break;
  117.                 case 2:
  118.                     gladiator = new Mage(health, damage);
  119.                     break;
  120.                 case 3:
  121.                     gladiator = new Archer(health, damage);
  122.                     break;
  123.             }
  124.  
  125.             _gladiators[i] = gladiator;
  126.         }
  127.     }
  128.  
  129.     private Gladiator ChooseGladiator()
  130.     {
  131.         Gladiator player = null;
  132.         string playerName;
  133.  
  134.         CreateGladiators();
  135.  
  136.         Console.Clear();
  137.         Console.WriteLine("Input name of player : ");
  138.         playerName = Console.ReadLine();
  139.  
  140.         int chooseNumber;
  141.         bool isParsed;
  142.         bool isInput = true;
  143.  
  144.         do
  145.         {
  146.             for (int i = 0; i < _gladiatorsAmount; ++i)
  147.                 Console.WriteLine($"\n{i} : {_gladiators[i]}");
  148.        
  149.             Console.WriteLine("\nInput number of gladiator : ");
  150.             isParsed = int.TryParse(Console.ReadLine(), out chooseNumber);
  151.  
  152.             if (isParsed && (chooseNumber >= 0 && chooseNumber < _gladiatorsAmount))
  153.             {
  154.                 player = _gladiators[chooseNumber];
  155.                 player.SetName(playerName);
  156.                 isInput = false;
  157.             }
  158.         }
  159.         while (isInput);
  160.  
  161.         Console.WriteLine();
  162.  
  163.         return player;
  164.     }
  165.  
  166.     private void PrintPlayer(Gladiator playerFirst, Gladiator playerSecond)
  167.     {
  168.         Console.WriteLine($"Turn : {_turnAmount}");
  169.  
  170.         if (playerFirst.IsActive != true)
  171.         {
  172.             Console.WriteLine($"Now turn is : {playerFirst.Name}");
  173.             Console.WriteLine("You are stunned");
  174.         }
  175.         else
  176.         {
  177.             Console.WriteLine($"Now turn is : {playerFirst.Name}");
  178.             Console.WriteLine("You : \n" + playerFirst);
  179.             Console.WriteLine("Enemy : \n" + playerSecond);
  180.         }
  181.     }  
  182.  
  183.     private void AttackPlayer(Gladiator playerFirst, Gladiator playerSecond)
  184.     {
  185.         if (playerFirst.IsActive == true)
  186.         {
  187.             bool isAttackChoosing = true;
  188.             string input;
  189.  
  190.             do
  191.             {
  192.                 Console.WriteLine("\nChoose attack:");
  193.                 Console.WriteLine("1. Default attack.");
  194.                 if (playerFirst.IsActiveSpecialAttack == true)
  195.                     Console.WriteLine("2. Special Attack");
  196.  
  197.                 input = Console.ReadLine();
  198.  
  199.                 if (input.Equals("1"))
  200.                 {
  201.                     playerFirst.Attack(playerSecond);
  202.                     isAttackChoosing = false;
  203.                 }
  204.                 else if (input.Equals("2") && playerFirst.IsActiveSpecialAttack == true)
  205.                 {
  206.                     playerFirst.AttackSpecial(playerSecond, _turnAmount);
  207.                     isAttackChoosing = false;
  208.                 }
  209.             }
  210.             while (isAttackChoosing);
  211.         }
  212.     }
  213.  
  214.     private Gladiator CheckTurns(Gladiator player)
  215.     {
  216.         if (player.IsActive != true && _turnAmount - player.TurnStun >= _turnsTimeStun)
  217.             player.Activate();
  218.  
  219.         if (player.IsActiveSpecialAttack != true && _turnAmount - player.TurnReload >= _turnsTimeReload)
  220.             player.ReloadAttackSpecial();
  221.  
  222.         return player;
  223.     }
  224. }
  225.  
  226. public class Gladiator
  227. {
  228.     protected int Health;
  229.     protected int Damage;
  230.     protected int DamageSpecialAttack;
  231.     protected int DamageIncreaseNumber;
  232.  
  233.     public int TurnStun { get; private set; }
  234.     public int TurnReload { get; private set; }
  235.     public string Name { get; private set; }
  236.  
  237.     public bool IsActiveSpecialAttack { get; private set; }
  238.     public bool IsActive { get; private set; }
  239.  
  240.     public Gladiator(int health, int damage)
  241.     {
  242.         SetValues(health, damage);
  243.     }  
  244.  
  245.     public void Attack(Gladiator gladiator)
  246.     {
  247.         gladiator.TakeDamage(Damage);
  248.         Console.WriteLine($"You decrease : {Damage} hp enemy.");
  249.     }
  250.  
  251.     public void TakeDamage(int damage)
  252.     {
  253.         Health -= damage;
  254.     }
  255.  
  256.     public void Stun(int turn)
  257.     {
  258.         IsActive = false;
  259.         TurnStun = turn;
  260.     }
  261.  
  262.     public void Activate()
  263.     {
  264.         IsActive = true;
  265.         TurnStun = -1;
  266.     }
  267.  
  268.     public void UseAttackSpecial(int turn)
  269.     {
  270.         IsActiveSpecialAttack = false;
  271.         TurnReload = turn;
  272.     }
  273.  
  274.     public void ReloadAttackSpecial()
  275.     {
  276.         IsActiveSpecialAttack = true;
  277.         TurnReload = -1;
  278.     }
  279.  
  280.     public bool IsDead()
  281.     {
  282.         return Health <= 0;
  283.     }
  284.  
  285.     public void SetName(string name)
  286.     {
  287.         Name = name;
  288.     }
  289.  
  290.     public virtual void AttackSpecial(Gladiator gladiator, int turn)
  291.     {
  292.     }
  293.  
  294.     public override string ToString()
  295.     {
  296.         string result = "\n";
  297.         result += "Health : " + Health + " \n";
  298.         result += "Damage : " + Damage + " \n";
  299.         return result;
  300.     }
  301.  
  302.     private void SetValues(int health, int damage)
  303.     {
  304.         Health = health;
  305.         Damage = damage;
  306.         TurnStun = -1;
  307.         TurnReload = -1;
  308.         IsActiveSpecialAttack = true;
  309.         IsActive = true;
  310.     }
  311. }
  312.  
  313. public class Fighter : Gladiator
  314. {
  315.     public Fighter(int health, int damage) : base (health, damage)
  316.     {
  317.         DamageIncreaseNumber = 40;
  318.         DamageSpecialAttack += damage + DamageIncreaseNumber;
  319.     }
  320.  
  321.     public override void AttackSpecial(Gladiator gladiator, int turn)
  322.     {
  323.         gladiator.TakeDamage(DamageSpecialAttack);
  324.         gladiator.Stun(turn);
  325.         UseAttackSpecial(turn);
  326.         Console.WriteLine($"You decrease : {DamageSpecialAttack} hp enemy " +
  327.                           $"and stunned your enemy with your special attack. ");
  328.     }
  329.  
  330.     public override string ToString()
  331.     {
  332.         string result;
  333.         result = base.ToString();
  334.         result += "Archetype : Fighter \n";
  335.         return result;
  336.     }
  337. }
  338.  
  339. public class Archer : Gladiator
  340. {
  341.     public Archer(int health, int damage) : base(health, damage)
  342.     {
  343.         DamageIncreaseNumber = 100;
  344.         DamageSpecialAttack = damage + DamageIncreaseNumber;
  345.     }
  346.  
  347.     public override void AttackSpecial(Gladiator gladiator, int turn)
  348.     {
  349.         gladiator.TakeDamage(DamageSpecialAttack);
  350.         UseAttackSpecial(turn);
  351.         Console.WriteLine($"You decrease : {DamageSpecialAttack} hp enemy with your special attack.");
  352.     }
  353.  
  354.     public override string ToString()
  355.     {
  356.         string result;
  357.         result = base.ToString();
  358.         result += "Archetype : Archer \n";
  359.         return result;
  360.     }
  361. }
  362.  
  363. public class Mage : Gladiator
  364. {
  365.     public Mage(int health, int damage) : base(health, damage)
  366.     {
  367.         DamageIncreaseNumber = 20;
  368.         DamageSpecialAttack = damage + DamageIncreaseNumber;
  369.     }
  370.  
  371.     public override void AttackSpecial(Gladiator gladiator, int turn)
  372.     {
  373.         gladiator.TakeDamage(DamageSpecialAttack);
  374.         Health += DamageSpecialAttack;
  375.         UseAttackSpecial(turn);
  376.         Console.WriteLine($"You decrease : {DamageSpecialAttack} hp enemy " +
  377.                           $"and increase your health on {DamageSpecialAttack} hp " +
  378.                           $"with your special attack.");
  379.     }
  380.  
  381.     public override string ToString()
  382.     {
  383.         string result;
  384.         result = base.ToString();
  385.         result += "Archetype : Mage \n";
  386.         return result;
  387.     }
  388. }
  389.  
Add Comment
Please, Sign In to add comment