Aliendreamer

dragon army

May 6th, 2018
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Dragon
  6. {
  7.     public string Colour { get; }
  8.     public int Armor { get; }
  9.     public int Damage { get; }
  10.     public int Health { get; }
  11.     public string Name { get; }
  12.  
  13.     public Dragon(string colour, string name, int damage, int health, int armor)
  14.     {
  15.         this.Colour = colour;
  16.         this.Name = name;
  17.         this.Damage = damage;
  18.         this.Health = health;
  19.         this.Armor = armor;
  20.     }
  21.     public override string ToString()
  22.     {
  23.         string result = $"-{this.Name} -> damage: {this.Damage}, health: {this.Health}, armor: {this.Armor}";
  24.         return result;
  25.     }
  26. }
  27.  
  28. public class StartUp
  29. {
  30.     public const int DefaultHealth = 250;
  31.     public const int DefaultDamage = 45;
  32.     public const int DefaultArmor = 10;
  33.  
  34.     static void Main()
  35.     {
  36.         Dictionary<string, List<Dragon>> allDragons = new Dictionary<string, List<Dragon>>();
  37.  
  38.         int numberOfDragons = int.Parse(Console.ReadLine());
  39.  
  40.         for (int i = 0; i < numberOfDragons; i++)
  41.         {
  42.             string[] input = Console.ReadLine()?.Split().ToArray();
  43.  
  44.  
  45.             string colour = input[0];
  46.             string name = input[1];
  47.             int damage = int.TryParse(input[2], out int inputDamage) ? inputDamage : DefaultDamage;
  48.             int health = int.TryParse(input[3], out int inputHealth) ? inputHealth : DefaultHealth;
  49.             int armor = int.TryParse(input[4], out int inputArmor) ? inputArmor : DefaultArmor;
  50.  
  51.             Dragon dragon = new Dragon(colour, name, damage, health, armor);
  52.  
  53.             if (!allDragons.ContainsKey(colour))
  54.             {
  55.                 allDragons.Add(colour, new List<Dragon>());
  56.             }
  57.  
  58.             Dragon itExists = allDragons[colour].FirstOrDefault(d => d.Name == name && d.Colour == colour);
  59.  
  60.             if (itExists != null)
  61.             {
  62.                 int index = allDragons[colour].IndexOf(itExists);
  63.                 allDragons[colour][index] = dragon;
  64.             }
  65.             else
  66.             {
  67.                 allDragons[colour].Add(dragon);
  68.             }
  69.         }
  70.  
  71.         foreach (var dragons in allDragons)
  72.         {
  73.  
  74.             double totalDamage = dragons.Value.Average(x => x.Damage);
  75.             double totalHealth = dragons.Value.Average(x => x.Health);
  76.             double totalArmor = dragons.Value.Average(x => x.Armor);
  77.             string colourOfDragon = $"{dragons.Key}::({totalDamage:f2}/{totalHealth:f2}/{totalArmor:f2})";
  78.             Console.WriteLine(colourOfDragon);
  79.             List<Dragon> colouredDragons = dragons.Value.OrderBy(x => x.Name).ToList();
  80.  
  81.  
  82.             foreach (Dragon dragon in colouredDragons)
  83.             {
  84.                 Console.WriteLine(dragon.ToString());
  85.             }
  86.  
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment