Пример #1
0
 public function submitAction($input)
 {
     try {
         $coordinate = $this->inputHandler->handle($input);
     } catch (NoInputException $e) {
         return $this->render($this->formatter->format());
     } catch (DebugException $e) {
         return $this->render($this->formatter->format(true));
     } catch (ValidationException $e) {
         return $this->render($this->formatter->format(), ConsoleOutputManager::ERROR_MESSAGE);
     }
     $hasHit = $this->shootManager->shoot($coordinate);
     if (!$hasHit) {
         return $this->render($this->formatter->format(), ConsoleOutputManager::MISS_MESSAGE);
     }
     if ($this->shootManager->isBattlefieldDestroyed()) {
         $shootNumber = $this->shootManager->getShotsCount();
         return $this->render($this->formatter->format(), ConsoleOutputManager::SUNK_MESSAGE, $shootNumber);
     }
     $ship = $this->battlefield->getFleet()->getShip($coordinate);
     if ($this->shootManager->isShipSunk($ship)) {
         return $this->render($this->formatter->format(), ConsoleOutputManager::SUNK_MESSAGE);
     }
     return $this->render($this->formatter->format(), ConsoleOutputManager::HIT_MESSAGE);
 }
Пример #2
0
 public function indexAction(Request $request)
 {
     /** @var Session $session */
     $session = $request->getSession();
     $this->battlefieldCreator->createFromSession($session);
     $input = $request->get('coord');
     try {
         $coordinate = $this->inputHandler->handle($input);
     } catch (NoInputException $e) {
         return $this->render('View/index.html', array('battlefield' => $this->formatter->format()));
     } catch (DebugException $e) {
         return $this->render('View/index.html', array('battlefield' => $this->formatter->format(true)));
     } catch (ValidationException $e) {
         $session->getFlashBag()->add('notice', ConsoleOutputManager::ERROR_MESSAGE);
         return $this->render('View/index.html', array('battlefield' => $this->formatter->format()));
     }
     $hasHit = $this->shootManager->shoot($coordinate);
     if (!$hasHit) {
         $session->getFlashBag()->add('notice', ConsoleOutputManager::MISS_MESSAGE);
         return $this->render('View/index.html', array('battlefield' => $this->formatter->format()));
     }
     if ($this->shootManager->isBattlefieldDestroyed()) {
         $session->set('gameFinished', true);
         $shotsNumber = $this->shootManager->getShotsCount();
         return $this->render('View/finish.html', array('shots' => $shotsNumber));
     }
     $ship = $this->battlefield->getFleet()->getShip($coordinate);
     if ($this->shootManager->isShipSunk($ship)) {
         $session->getFlashBag()->add('notice', ConsoleOutputManager::SUNK_MESSAGE);
         return $this->render('View/index.html', array('battlefield' => $this->formatter->format()));
     }
     $session->getFlashBag()->add('notice', ConsoleOutputManager::HIT_MESSAGE);
     return $this->render('View/index.html', array('battlefield' => $this->formatter->format()));
 }