Example #1
0
 public function homeAction()
 {
     $info = null;
     $battlefield = $this->getDataSaver()->load('battlefield');
     /* @var $battlefield \Model\Battlefield\Battlefield */
     if (!$battlefield) {
         $battlefieldFactory = new \Model\Battlefield\BattlefieldFactory();
         $battlefield = $battlefieldFactory->createDefaultBattlefield();
     }
     $battlefield->setEventDispacher($this->getEventDispacher());
     if (!empty($_POST)) {
         $shotData = $_POST['shot'];
         try {
             $pointFactory = new \Model\Battlefield\Point\PointFactory();
             $point = $pointFactory->createPoint($shotData);
             $battlefield->shoot($point);
             $this->getDataSaver()->save($battlefield, 'battlefield');
             if (!$battlefield->isThereNonSunkBattleship()) {
                 $this->setTemplate('Web' . DIRECTORY_SEPARATOR . 'endGame');
                 return $this->endGameAction();
             }
         } catch (\Model\Battlefield\Exception\HumanReadableException $e) {
             $info = $e->getMessage();
         } catch (\Exception\Exception $e) {
             $info = 'error';
         }
     }
     $visualizer = $this->getVisualizerFactory()->create($battlefield);
     return ['visualizer' => $visualizer, 'info' => $info];
 }
Example #2
0
 /**
  * Force user to enter valid point
  * @param Battlefield $battlefield
  * @return \Model\Battlefield\Point\PointInterface
  */
 protected function shoot(Battlefield $battlefield)
 {
     $pointFactory = new \Model\Battlefield\Point\PointFactory();
     $shot = null;
     while ($shot === null) {
         echo "Enter point coordinates: ";
         $coordinates = trim(fgets(STDIN));
         try {
             $shot = $pointFactory->createPoint($coordinates);
             $battlefield->shoot($shot);
         } catch (\Model\Battlefield\Exception\HumanReadableException $e) {
             echo "Error. " . $e->getMessage() . PHP_EOL;
             $shot = null;
         }
     }
     return $shot;
 }