/** * Places a ship on the current grid * @param Ship $ship Instance of the ship * @param integer $x X-Coordinate of the Bow (front) of the ship * @param integer $y Y-Coordinate of the Bow (front) of the ship * @return boolean True on successful ship placement, False on failure */ public function placeShip(Ship $ship, $x, $y) { $this->fitShip($x, $y); $ship->addPoint(new Coordinate($x, $y)); $length = $ship->getLength(); switch ($ship->getOrientation()) { case Ship::ORIENTATION_HORIZONTAL: for ($i = 1; $i < $length; $i++) { $newX = $x + $i; $newY = $y; $this->fitShip($newX, $newY); $ship->addPoint(new Coordinate($newX, $newY)); } break; case Ship::ORIENTATION_VERTICAL: for ($i = 1; $i < $length; $i++) { $newX = $x; $newY = $y + $i; $this->fitShip($newX, $newY); $ship->addPoint(new Coordinate($newX, $newY)); } break; } $this->ships[] = $ship; return true; }
public function testShipOrientation() { $ship = new Ship(4, Ship::ORIENTATION_VERTICAL); $orientation = $ship->getOrientation(); $this->assertEquals($orientation, Ship::ORIENTATION_VERTICAL); }