Advertisement
TeT91

ДЗ Гладиаторсике бои

Jun 4th, 2024 (edited)
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace CSLight
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Arena arena = new Arena();
  11.             arena.Fight();
  12.         }
  13.     }
  14.  
  15.     class Arena
  16.     {
  17.         private List<Fighter> _fighters = new List<Fighter>();
  18.         public Arena()
  19.         {
  20.             _fighters.Add(new Warrior());
  21.             _fighters.Add(new Barbarian());
  22.             _fighters.Add(new Paladin());
  23.             _fighters.Add(new Mage());
  24.             _fighters.Add(new Scout());
  25.         }
  26.  
  27.         public void Fight()
  28.         {
  29.             List<Fighter> fighters = new List<Fighter>();
  30.             int firstFighterId = 0;
  31.             int secondFighterId = 1;
  32.  
  33.             ConsoleColor firstTeamColor = ConsoleColor.Green;
  34.             ConsoleColor secondTeamColor = ConsoleColor.Blue;
  35.  
  36.             Console.ForegroundColor = firstTeamColor;
  37.             Console.WriteLine("Выбери первого бойца");
  38.             fighters.Add(GetFighter());
  39.  
  40.             Console.ForegroundColor = secondTeamColor;
  41.             Console.WriteLine("Выбери второго бойца");
  42.             fighters.Add(GetFighter());
  43.  
  44.             Console.ResetColor();
  45.  
  46.             bool isFighting = true;
  47.  
  48.             while (isFighting)
  49.             {
  50.                 foreach (Fighter fighter in fighters)
  51.                 {
  52.                     Console.WriteLine($"У {fighter.Name} осталось {fighter.Health} здоровья");
  53.                 }
  54.  
  55.                 Console.WriteLine($"======================");
  56.  
  57.                 Console.ForegroundColor = firstTeamColor;
  58.                 fighters[firstFighterId].Attack(fighters[secondFighterId]);
  59.  
  60.                 Console.ForegroundColor = secondTeamColor;
  61.                 fighters[secondFighterId].Attack(fighters[firstFighterId]);
  62.  
  63.                 if (TryStopFight(fighters[firstFighterId], fighters[secondFighterId]))
  64.                 {
  65.                     isFighting = false;
  66.                 }
  67.  
  68.                 Console.ResetColor();
  69.                 Console.ReadKey();
  70.             }
  71.  
  72.             if (TrySelectWinner(fighters, out Fighter winner))
  73.             {
  74.                 Console.WriteLine($"Бой окончен. Победил {winner.Name}");
  75.             }
  76.             else
  77.             {
  78.                 Console.WriteLine($"Бой окончен. Ничья");
  79.             }
  80.  
  81.             Console.ReadLine();
  82.         }
  83.  
  84.         private Fighter GetFighter()
  85.         {
  86.             bool isSelecting = true;
  87.  
  88.             Fighter fighter = null;
  89.  
  90.             while (isSelecting)
  91.             {
  92.                 for (int i = 0; i < _fighters.Count; i++)
  93.                 {
  94.                     Console.WriteLine($"{i} - {_fighters[i].Name}");
  95.                 }
  96.  
  97.                 string userInput = Console.ReadLine();
  98.  
  99.                 if (int.TryParse(userInput, out int id) && id >= 0 && id < _fighters.Count)
  100.                 {
  101.                     isSelecting = false;
  102.                     fighter =  _fighters[id].Clone();
  103.                 }
  104.                 else
  105.                 {
  106.                     Console.WriteLine("Неверный ввод");
  107.                 }
  108.             }
  109.  
  110.             return fighter;
  111.         }
  112.  
  113.         private bool TryStopFight(Fighter firstFighter, Fighter secondFighter)
  114.         {
  115.             return firstFighter.Health <= 0 || secondFighter.Health <= 0;
  116.         }
  117.  
  118.         private bool TrySelectWinner(List<Fighter> fighters, out Fighter winner)
  119.         {
  120.             winner = null;
  121.  
  122.             foreach (Fighter fighter in fighters)
  123.             {
  124.                 if (fighter.Health > 0)
  125.                 {
  126.                     winner = fighter;
  127.                     return true;
  128.                 }
  129.             }
  130.  
  131.             return false;
  132.         }
  133.     }
  134.  
  135.     class Fighter
  136.     {
  137.         public Fighter()
  138.         {
  139.             Health = 100;
  140.             MaxHealth = Health;
  141.         }
  142.  
  143.         protected int MaxHealth { get; private set; }
  144.  
  145.         public string Name { get; protected set; }
  146.  
  147.         public int Health { get; protected set; }
  148.  
  149.         public int AttackPower { get; protected set; }
  150.  
  151.         public virtual Fighter Clone()
  152.         {
  153.             return new Fighter();
  154.         }
  155.  
  156.         public virtual void Attack(Fighter enemy)
  157.         {
  158.             enemy.TakeDamage(AttackPower);
  159.  
  160.             Console.WriteLine($"{Name} атакует {enemy.Name} и наносит {AttackPower} урона");
  161.         }
  162.  
  163.         public virtual void TakeDamage(int damage)
  164.         {
  165.             if (damage >= 0)
  166.             {
  167.                 Health -= damage;
  168.             }
  169.         }
  170.  
  171.         protected bool TryActivateSkill(int chance)
  172.         {
  173.             int percent = UserUtils.GenerateRandomValue(chance);
  174.  
  175.             if (percent <= chance)
  176.             {
  177.                 return true;
  178.             }
  179.             else
  180.             {
  181.                 return false;
  182.             }
  183.         }
  184.     }
  185.  
  186.     class Warrior : Fighter
  187.     {
  188.         private int _chanceToActivateSkillDoubleAttack;
  189.  
  190.         public override Fighter Clone()
  191.         {
  192.             return new Warrior();
  193.         }
  194.  
  195.         public Warrior() : base()
  196.         {
  197.             Name = "Warrior";
  198.             AttackPower = 10;
  199.             _chanceToActivateSkillDoubleAttack = 20;
  200.         }
  201.  
  202.         public override void Attack(Fighter enemy)
  203.         {
  204.             if (TryActivateSkill(_chanceToActivateSkillDoubleAttack))
  205.             {
  206.                 ActivateSkillDoubleDamage(enemy);
  207.             }
  208.             else
  209.             {
  210.                 base.Attack(enemy);
  211.             }
  212.         }
  213.  
  214.         private void ActivateSkillDoubleDamage(Fighter enemy)
  215.         {
  216.             int powerMultiplyer = 2;
  217.  
  218.             enemy.TakeDamage(AttackPower * powerMultiplyer);
  219.             Console.WriteLine($"{Name} активирует скилл и наносит двойной урон");
  220.         }
  221.     }
  222.  
  223.     class Barbarian : Fighter
  224.     {
  225.         private int _skillDoubleAttackChargesCapacity;
  226.         private int _skillDoubleAttackCharges;
  227.  
  228.         public Barbarian() : base()
  229.         {
  230.             Name = "Barbarian";
  231.             Health = 120;
  232.             AttackPower = 10;
  233.             _skillDoubleAttackChargesCapacity = 3;
  234.             _skillDoubleAttackCharges = 0;
  235.         }
  236.  
  237.         public override Fighter Clone()
  238.         {
  239.             return new Barbarian();
  240.         }
  241.  
  242.         public override void Attack(Fighter enemy)
  243.         {
  244.             if (GetSkillDoubleAttackStatus())
  245.             {
  246.                 ActivateSkillDoubleAttack(enemy);
  247.             }
  248.             else
  249.             {
  250.                 base.Attack(enemy);
  251.                 _skillDoubleAttackCharges += ChargeSkill();
  252.             }
  253.         }
  254.  
  255.         private void ActivateSkillDoubleAttack(Fighter enemy)
  256.         {
  257.             base.Attack(enemy);
  258.             base.Attack(enemy);
  259.  
  260.             _skillDoubleAttackCharges = 0;
  261.  
  262.             Console.WriteLine($"{Name} активирует скилл и производит две атаки");
  263.         }
  264.  
  265.         private bool GetSkillDoubleAttackStatus()
  266.         {
  267.             return _skillDoubleAttackCharges >= _skillDoubleAttackChargesCapacity;
  268.         }
  269.  
  270.         private int ChargeSkill()
  271.         {
  272.             int chargesCount = 1;
  273.  
  274.             Console.WriteLine($"{Name} заряжает скилл");
  275.  
  276.             return chargesCount; ;
  277.         }
  278.     }
  279.  
  280.     class Paladin : Fighter
  281.     {
  282.         private int _skillRageChargesCapacity;
  283.         private int _skillRageCharges;
  284.  
  285.         public Paladin() : base()
  286.         {
  287.             Name = "Paladin";
  288.             Health = 90;
  289.             AttackPower = 7;
  290.             _skillRageChargesCapacity = 5;
  291.             _skillRageCharges = 0;
  292.         }
  293.  
  294.         public override Fighter Clone()
  295.         {
  296.             return new Paladin();
  297.         }
  298.  
  299.         public override void TakeDamage(int damage)
  300.         {
  301.             base.TakeDamage(damage);
  302.  
  303.             _skillRageCharges += ChargeSkill();
  304.  
  305.             if (GetSkillRageStatus())
  306.             {
  307.                 ActivateSkillRage(this);
  308.             }
  309.         }
  310.  
  311.         private void ActivateSkillRage(Fighter enemy)
  312.         {
  313.             int maxHealPower = 30;
  314.             int healPower = UserUtils.GenerateRandomValue(maxHealPower);
  315.  
  316.             Health += healPower;
  317.  
  318.             if (Health > MaxHealth)
  319.             {
  320.                 Health = MaxHealth;
  321.             }
  322.  
  323.             Console.WriteLine($"{Name} активирует скилл и лечит себя на {healPower}");
  324.         }
  325.  
  326.         private bool GetSkillRageStatus()
  327.         {
  328.             return _skillRageCharges >= _skillRageChargesCapacity;
  329.         }
  330.  
  331.         protected int ChargeSkill()
  332.         {
  333.             int chargesCount = 1;
  334.  
  335.             Console.WriteLine($"{Name} заряжает скилл");
  336.  
  337.             return chargesCount; ;
  338.         }
  339.  
  340.     }
  341.  
  342.     class Mage : Fighter
  343.     {
  344.         private int _mana;
  345.         private int _fireballManaCost;
  346.  
  347.         public Mage()
  348.         {
  349.             Name = "Mage";
  350.             Health = 70;
  351.             AttackPower = 3;
  352.             _mana = 60;
  353.             _fireballManaCost = 20;
  354.         }
  355.  
  356.         public override Fighter Clone()
  357.         {
  358.             return new Mage();
  359.         }
  360.  
  361.         public override void Attack(Fighter enemy)
  362.         {
  363.             if (_mana >= _fireballManaCost)
  364.             {
  365.                 ActivateSkillFireball(enemy);
  366.             }
  367.             else
  368.             {
  369.                 enemy.TakeDamage(AttackPower);
  370.             }
  371.         }
  372.  
  373.         private void ActivateSkillFireball(Fighter enemy)
  374.         {
  375.             int fireballPower = 15;
  376.  
  377.             enemy.TakeDamage(fireballPower);
  378.  
  379.             Console.WriteLine($"{Name} запускает фаерболл и наносит {fireballPower} урона");
  380.         }
  381.     }
  382.  
  383.     class Scout : Fighter
  384.     {
  385.         private int _chanceToActivateSkillDodge;
  386.  
  387.         public Scout()
  388.         {
  389.             Name = "Scout";
  390.             Health = 85;
  391.             _chanceToActivateSkillDodge = 40;
  392.             AttackPower = 4;
  393.         }
  394.  
  395.         public override Fighter Clone()
  396.         {
  397.             return new Scout();
  398.         }
  399.  
  400.         public override void TakeDamage(int damage)
  401.         {
  402.             if (TryActivateSkill(_chanceToActivateSkillDodge) == false)
  403.             {
  404.                 base.TakeDamage(damage);
  405.             }
  406.         }
  407.     }
  408.  
  409.     static class UserUtils
  410.     {
  411.         public static int GenerateRandomValue(int maxValue)
  412.         {
  413.             Random random = new Random();
  414.             return random.Next(maxValue);
  415.         }
  416.     }
  417. }
  418.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement