Advertisement
dragonbs

Heroes of Code and Logic VII

Mar 27th, 2023
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Hero
  5. {
  6.     public string Name { get; set; }
  7.     public int HP { get; set; }
  8.     public int MP { get; set; }
  9.  
  10.     public Hero(string name, int hp, int mp)
  11.     {
  12.         this.Name = name;
  13.         this.HP = hp;
  14.         this.MP = mp;
  15.     }
  16. }
  17.  
  18. class Program
  19. {
  20.     static List<Hero> heroes = new List<Hero>();
  21.  
  22.     static void Main()
  23.     {
  24.         GetHeroesInfo();
  25.         string input;
  26.         while ((input = Console.ReadLine()) != "End")
  27.         {
  28.             string[] cmd = input.Split(" - ");
  29.             Hero hero = heroes.Find(x => x.Name == cmd[1]);
  30.             switch (cmd[0])
  31.             {
  32.                 case "CastSpell": CastSpell(cmd, hero); break;
  33.                 case "TakeDamage": TakeDamage(cmd, hero); break;
  34.                 case "Recharge": Recharge(cmd, hero); break;
  35.                 case "Heal": Heal(cmd, hero); break;
  36.             }
  37.         }
  38.         foreach (Hero hero in heroes)
  39.         {
  40.             Console.WriteLine(hero.Name);
  41.             Console.WriteLine($"  HP: {hero.HP}");
  42.             Console.WriteLine($"  MP: {hero.MP}");
  43.         }
  44.     }
  45.  
  46.  
  47.     private static void GetHeroesInfo()
  48.     {
  49.         int numberOfHeroes = int.Parse(Console.ReadLine());
  50.         for (int i = 0; i < numberOfHeroes; i++)
  51.         {
  52.             string[] entryInfo = Console.ReadLine().Split();
  53.             string name = entryInfo[0];
  54.             int hp = int.Parse(entryInfo[1]);
  55.             int mp = int.Parse(entryInfo[2]);
  56.             heroes.Add(new Hero(name, hp, mp));
  57.         }
  58.     }
  59.  
  60.     private static void TakeDamage(string[] cmd, Hero hero)
  61.     {
  62.         int damage = int.Parse(cmd[2]);
  63.         string attacker = cmd[3];
  64.         hero.HP -= damage;
  65.         if (hero.HP > 0)
  66.             Console.WriteLine($"{hero.Name} was hit for {damage} HP by {attacker} and now has {hero.HP} HP left!");
  67.         else
  68.         {
  69.             Console.WriteLine($"{hero.Name} has been killed by {attacker}!");
  70.             heroes.Remove(hero);
  71.         }
  72.     }
  73.  
  74.  
  75.     private static void CastSpell(string[] cmd, Hero hero)
  76.     {
  77.         int mpNeeded = int.Parse(cmd[2]);
  78.         string spellName = cmd[3];
  79.         if (hero.MP >= mpNeeded)
  80.         {
  81.             hero.MP -= mpNeeded;
  82.             Console.WriteLine($"{hero.Name} has successfully cast {spellName} and now has {hero.MP} MP!");
  83.         }
  84.         else
  85.             Console.WriteLine($"{hero.Name} does not have enough MP to cast {spellName}!");
  86.     }
  87.  
  88.     private static void Recharge(string[] cmd, Hero hero)
  89.     {
  90.         int amount = int.Parse(cmd[2]);
  91.         int rechargedFor = Math.Min(amount, 200 - hero.MP);
  92.         hero.MP += rechargedFor;
  93.         Console.WriteLine($"{hero.Name} recharged for {rechargedFor} MP!");
  94.     }
  95.  
  96.     private static void Heal(string[] cmd, Hero hero)
  97.     {
  98.         int amount = int.Parse(cmd[2]);
  99.         int healedFor = Math.Min(amount, 100 - hero.HP);
  100.         hero.HP += healedFor;
  101.         Console.WriteLine($"{hero.Name} healed for {healedFor} HP!");
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement