Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2017
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. <?php
  2. class Trainer
  3. {
  4. private $name;
  5. private $numberOfBadges;
  6. private $pokemons = [];
  7. public function __construct(string $name, int $numberOfBadges = 0)
  8. {
  9. $this->name = $name;
  10. $this->numberOfBadges = $numberOfBadges;
  11. }
  12. public function setName(string $name)
  13. {
  14. $this->name = $name;
  15. }
  16. public function setNumberOfBadges(int $numberOfBadges)
  17. {
  18. $this->numberOfBadges = $numberOfBadges;
  19. }
  20. public function getName(): string
  21. {
  22. return $this->name;
  23. }
  24. public function getNumberOfBadges(): int
  25. {
  26. return $this->numberOfBadges;
  27. }
  28. public function getPokemons(): array
  29. {
  30. return $this->pokemons;
  31. }
  32. public function Pokemons(Pokemon $pokemon)
  33. {
  34. $this->pokemons [] = $pokemon;
  35. }
  36. public function addBadge()
  37. {
  38. return $this->numberOfBadges++;
  39. }
  40. public function countOfPokemons()
  41. {
  42. return count($this->pokemons);
  43. }
  44. public function addPocemonCollection($collection)
  45. {
  46. $this->pokemons = $collection;
  47. }
  48. }
  49. class Pokemon
  50. {
  51. private $name;
  52. private $element;
  53. private $health;
  54. public function __construct(string $name, string $element, int $health)
  55. {
  56. $this->name = $name;
  57. $this->element = $element;
  58. $this->health = $health;
  59. }
  60. public function getName(): string
  61. {
  62. return $this->name;
  63. }
  64. public function setName(string $name)
  65. {
  66. $this->name = $name;
  67. }
  68. public function getElement(): string
  69. {
  70. return $this->element;
  71. }
  72. public function setElement(string $element)
  73. {
  74. $this->element = $element;
  75. }
  76. public function getHealth(): int
  77. {
  78. return $this->health;
  79. }
  80. public function setHealth(int $health)
  81. {
  82. $this->health = $health;
  83. }
  84. public function reduceHealth()
  85. {
  86. $this->health -= 10;
  87. }
  88. }
  89. $trainersAndPokemons = [];
  90. $namesOfTrainers = [];
  91. while (true) {
  92. $input = trim(fgets(STDIN));
  93. if ($input == "Tournament") {
  94. break;
  95. }
  96. $trainerInfo = explode(' ', $input);
  97. $trainerName = $trainerInfo[0];
  98. $pokemonName = $trainerInfo[1];
  99. $pokemonElement = $trainerInfo[2];
  100. $pokemonHealth = intval($trainerInfo[3]);
  101. if (!in_array($trainerName, $namesOfTrainers)) {
  102. $pokemon = new Pokemon($pokemonName, $pokemonElement, $pokemonHealth);
  103. $trainer = new Trainer($trainerName);
  104. $trainer->Pokemons($pokemon);
  105. $trainersAndPokemons[] = $trainer;
  106. $namesOfTrainers[] = $trainerName;
  107. } else {
  108. $pokemon = new Pokemon($pokemonName, $pokemonElement, $pokemonHealth);
  109. foreach ($trainersAndPokemons as $trainerAndPokemon) {
  110. if ($trainerAndPokemon->getName() == $trainerName) {
  111. $trainerAndPokemon->Pokemons($pokemon);
  112. }
  113. }
  114. }
  115. }
  116. while (true) {
  117. $input = trim(fgets(STDIN));
  118. if ($input == "End") {
  119. break;
  120. }
  121. foreach ($trainersAndPokemons as $trainerAndPokemon) {
  122. $pokemonsByTrainer = $trainerAndPokemon->getPokemons();
  123. foreach ($pokemonsByTrainer as $key => $pokemon) {
  124. if ($pokemon->getElement() == $input) {
  125. $trainerAndPokemon->addBadge();
  126. break;
  127. } else {
  128. $pokemon->reduceHealth();
  129. if ($pokemon->getHealth() <= 0) {
  130. array_splice($pokemonsByTrainer, $key, 1);
  131. }
  132. }
  133. }
  134. $trainerAndPokemon->addPocemonCollection($pokemonsByTrainer);
  135. }
  136. }
  137. usort($trainersAndPokemons, 'orderByNumberOfBadges');
  138. foreach ($trainersAndPokemons as $trainerAndPokemons) {
  139. echo $trainerAndPokemons->getName() . ' ' .
  140. $trainerAndPokemons->getNumberOfBadges() . ' ' .
  141. $trainerAndPokemons->countOfPokemons() . PHP_EOL;
  142. }
  143. function orderByNumberOfBadges($a, $b)
  144. {
  145. return $a->getNumberOfBadges() < $b->getNumberOfBadges();
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement