Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- *
- * CLASS UTILS
- *
- */
- class Utils {
- public static $toReturn;
- public static function sendTroop($source, $destinatation, $count) {
- self::$toReturn .= "MOVE $source $destinatation $count;";
- }
- public static function sendAll() {
- echo substr(self::$toReturn,0 , -1)."\n";
- }
- public static function doNothing() {
- self::$toReturn .= "WAIT;";
- }
- public static function dd($var) {
- error_log(var_export($var, true));
- }
- public static function evaluation($factories)
- {
- foreach($factories->getOwnFactories() as $Owner)
- {
- if($factories->getFactory($Owner->getNearestFactory($factories)))
- {
- self::sendTroop($Owner->getId(), $factories->getFactory($Owner->getNearestFactory($factories))->getId(), $Owner->getCyborgs());
- }
- }
- }
- }
- /**
- *
- * CLASS DE TROOP
- *
- */
- class Troop {
- public $faction;
- public $origin;
- public $destination;
- public $countCyborg;
- public $countLapBeforeArrived;
- public function getFaction()
- {
- return $this->faction;
- }
- public function getOrigin()
- {
- return $this->faction;
- }
- public function getDestination()
- {
- return $this->destination;
- }
- public function getCountCyborg()
- {
- return $this->countCyborg;
- }
- public function getCountLapBeforeArrived()
- {
- return $this->countLapBeforeArrived;
- }
- /**
- * Permet de savoir a quelle faction (1 : nous | -1 : ennemi)
- */
- public function setFaction($arg)
- {
- $this->faction = $arg;
- }
- /**
- * Permet de savoir origine factory troop
- */
- public function setOrigin($arg)
- {
- $this->origin = $arg;
- }
- /**
- * Permet de savoir destination factory troop
- */
- public function setDestination($arg)
- {
- $this->destination = $arg;
- }
- /**
- * Permet de savoir le nb de Cyborg
- */
- public function setCountCyborg($arg)
- {
- $this->countCyborg = $arg;
- }
- /*
- * Permet de definir combien de tour avant arriver
- */
- public function setCountLapBeforeArrived($arg)
- {
- $this->countLapBeforeArrived = $arg;
- }
- }
- /**
- *
- * CLASS DE COLLECTION DE FACTORY
- *
- */
- class Factories {
- public $arrayFactory = [];
- public function addFactory($factory) {
- $this->arrayFactory[$factory->getId()] = $factory;
- }
- public function getFactory($id){
- if(array_key_exists($id, $this->arrayFactory)) {
- return $this->arrayFactory[$id];
- }
- return null;
- }
- public function getOwnFactories() {
- $ownFactories = [];
- foreach($this->arrayFactory as $factory) {
- if($factory->isOwned()) {
- $ownFactories[] = $factory;
- }
- }
- return $ownFactories;
- }
- public function purgeAllTroopsFactories() {
- foreach($this->arrayFactory as $factory) {
- $factory->purgeTroops();
- }
- }
- public function countAllCyborg()
- {
- $count = 0;
- foreach($this->getOwnFactories() as $factory)
- {
- $count += $factory->getCyborgs();
- }
- return $count;
- }
- }
- /**
- *
- * CLASS DE FACTORY
- *
- */
- class Factory {
- public $id;
- public $faction;
- public $production;
- public $countCyborgInside;
- public $arrayDistance = [];
- public $arrayTroopArrived = []; // Troupe en destination de $this
- public $arrayTroopStart = []; // Trouve au départ de $this
- public function __construct($id) {
- $this->id = $id;
- }
- public function getId() {
- return $this->id;
- }
- public function addDistance($idFactoryDistante, $distance) {
- $this->arrayDistance[$idFactoryDistante] = $distance;
- }
- /**
- * Permet de définir l'appartenance de la Factory
- * 1: possédée
- * -1: ennemi
- * 0: neutre
- */
- public function setFaction($arg) {
- $this->faction = $arg;
- }
- /**
- * Permet de définir la production de la factory
- **/
- public function setProduction($arg) {
- $this->production = $arg;
- }
- /**
- * Permet de définir le nombre de cyborg présent dans la factory
- **/
- public function setCyborgInside($arg) {
- $this->countCyborgInside = $arg;
- }
- /**
- * Permet de définir si la Factory est possédée ou non
- **/
- public function isOwned() {
- if($this->faction == '1') {
- return true;
- }
- return false;
- }
- public function isEnemy() {
- if($this->faction == '-1') {
- return true;
- }
- return false;
- }
- public function isNeutral() {
- if($this->faction == '0') {
- return true;
- }
- return false;
- }
- public function purgeTroops() {
- $this->arrayTroopArrived = [];
- $this->arrayTroopStart = [];
- }
- public function getProduction() {
- return $this->production;
- }
- public function getCyborgs() {
- return $this->countCyborgInside;
- }
- /**
- * Permet de récupérer l'identifiant de la factory la plus proche de $this
- */
- public function getNearestFactory($factories) {
- Utils::dd($factories);
- foreach($this->arrayDistance as $k => $v) {
- if(!isset($nearstFactoryId)) {
- $nearstFactoryId = $k;
- }
- if($this->arrayDistance[$nearstFactoryId] < $v) {
- $nearstFactoryId = $k;
- }
- }
- return $factories->getFactory($nearstFactoryId)->getId();
- }
- public function getMostProductiveFactory($factories) {
- foreach($factories->arrayFactory as $k => $v) {
- if(!isset($cible))
- {
- $cible = $v->getId();
- }
- if($v->isEnemy() || $v->isNeutral())
- {
- if($v->getProduction() > $factories->arrayFactory[$factories->getFactory($cible)->getId()]->getProduction()
- || $this->arrayDistance[$v->getId()] < $this->arrayDistance[$factories->getFactory($cible)->getId()]
- && $v->getProduction() == $factories->arrayFactory[$factories->getFactory($cible)->getId()]->getProduction())
- {
- $cible = $v->getId();
- }
- }
- }
- return $cible;
- }
- /**
- * Permet de récupérer l'identifiant de la factory enemie la plus proche de $this
- */
- public function getNearestFactoryEnemy($factories) {
- foreach($this->arrayDistance as $k => $v) {
- if(!isset($nearstFactoryId)) {
- $nearstFactoryId = $v->getId();
- }
- if($this->arrayDistance[$nearstFactoryId] < $v && $factories->getFactory($v->getId())->isEnemy()) {
- $nearstFactoryId = $v->getId();
- }
- }
- return $factories->getFactory($nearstFactoryId);
- }
- public function countArrivedTroop()
- {
- $count = 0;
- foreach ($this->arrayTroopArrived as $troop)
- {
- $count += $troop->getCountCyborg();
- }
- return $count;
- }
- public function countStartTroop()
- {
- $count = 0;
- foreach ($this->arrayStartTroop as $troop)
- {
- $count += $troop->getCountCyborg();
- }
- return $count;
- }
- /**
- * On récupere un objet troop
- */
- public function addTroopStart($troop)
- {
- $this->arrayTroopStart[] = $troop;
- }
- /**
- *
- */
- public function addTroopArrived($troop)
- {
- $this->arrayTroopArrived[] = $troop;
- }
- }
- fscanf(STDIN, "%d",
- $factoryCount // Nombre de factories
- );
- fscanf(STDIN, "%d",
- $linkCount // Nombre total de lien entre toute les factory
- );
- $factories = new Factories();
- // Récupère toute les distances entre chaque factory
- for ($i = 0; $i < $linkCount; $i++)
- {
- fscanf(STDIN, "%d %d %d",
- $factory1,
- $factory2,
- $distance
- );
- if($factories->getFactory($factory1) === null) {
- $factoryObjectOne = new Factory($factory1);
- } else {
- $factoryObjectOne = $factories->getFactory($factory1);
- }
- if($factories->getFactory($factory2) === null) {
- $factoryObjectTwo = new Factory($factory2);
- } else {
- $factoryObjectTwo = $factories->getFactory($factory2);
- }
- $factoryObjectOne->addDistance($factory2, $distance);
- $factoryObjectTwo->addDistance($factory1, $distance);
- $factories->addFactory($factoryObjectOne);
- $factories->addFactory($factoryObjectTwo);
- }
- /**
- *
- * DEBUT D'UN NOUVEAU TOUR
- *
- */
- while (TRUE) {
- fscanf(STDIN, "%d",
- $entityCount // Nombre total d'entité sur le jeu
- );
- for ($i = 0; $i < $entityCount; $i++) {
- fscanf(STDIN, "%d %s %d %d %d %d %d",
- $entityId,
- $entityType,
- $arg1,
- $arg2,
- $arg3,
- $arg4,
- $arg5
- );
- if($entityType == 'TROOP') {
- $troop = new Troop();
- $troop->setFaction($arg1);
- $troop->setOrigin($arg2);
- $troop->setDestination($arg3);
- $troop->setCountCyborg($arg4);
- $troop->setCountLapBeforeArrived($arg5);
- $factories->getFactory($arg2)->addTroopStart($troop); // ToDo : Crééer function addTroop*
- $factories->getFactory($arg3)->addTroopArrived($troop);
- }
- if($entityType == 'FACTORY') {
- $factory = $factories->getFactory($entityId);
- $factory->setFaction($arg1);
- $factory->setCyborgInside($arg2);
- $factory->setProduction($arg3);
- }
- /**
- * // Troop -> Ensemble de cyborgs
- * $entityId; // identifiant unique d'une entité
- * $entityType; // type de l'entité (Factory, Troop)
- * $arg1; // Possession de l'entité (1: possédé, -1: ennemi, 0:neutre)
- * $arg2; // Factory -> Production de l'usine | Troop -> Id factory départ
- * $arg3; // Factory -> Nombre de cyborg dedans | Troop -> Id factory arrivé
- * $arg4; // Troop -> Nbr de cyborg
- * $arg5; // Troop -> Nbr de tour avant arrivé à $arg3
- **/
- }
- $response = Utils::evaluation($factories);
- if(isset($response))
- {
- Utils::doNothing();
- }
- Utils::sendAll();
- $factories->purgeAllTroopsFactories();
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement