private static function validEndpoint(Fields $fields, Coords $spot, $shipSize, $direction)
 {
     /** @var Field $field */
     $field = $fields->at($spot);
     if ($field->occupied()) {
         return;
     }
     if (!$fields->hasAt($spot->{$direction}($shipSize - 1))) {
         return;
     }
     $neighbour = null;
     for ($i = 1; $i < $shipSize; $i++) {
         $neighbour = $fields->at($spot->{$direction}($i));
         if ($neighbour->occupied()) {
             return;
         }
     }
     if ($neighbour === null) {
         return;
     }
     return $neighbour->coords();
 }
Exemple #2
0
 /**
  * @return Ship[]
  */
 public function ships()
 {
     return $this->fields->ships();
 }