Advertisement
iskren_penev

PokemonTrainer

Jun 21st, 2016
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _08.PokemonTrainer
  6. {
  7.     public class PokemonTrainer
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             List<Trainer> pokemonTrainers = new List<Trainer>();
  12.             string inputLine = Console.ReadLine();
  13.             while (inputLine != "Tournament")
  14.             {
  15.                 string[] tokens = inputLine
  16.                     .Split(new char[] { ' ', '\t', '\n' }, StringSplitOptions.RemoveEmptyEntries);
  17.                 string trainerName = tokens[0];
  18.                 string pokemonName = tokens[1];
  19.                 string pokemonType = tokens[2];
  20.                 int pokemonHealth = int.Parse(tokens[3]);
  21.                 Trainer currentTrainer = new Trainer(trainerName);
  22.                 Pokemon currentPokemon = new Pokemon(pokemonName, pokemonType, pokemonHealth);
  23.                 currentTrainer.Pokemons.Add(currentPokemon);
  24.                 bool wasAdded = false;
  25.                 foreach (var trainer in pokemonTrainers)
  26.                 {
  27.                     if (trainer.Name == trainerName)
  28.                     {
  29.                         trainer.Pokemons.Add(currentPokemon);
  30.                         wasAdded = true;
  31.                         break;
  32.                     }
  33.                 }
  34.                 if (!wasAdded)
  35.                     pokemonTrainers.Add(currentTrainer);
  36.                 inputLine = Console.ReadLine();
  37.             }
  38.             inputLine = Console.ReadLine();
  39.             while (inputLine != "End")
  40.             {
  41.                 string type = inputLine;
  42.                 for (int i = 0; i < pokemonTrainers.Count; i++)
  43.                 {
  44.                     Trainer currentTrainer = pokemonTrainers[i];
  45.                     if (currentTrainer.ContainsType(type))
  46.                         currentTrainer.NumberOfBadges++;
  47.                     else
  48.                     {
  49.                         currentTrainer.DecreaseHealth();
  50.                         currentTrainer.RemoveDeadPokemons();
  51.                     }
  52.                 }
  53.                 inputLine = Console.ReadLine();
  54.             }
  55.             pokemonTrainers = pokemonTrainers.OrderByDescending(trainer => trainer.NumberOfBadges).ToList();
  56.             foreach (var trainer in pokemonTrainers)
  57.             {
  58.                 trainer.PrintTrainerInfo();
  59.             }
  60.         }
  61.     }
  62.  
  63.     public class Pokemon
  64.     {
  65.         public string Name;
  66.         public string Type;
  67.         public int Health;
  68.  
  69.         public Pokemon(string name, string type, int health)
  70.         {
  71.             Name = name;
  72.             Type = type;
  73.             Health = health;
  74.         }
  75.     }
  76.  
  77.     public class Trainer
  78.     {
  79.         public string Name;
  80.         public int NumberOfBadges;
  81.         public List<Pokemon> Pokemons;
  82.  
  83.         public Trainer(string name)
  84.         {
  85.             Name = name;
  86.             NumberOfBadges = 0;
  87.             Pokemons = new List<Pokemon>();
  88.         }
  89.  
  90.         public bool ContainsType(string type)
  91.         {
  92.             foreach (var pokemon in Pokemons)
  93.             {
  94.                 if (pokemon.Type == type)
  95.                 {
  96.                     return true;
  97.                 }
  98.             }
  99.             return false;
  100.         }
  101.  
  102.         public void DecreaseHealth()
  103.         {
  104.             for (int i = 0; i < Pokemons.Count; i++)
  105.             {
  106.                 Pokemons[i].Health -= 10;
  107.             }
  108.         }
  109.  
  110.         public void RemoveDeadPokemons()
  111.         {
  112.             Pokemons = Pokemons.Where(pokemon => pokemon.Health > 0).ToList();
  113.         }
  114.  
  115.         public void PrintTrainerInfo()
  116.         {
  117.             Console.WriteLine("{0} {1} {2}", Name, NumberOfBadges, Pokemons.Count);
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement