Exemple #1
0
 /**
  * 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;
 }
Exemple #2
0
 public function testIsSunk()
 {
     $ship = new Ship(4);
     $points = [new Coordinate(5, 1), new Coordinate(4, 1), new Coordinate(3, 1), new Coordinate(2, 1)];
     foreach ($points as $point) {
         $ship->addPoint($point);
     }
     $ship->receiveShot(5, 1);
     $ship->receiveShot(4, 1);
     $ship->receiveShot(3, 1);
     $ship->receiveShot(2, 1);
     $this->assertEquals($ship->countHits(), 4);
     $this->assertTrue($ship->isSunk());
     $this->assertTrue($ship->anyHits());
 }