simeon3000

11. Pokemon Trainer

Feb 16th, 2018
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class StartUp
  6. {
  7.     static List<Trainer> trainers;
  8.  
  9.     public static void Main()
  10.     {
  11.         trainers = new List<Trainer>();
  12.         AddTrainers();
  13.  
  14.         CheckPokemons();
  15.      
  16.         PrintTrainers();
  17.     }
  18.  
  19.     private static void PrintTrainers()
  20.     {
  21.         foreach (var t in trainers.OrderByDescending(t => t.Badges))
  22.         {
  23.             Console.WriteLine($"{t.Name} {t.Badges} {t.Pokemons.Count}");
  24.         }
  25.     }
  26.  
  27.     private static void CheckPokemons()
  28.     {
  29.         string element;
  30.         while ((element = Console.ReadLine()) != "End")
  31.         {
  32.             foreach (var trainer in trainers)
  33.             {
  34.                 if (trainer.Pokemons.Count(p => p.Element == element) > 0)
  35.                 {
  36.                     trainer.Badges++;
  37.                 }
  38.                 else
  39.                 {
  40.                     trainer.Pokemons.Select(p => p.Health -= 10).ToList();
  41.                     trainer.Pokemons.RemoveAll(p => p.Health <= 0);
  42.                 }
  43.             }
  44.         }
  45.     }
  46.  
  47.     private static void AddTrainers()
  48.     {
  49.         string input;
  50.         while ((input = Console.ReadLine()) != "Tournament")
  51.         {
  52.             string[] args = input.Split();
  53.             string trainerName = args[0];
  54.             string pokemonName = args[1];
  55.             string pokemonElement = args[2];
  56.             int pokemonHealth = int.Parse(args[3]);
  57.  
  58.             Pokemon pokemon = new Pokemon(pokemonName, pokemonElement, pokemonHealth);
  59.             Trainer trainer = trainers.FirstOrDefault(t => t.Name == trainerName);
  60.  
  61.             if (trainer == null)
  62.             {
  63.                 trainer = new Trainer(trainerName);
  64.                 trainers.Add(trainer);
  65.             }
  66.             trainer.Pokemons.Add(pokemon);          
  67.         }
  68.     }
  69. }
  70.  
  71. //////////////////
  72.  
  73. public class Pokemon
  74. {
  75.     public Pokemon(string name, string element, int health)
  76.     {
  77.         this.Name = name;
  78.         this.Element = element;
  79.         this.Health = health;
  80.     }
  81.  
  82.     public string Name { get; set; }
  83.     public string Element { get; set; }
  84.     public int Health { get; set; }
  85. }
  86.  
  87. //////////////////
  88.  
  89. using System.Collections.Generic;
  90. public class Trainer
  91. {
  92.     public Trainer(string name)
  93.     {
  94.         this.Name = name;        
  95.     }
  96.  
  97.     public string Name { get; set; }
  98.     public int Badges { get; set; } = 0;
  99.  
  100.     public List<Pokemon> Pokemons { get; set; } = new List<Pokemon>();
  101. }
Advertisement
Add Comment
Please, Sign In to add comment