Exemple #1
0
 /**
  * @param Hole $hole
  *
  * @return int
  *
  * @throws AllShipsAreNotPlacedException
  */
 public function shot(Hole $hole)
 {
     if (!$this->areAllShipsPlaced()) {
         throw new AllShipsAreNotPlacedException('All ships must be placed before shooting');
     }
     $y = Hole::letterToNumber($hole->letter()) - 1;
     $x = $hole->number() - 1;
     $shipId = $this->grid[$y][$x];
     if ($shipId !== 0) {
         $this->grid[$y][$x] = -abs($this->grid[$y][$x]);
         if ($this->isShipSunk($this->ships[abs($shipId)])) {
             return self::SUNK;
         }
         return self::HIT;
     }
     return self::WATER;
 }