/** * Places a ship on the board * @param \Model\Ship $ship * @return \Model\Ship */ protected function placeShip(Ship $ship) { $length = $ship->getLengh(); while (!$ship->isPlaced()) { //TODO: optimize $direction = rand(0, 1) ? Ship::DIRECTION_HORIZONTAL : Ship::DIRECTION_VERTICAL; $row = rand(1, self::ROWS); $col = rand(1, self::COLS); $points = $this->generateShipPoints($length, $direction, $row, $col); if (!$this->hasCollision($points)) { $ship->setPoints($points); } } return $ship; }