Advertisement
whitestarrr

Untitled

Feb 28th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. <?php
  2.  
  3. class Trainer {
  4. private $name;
  5. private $badges;
  6. private $pokemons;
  7.  
  8. public function __construct($name)
  9. {
  10. $this->name = $name;
  11. $this->pokemons = [];
  12. $this->badges = 0;
  13. }
  14.  
  15. /**
  16. * @return mixed
  17. */
  18. public function getName()
  19. {
  20. return $this->name;
  21. }
  22.  
  23. public function addPokemon($pokemon)
  24. {
  25. return $this->pokemons[$pokemon->getName()] = $pokemon;
  26. }
  27.  
  28. /**
  29. * @return array
  30. */
  31. public function getPokemons(): array
  32. {
  33. return $this->pokemons;
  34. }
  35.  
  36. /**
  37. * @param int $badges
  38. */
  39. public function setBadges(int $badges)
  40. {
  41. $this->badges = $badges;
  42. }
  43.  
  44. /**
  45. * @return int
  46. */
  47. public function getBadges(): int
  48. {
  49. return $this->badges;
  50. }
  51.  
  52. public function __toString()
  53. {
  54. return $this->name . " " . $this->badges . " " . count($this->pokemons);
  55. }
  56.  
  57. /**
  58. * @param array $pokemons
  59. */
  60. public function setPokemons(array $pokemons)
  61. {
  62. $this->pokemons = $pokemons;
  63. }
  64.  
  65. public function removePokemon($pokemon) {
  66. $pokemons = $this->getPokemons();
  67. unset($pokemons[$pokemon]);
  68. $this->setPokemons($pokemons);
  69. }
  70. }
  71.  
  72. class Pokemon {
  73. private $name;
  74. private $element;
  75. private $health;
  76.  
  77. public function __construct($name, $element, $health)
  78. {
  79. $this->name = $name;
  80. $this->element = $element;
  81. $this->health = $health;
  82. }
  83.  
  84. /**
  85. * @return mixed
  86. */
  87. public function getName()
  88. {
  89. return $this->name;
  90. }
  91.  
  92. /**
  93. * @return mixed
  94. */
  95. public function getElement()
  96. {
  97. return $this->element;
  98. }
  99.  
  100. /**
  101. * @param mixed $health
  102. */
  103. public function setHealth($health)
  104. {
  105. $this->health = $health;
  106. }
  107.  
  108. /**
  109. * @return mixed
  110. */
  111. public function getHealth()
  112. {
  113. return $this->health;
  114. }
  115.  
  116.  
  117. }
  118.  
  119. $input = trim(fgets(STDIN));
  120. $trainers = [];
  121.  
  122. while ($input != "Tournament") {
  123. $arr = explode(" ", $input);
  124. $trainerName = $arr[0];
  125. $pokemonName = $arr[1];
  126. $element = $arr[2];
  127. $health = $arr[3];
  128. $pokemon = new Pokemon($pokemonName, $element, $health);
  129.  
  130. //if the trainer doesn't exist
  131. if (!array_key_exists($trainerName, $trainers)) {
  132. $trainer = new Trainer($trainerName);
  133. $trainer->addPokemon($pokemon);
  134. $trainers[$trainerName] = $trainer;
  135. } else {
  136. $trainers[$trainerName]->addPokemon($pokemon);
  137. }
  138. $input = trim(fgets(STDIN));
  139. }
  140.  
  141. $cmd = trim(fgets(STDIN));
  142.  
  143. while ($cmd != "End") {
  144.  
  145. foreach ($trainers as $trainer) {
  146. $pokemons = $trainer->getPokemons();
  147. $result = 'loss';
  148. foreach ($pokemons as $pokemon) {
  149. if ($pokemon->getElement() == $cmd) {
  150. $trainer->setBadges($trainer->getBadges() + 1);
  151. $result = 'win';
  152. break;
  153. }
  154. }
  155.  
  156. if ($result == 'loss') {
  157. foreach ($pokemons as $pokemon) {
  158. $pokemon->setHealth($pokemon->getHealth() - 10);
  159. if ($pokemon->getHealth() <= 0) {
  160. $trainer->removePokemon($pokemon->getName());
  161. }
  162. }
  163. }
  164. }
  165.  
  166. $cmd = trim(fgets(STDIN));
  167. }
  168.  
  169. usort($trainers, function (Trainer $a, Trainer $b) {
  170. return $b->getBadges() <=> $a->getBadges();
  171. });
  172.  
  173.  
  174. foreach ($trainers as $trainer) {
  175. echo $trainer . "\n";
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement