Advertisement
Danny_Berova

11.PokemonTrainer

Feb 18th, 2018
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Program
  6. {
  7.     public static void Main()
  8.     {
  9.         var trainers = new Dictionary<string, Trainer>();
  10.         var input = Console.ReadLine();
  11.  
  12.         while (input != "Tournament")
  13.         {
  14.             var inputTokens = input.Split();
  15.             if (inputTokens.Length == 4)
  16.             {
  17.                 var trainerName = inputTokens[0];
  18.                 var pokemonName = inputTokens[1];
  19.                 var pokemonElement = inputTokens[2];
  20.                 var pokemonHealth = int.Parse(inputTokens[3]);
  21.  
  22.                 var currentTrainer = new Trainer(trainerName);
  23.                 var currentPokemon = new Pokemon(pokemonName, pokemonElement, pokemonHealth);
  24.  
  25.                 if (!trainers.ContainsKey(trainerName))
  26.                 {
  27.                     trainers[trainerName] = currentTrainer;
  28.                 }
  29.                 trainers[trainerName].Pokemons.Add(currentPokemon);
  30.             }
  31.  
  32.             input = Console.ReadLine();
  33.         }
  34.  
  35.         while ((input = Console.ReadLine()) != "End")
  36.         {
  37.  
  38.             foreach (var trainer in trainers)
  39.             {
  40.                 var trainerToCheck = trainer.Value;
  41.                 if (trainerToCheck.Pokemons.Any(p => p.Element == input))
  42.                 {
  43.                     trainerToCheck.Badges++;
  44.                 }
  45.                 else
  46.                 {
  47.                     for (int i = 0; i < trainerToCheck.Pokemons.Count; i++)
  48.                     {
  49.                         var pokemonToCheck = trainerToCheck.Pokemons[i];
  50.                         if (pokemonToCheck.Health > 10)
  51.                         {
  52.                             pokemonToCheck.Health -= 10;
  53.                         }
  54.                         else
  55.                         {
  56.                             trainerToCheck.Pokemons.Remove(pokemonToCheck);
  57.                             i--;
  58.                         }
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.  
  64.         var sorted = trainers.OrderByDescending(t => t.Value.Badges);
  65.  
  66.         foreach (var tr in sorted)
  67.         {
  68.             var trn = tr.Value;
  69.             Console.WriteLine($"{trn.Name} {trn.Badges} {trn.Pokemons.Count}");
  70.         }
  71.     }
  72.  
  73. public class Pokemon
  74. {
  75.     private string name;
  76.     private string element;
  77.     private int health;
  78.  
  79.     public Pokemon(string name, string element, int health)
  80.     {
  81.         this.name = name;
  82.         this.element = element;
  83.         this.health = health;
  84.     }
  85.  
  86.     public string Name
  87.     {
  88.         get { return this.name; }
  89.         set { this.name = value; }
  90.     }
  91.  
  92.     public string Element
  93.     {
  94.         get { return this.element; }
  95.         set { this.element = value; }
  96.     }
  97.  
  98.     public int Health
  99.     {
  100.         get { return this.health; }
  101.         set { this.health = value; }
  102.     }
  103. }
  104.  
  105. using System.Collections.Generic;
  106.  
  107. public class Trainer
  108. {
  109.     private string name;
  110.     private int badges;
  111.     private List<Pokemon> pokemons;
  112.  
  113.     public Trainer(string name)
  114.     {
  115.         this.name = name;
  116.         this.Badges = 0;
  117.         this.Pokemons = new List<Pokemon>();
  118.     }
  119.  
  120.     public string Name
  121.     {
  122.         get { return this.name; }
  123.         set { this.name = value; }
  124.     }
  125.  
  126.     public int Badges
  127.     {
  128.         get { return this.badges; }
  129.         set { this.badges = value; }
  130.     }
  131.  
  132.     public List<Pokemon> Pokemons
  133.     {
  134.         get { return this.pokemons; }
  135.         set { this.pokemons = value; }
  136.     }
  137. }
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement