Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Trainer {
- private $name;
- private $badges;
- private $pokemons;
- public function __construct($name)
- {
- $this->name = $name;
- $this->pokemons = [];
- $this->badges = 0;
- }
- /**
- * @return mixed
- */
- public function getName()
- {
- return $this->name;
- }
- public function addPokemon($pokemon)
- {
- return $this->pokemons[$pokemon->getName()] = $pokemon;
- }
- /**
- * @return array
- */
- public function getPokemons(): array
- {
- return $this->pokemons;
- }
- /**
- * @param int $badges
- */
- public function setBadges(int $badges)
- {
- $this->badges = $badges;
- }
- /**
- * @return int
- */
- public function getBadges(): int
- {
- return $this->badges;
- }
- public function __toString()
- {
- return $this->name . " " . $this->badges . " " . count($this->pokemons);
- }
- /**
- * @param array $pokemons
- */
- public function setPokemons(array $pokemons)
- {
- $this->pokemons = $pokemons;
- }
- public function removePokemon($pokemon) {
- $pokemons = $this->getPokemons();
- unset($pokemons[$pokemon]);
- $this->setPokemons($pokemons);
- }
- }
- class Pokemon {
- private $name;
- private $element;
- private $health;
- public function __construct($name, $element, $health)
- {
- $this->name = $name;
- $this->element = $element;
- $this->health = $health;
- }
- /**
- * @return mixed
- */
- public function getName()
- {
- return $this->name;
- }
- /**
- * @return mixed
- */
- public function getElement()
- {
- return $this->element;
- }
- /**
- * @param mixed $health
- */
- public function setHealth($health)
- {
- $this->health = $health;
- }
- /**
- * @return mixed
- */
- public function getHealth()
- {
- return $this->health;
- }
- }
- $input = trim(fgets(STDIN));
- $trainers = [];
- while ($input != "Tournament") {
- $arr = explode(" ", $input);
- $trainerName = $arr[0];
- $pokemonName = $arr[1];
- $element = $arr[2];
- $health = $arr[3];
- $pokemon = new Pokemon($pokemonName, $element, $health);
- //if the trainer doesn't exist
- if (!array_key_exists($trainerName, $trainers)) {
- $trainer = new Trainer($trainerName);
- $trainer->addPokemon($pokemon);
- $trainers[$trainerName] = $trainer;
- } else {
- $trainers[$trainerName]->addPokemon($pokemon);
- }
- $input = trim(fgets(STDIN));
- }
- $cmd = trim(fgets(STDIN));
- while ($cmd != "End") {
- foreach ($trainers as $trainer) {
- $pokemons = $trainer->getPokemons();
- $result = 'loss';
- foreach ($pokemons as $pokemon) {
- if ($pokemon->getElement() == $cmd) {
- $trainer->setBadges($trainer->getBadges() + 1);
- $result = 'win';
- break;
- }
- }
- if ($result == 'loss') {
- foreach ($pokemons as $pokemon) {
- $pokemon->setHealth($pokemon->getHealth() - 10);
- if ($pokemon->getHealth() <= 0) {
- $trainer->removePokemon($pokemon->getName());
- }
- }
- }
- }
- $cmd = trim(fgets(STDIN));
- }
- usort($trainers, function (Trainer $a, Trainer $b) {
- return $b->getBadges() <=> $a->getBadges();
- });
- foreach ($trainers as $trainer) {
- echo $trainer . "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement