Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.12 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  *
  5.  * CLASS UTILS
  6.  *
  7.  */
  8. class Utils {
  9.     public static $toReturn;
  10.    
  11.     public static function sendTroop($source, $destinatation, $count) {
  12.         self::$toReturn .= "MOVE $source $destinatation $count;";
  13.     }
  14.    
  15.     public static function sendAll() {
  16.         echo substr(self::$toReturn,0 , -1)."\n";
  17.     }
  18.    
  19.     public static function doNothing() {
  20.         self::$toReturn .= "WAIT;";
  21.     }
  22.    
  23.     public static function dd($var) {
  24.         error_log(var_export($var, true));
  25.     }
  26.    
  27.     public static function evaluation($factories)
  28.     {
  29.         foreach($factories->getOwnFactories() as $Owner)
  30.         {
  31.             if($factories->getFactory($Owner->getNearestFactory($factories)))
  32.             {
  33.                 self::sendTroop($Owner->getId(), $factories->getFactory($Owner->getNearestFactory($factories))->getId(), $Owner->getCyborgs());
  34.             }
  35.         }
  36.     }
  37. }
  38.  
  39. /**
  40.  *
  41.  * CLASS DE TROOP
  42.  *
  43.  */
  44. class Troop {
  45.    
  46.     public $faction;
  47.    
  48.     public $origin;
  49.    
  50.     public $destination;
  51.    
  52.     public $countCyborg;
  53.    
  54.     public $countLapBeforeArrived;
  55.    
  56.    
  57.     public function getFaction()
  58.     {
  59.         return $this->faction;
  60.     }
  61.  
  62.     public function getOrigin()
  63.     {
  64.         return $this->faction;
  65.     }
  66.    
  67.     public function getDestination()
  68.     {
  69.         return $this->destination;
  70.     }
  71.    
  72.     public function getCountCyborg()
  73.     {
  74.         return $this->countCyborg;
  75.     }
  76.    
  77.     public function getCountLapBeforeArrived()
  78.     {
  79.         return $this->countLapBeforeArrived;
  80.     }
  81.    
  82.    
  83.     /**
  84.      * Permet de savoir a quelle faction (1 : nous | -1 : ennemi)
  85.      */
  86.     public function setFaction($arg)
  87.     {
  88.         $this->faction = $arg;
  89.     }
  90.    
  91.     /**
  92.      * Permet de savoir origine factory troop
  93.      */
  94.     public function setOrigin($arg)
  95.     {
  96.         $this->origin = $arg;
  97.     }
  98.    
  99.     /**
  100.      * Permet de savoir destination factory troop
  101.      */
  102.     public function setDestination($arg)
  103.     {
  104.         $this->destination = $arg;
  105.     }
  106.    
  107.     /**
  108.      * Permet de savoir le nb de Cyborg
  109.      */
  110.     public function setCountCyborg($arg)
  111.     {
  112.         $this->countCyborg = $arg;
  113.     }
  114.    
  115.     /*
  116.     * Permet de definir combien de tour avant arriver
  117.     */
  118.     public function setCountLapBeforeArrived($arg)
  119.     {
  120.         $this->countLapBeforeArrived = $arg;
  121.     }
  122. }
  123.  
  124.  
  125. /**
  126.  *
  127.  * CLASS DE COLLECTION DE FACTORY
  128.  *
  129.  */
  130. class Factories {
  131.    
  132.     public $arrayFactory = [];
  133.    
  134.     public function addFactory($factory) {
  135.         $this->arrayFactory[$factory->getId()] = $factory;
  136.     }
  137.    
  138.     public function getFactory($id){
  139.         if(array_key_exists($id, $this->arrayFactory)) {
  140.             return $this->arrayFactory[$id];  
  141.         }
  142.         return null;
  143.     }
  144.    
  145.     public function getOwnFactories() {
  146.         $ownFactories = [];
  147.         foreach($this->arrayFactory as $factory) {
  148.             if($factory->isOwned()) {
  149.                 $ownFactories[] = $factory;
  150.             }
  151.         }
  152.         return $ownFactories;
  153.     }
  154.    
  155.     public function purgeAllTroopsFactories() {
  156.         foreach($this->arrayFactory as $factory) {
  157.             $factory->purgeTroops();
  158.         }
  159.     }
  160.    
  161.     public function countAllCyborg()
  162.     {
  163.         $count = 0;
  164.         foreach($this->getOwnFactories() as $factory)
  165.         {
  166.             $count += $factory->getCyborgs();
  167.         }
  168.         return $count;
  169.     }
  170.    
  171. }
  172.  
  173. /**
  174.  *
  175.  * CLASS DE FACTORY
  176.  *
  177.  */
  178. class Factory {
  179.    
  180.     public $id;
  181.    
  182.     public $faction;
  183.    
  184.     public $production;
  185.    
  186.     public $countCyborgInside;
  187.    
  188.     public $arrayDistance = [];
  189.    
  190.     public $arrayTroopArrived = []; // Troupe en destination de $this
  191.    
  192.     public $arrayTroopStart = []; // Trouve au départ de $this
  193.    
  194.     public function __construct($id) {
  195.         $this->id = $id;
  196.     }
  197.    
  198.     public function getId() {
  199.         return $this->id;
  200.     }
  201.    
  202.     public function addDistance($idFactoryDistante, $distance) {
  203.         $this->arrayDistance[$idFactoryDistante] = $distance;
  204.     }
  205.    
  206.     /**
  207.      * Permet de définir l'appartenance de la Factory
  208.      * 1: possédée
  209.      * -1: ennemi
  210.      * 0: neutre
  211.      */
  212.     public function setFaction($arg) {
  213.         $this->faction = $arg;
  214.     }
  215.    
  216.     /**
  217.      * Permet de définir la production de la factory
  218.      **/
  219.     public function setProduction($arg) {
  220.         $this->production = $arg;
  221.     }
  222.    
  223.     /**
  224.      * Permet de définir le nombre de cyborg présent dans la factory
  225.      **/
  226.     public function setCyborgInside($arg) {
  227.         $this->countCyborgInside = $arg;
  228.     }
  229.    
  230.     /**
  231.      * Permet de définir si la Factory est possédée ou non
  232.      **/
  233.     public function isOwned() {
  234.         if($this->faction == '1') {
  235.             return true;
  236.         }
  237.         return false;
  238.     }
  239.    
  240.     public function isEnemy() {
  241.         if($this->faction == '-1') {
  242.             return true;
  243.         }
  244.         return false;
  245.     }
  246.    
  247.     public function isNeutral() {
  248.         if($this->faction == '0') {
  249.             return true;
  250.         }
  251.         return false;
  252.     }
  253.    
  254.     public function purgeTroops() {
  255.         $this->arrayTroopArrived = [];
  256.         $this->arrayTroopStart = [];
  257.     }
  258.    
  259.     public function getProduction() {
  260.         return $this->production;
  261.     }
  262.    
  263.     public function getCyborgs() {
  264.         return $this->countCyborgInside;
  265.     }
  266.    
  267.     /**
  268.      * Permet de récupérer l'identifiant de la factory la plus proche de $this
  269.      */
  270.     public function getNearestFactory($factories) {
  271.        
  272.         Utils::dd($factories);
  273.         foreach($this->arrayDistance as $k => $v) {
  274.            
  275.             if(!isset($nearstFactoryId)) {
  276.                 $nearstFactoryId = $k;
  277.             }
  278.             if($this->arrayDistance[$nearstFactoryId] < $v) {
  279.                 $nearstFactoryId = $k;
  280.             }
  281.         }
  282.         return $factories->getFactory($nearstFactoryId)->getId();
  283.     }
  284.    
  285.     public function getMostProductiveFactory($factories) {
  286.         foreach($factories->arrayFactory as $k => $v) {
  287.             if(!isset($cible))
  288.             {
  289.                 $cible = $v->getId();
  290.             }
  291.             if($v->isEnemy() || $v->isNeutral())
  292.             {
  293.                 if($v->getProduction() > $factories->arrayFactory[$factories->getFactory($cible)->getId()]->getProduction()
  294.                    || $this->arrayDistance[$v->getId()] < $this->arrayDistance[$factories->getFactory($cible)->getId()]
  295.                    && $v->getProduction() == $factories->arrayFactory[$factories->getFactory($cible)->getId()]->getProduction())
  296.                 {
  297.                     $cible = $v->getId();
  298.                 }
  299.             }
  300.         }
  301.         return $cible;
  302.     }
  303.    
  304.     /**
  305.      * Permet de récupérer l'identifiant de la factory enemie la plus proche de $this
  306.      */
  307.     public function getNearestFactoryEnemy($factories) {
  308.         foreach($this->arrayDistance as $k => $v) {
  309.             if(!isset($nearstFactoryId)) {
  310.                 $nearstFactoryId = $v->getId();
  311.             }
  312.             if($this->arrayDistance[$nearstFactoryId] < $v && $factories->getFactory($v->getId())->isEnemy()) {
  313.                 $nearstFactoryId = $v->getId();
  314.             }
  315.         }
  316.         return $factories->getFactory($nearstFactoryId);
  317.     }
  318.    
  319.     public function countArrivedTroop()
  320.     {
  321.         $count = 0;
  322.         foreach ($this->arrayTroopArrived as $troop)
  323.         {
  324.              $count += $troop->getCountCyborg();
  325.         }
  326.         return $count;
  327.     }
  328.    
  329.     public function countStartTroop()
  330.     {
  331.         $count = 0;
  332.         foreach ($this->arrayStartTroop as $troop)
  333.         {
  334.              $count += $troop->getCountCyborg();
  335.         }
  336.         return $count;
  337.     }
  338.    
  339.     /**
  340.      * On récupere un objet troop
  341.      */
  342.     public function addTroopStart($troop)
  343.     {
  344.          $this->arrayTroopStart[] = $troop;
  345.     }
  346.    
  347.     /**
  348.      *
  349.      */
  350.     public function addTroopArrived($troop)
  351.     {
  352.         $this->arrayTroopArrived[] = $troop;
  353.     }
  354.    
  355. }
  356.  
  357.  
  358.  
  359.  
  360. fscanf(STDIN, "%d",
  361.     $factoryCount // Nombre de factories
  362. );
  363. fscanf(STDIN, "%d",
  364.     $linkCount // Nombre total de lien entre toute les factory
  365. );
  366.  
  367. $factories = new Factories();
  368. // Récupère toute les distances entre chaque factory
  369. for ($i = 0; $i < $linkCount; $i++)
  370. {
  371.     fscanf(STDIN, "%d %d %d",
  372.         $factory1,
  373.         $factory2,
  374.         $distance
  375.     );
  376.    
  377.     if($factories->getFactory($factory1) === null) {
  378.         $factoryObjectOne = new Factory($factory1);
  379.     } else {
  380.         $factoryObjectOne = $factories->getFactory($factory1);
  381.     }
  382.    
  383.     if($factories->getFactory($factory2) === null) {
  384.         $factoryObjectTwo = new Factory($factory2);
  385.     } else {
  386.         $factoryObjectTwo = $factories->getFactory($factory2);
  387.     }
  388.    
  389.     $factoryObjectOne->addDistance($factory2, $distance);
  390.     $factoryObjectTwo->addDistance($factory1, $distance);
  391.    
  392.     $factories->addFactory($factoryObjectOne);
  393.     $factories->addFactory($factoryObjectTwo);
  394. }
  395.  
  396.  
  397. /**
  398.  *
  399.  * DEBUT D'UN NOUVEAU TOUR
  400.  *
  401.  */
  402. while (TRUE) {
  403.     fscanf(STDIN, "%d",
  404.         $entityCount // Nombre total d'entité sur le jeu
  405.     );
  406.    
  407.     for ($i = 0; $i < $entityCount; $i++) {
  408.         fscanf(STDIN, "%d %s %d %d %d %d %d",
  409.             $entityId,
  410.             $entityType,
  411.             $arg1,
  412.             $arg2,
  413.             $arg3,
  414.             $arg4,
  415.             $arg5
  416.         );
  417.         if($entityType == 'TROOP') {
  418.             $troop = new Troop();
  419.             $troop->setFaction($arg1);
  420.             $troop->setOrigin($arg2);
  421.             $troop->setDestination($arg3);
  422.             $troop->setCountCyborg($arg4);
  423.             $troop->setCountLapBeforeArrived($arg5);
  424.            
  425.            
  426.             $factories->getFactory($arg2)->addTroopStart($troop); // ToDo : Crééer function addTroop*
  427.             $factories->getFactory($arg3)->addTroopArrived($troop);
  428.         }
  429.        
  430.         if($entityType == 'FACTORY') {
  431.             $factory = $factories->getFactory($entityId);
  432.             $factory->setFaction($arg1);
  433.             $factory->setCyborgInside($arg2);
  434.             $factory->setProduction($arg3);
  435.         }
  436.        
  437.        
  438.        
  439.         /**
  440.          * // Troop -> Ensemble de cyborgs
  441.          * $entityId; // identifiant unique d'une entité
  442.          * $entityType; // type de l'entité (Factory, Troop)
  443.          * $arg1; // Possession de l'entité (1: possédé, -1: ennemi, 0:neutre)
  444.          * $arg2; // Factory -> Production de l'usine | Troop -> Id factory départ
  445.          * $arg3; // Factory -> Nombre de cyborg dedans | Troop -> Id factory arrivé
  446.          * $arg4; // Troop -> Nbr de cyborg
  447.          * $arg5; // Troop -> Nbr de tour avant arrivé à $arg3
  448.          **/
  449.        
  450.     }
  451.    
  452.     $response = Utils::evaluation($factories);
  453.    
  454.     if(isset($response))
  455.     {
  456.         Utils::doNothing();
  457.     }
  458.     Utils::sendAll();
  459.    
  460.     $factories->purgeAllTroopsFactories();
  461. }
  462. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement