Advertisement
yanchevilian

09. Pokemon Trainer

Sep 2nd, 2021
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace DefiningClasses
  6. {
  7.     public class StartUp
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<Trainer> allTrainers = new List<Trainer>();
  12.             string inputLines = Console.ReadLine();
  13.  
  14.             while (inputLines?.ToLower() != "tournament")
  15.             {
  16.                 string[] inputArray = inputLines.Split(" ", StringSplitOptions.RemoveEmptyEntries);
  17.                 string trainerName = inputArray[0];
  18.                 string pokemonName = inputArray[1];
  19.                 string pokemonElement = inputArray[2];
  20.                 int pokemonHealth = int.Parse(inputArray[3]);
  21.  
  22.                 if (allTrainers.Any(x => x.Name == trainerName) == false)
  23.                 {
  24.                     allTrainers.Add(new Trainer(trainerName));
  25.                 }
  26.  
  27.                 Trainer currentTrainer = allTrainers.Find(t => t.Name == trainerName);
  28.                 currentTrainer.Pokemons.Add(new Pokemon(pokemonName, pokemonElement, pokemonHealth));
  29.  
  30.                 inputLines = Console.ReadLine();
  31.             }
  32.  
  33.             string command = Console.ReadLine();
  34.             while (command?.ToLower() != "end")
  35.             {
  36.                 foreach (Trainer trainer in allTrainers)
  37.                 {
  38.                     if (trainer.Pokemons.Any(p => p.Element == command))
  39.                     {
  40.                         trainer.NumberOfBadges += 1;
  41.                     }
  42.                     else
  43.                     {
  44.                         for (int i = 0; i < trainer.Pokemons.Count; i++)
  45.                         {
  46.                             trainer.Pokemons[i].Health -= 10;
  47.                             if (trainer.Pokemons[i].Health <= 0)
  48.                             {
  49.                                 trainer.Pokemons.RemoveAt(i);
  50.                                 i--;
  51.                             }
  52.                         }
  53.                     }
  54.                 }
  55.  
  56.                 command = Console.ReadLine();
  57.             }
  58.  
  59.             foreach (Trainer trainer in allTrainers.OrderByDescending(t => t.NumberOfBadges))
  60.             {
  61.                 Console.WriteLine($"{trainer.Name} {trainer.NumberOfBadges} {trainer.Pokemons.Count}");
  62.             }
  63.         }
  64.     }
  65.  
  66.     public class Pokemon
  67.     {
  68.         public Pokemon(string name, string element, int health)
  69.         {
  70.             this.Name = name;
  71.             this.Element = element;
  72.             this.Health = health;
  73.         }
  74.  
  75.         public string Name { get; set; }
  76.         public string Element { get; set; }
  77.         public int Health { get; set; }
  78.     }
  79.  
  80.     public class Trainer
  81.     {
  82.         public Trainer(string name)
  83.         {
  84.             this.Name = name;
  85.             this.Pokemons = new List<Pokemon>();
  86.             NumberOfBadges = 0;
  87.         }
  88.  
  89.         public string Name { get; set; }
  90.         public int NumberOfBadges { get; set; }
  91.         public List<Pokemon> Pokemons { get; set; }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement