warXx_

Problem 8. Pokemon Trainer FIXED

Jun 28th, 2016
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Trainer
  6. {
  7.     public string name;
  8.     public int badges;
  9.     public List<Pokemon> pokemonList;
  10.  
  11.     public Trainer(string name, string PokemonName, string PokemonElement, int PokemonHealth)
  12.     {
  13.         this.name = name;
  14.         this.badges = 0;
  15.         pokemonList = new List<Pokemon>();
  16.         AddPokemon(PokemonName, PokemonElement, PokemonHealth);
  17.     }
  18.     public void AddPokemon(string name, string element, int health)
  19.     {
  20.         Pokemon pokemon = new Pokemon(name, element, health);
  21.         pokemonList.Add(pokemon);
  22.     }
  23. }
  24.  
  25. class Pokemon
  26. {
  27.     public string name;
  28.     public string element;
  29.     public int health;
  30.  
  31.     public Pokemon(string name, string element, int health)
  32.     {
  33.         this.name = name;
  34.         this.element = element;
  35.         this.health = health;
  36.     }
  37. }
  38.  
  39. class Program
  40. {
  41.     static List<Trainer> trainers;
  42.  
  43.     static void Main(string[] args)
  44.     {
  45.         int count = 0;
  46.         int pokemonsCount = 0;
  47.  
  48.         trainers = new List<Trainer>();
  49.         string input = Console.ReadLine();
  50.         while (input != "Tournament")
  51.         {
  52.             string[] trainerInfo = input.Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  53.  
  54.             Trainer trainer;
  55.             if ((trainer = ContainTrainer(trainerInfo[0])) == null)
  56.             {
  57.                 trainer = new Trainer(trainerInfo[0], trainerInfo[1], trainerInfo[2], int.Parse(trainerInfo[3]));
  58.                 trainers.Add(trainer);
  59.             }
  60.             else
  61.                 trainer.AddPokemon(trainerInfo[1], trainerInfo[2], int.Parse(trainerInfo[3]));
  62.             input = Console.ReadLine();
  63.         }
  64.         string elementInput = Console.ReadLine().Trim();
  65.         while (elementInput != "End")
  66.         {
  67.             foreach (var currentTrainer in trainers)
  68.             {
  69.                 foreach (var pokemon in currentTrainer.pokemonList)
  70.                 {
  71.                     if(pokemon.element == elementInput && pokemon.health>0)
  72.                     {
  73.                         count++;
  74.                     }
  75.                 }
  76.                 if (count > 0)
  77.                     currentTrainer.badges++;
  78.                 else
  79.                 {
  80.                     foreach (var pokemon in currentTrainer.pokemonList)
  81.                     {
  82.                         pokemon.health -= 10;
  83.                     }
  84.                 }
  85.                 count = 0;
  86.             }
  87.  
  88.             elementInput = Console.ReadLine().Trim();
  89.         }
  90.  
  91.         foreach (var currentTrainer in trainers.OrderByDescending(x=>x.badges))
  92.         {
  93.             foreach (var pokemon in currentTrainer.pokemonList)
  94.             {
  95.                 if (pokemon.health > 0)
  96.                 {
  97.                     pokemonsCount++;
  98.                 }
  99.             }
  100.             Console.WriteLine($"{currentTrainer.name} {currentTrainer.badges} {pokemonsCount}");
  101.             pokemonsCount = 0;
  102.         }
  103.     }
  104.  
  105.    
  106.     static Trainer ContainTrainer(string name)
  107.     {
  108.         foreach (var item in trainers)
  109.         {
  110.             if (item.name == name)
  111.             {
  112.                 return item;
  113.             }
  114.         }
  115.         return null;
  116.     }
  117. }
Add Comment
Please, Sign In to add comment