Пример #1
0
 /**
  * Placement assumes x,y represent stern (back of ship)
  * @todo: Convert to use strategy pattern, possibly placement from bow or other?
  */
 public function placeShip(Ship $ship, $x, $y)
 {
     $this->fitShip($x, $y);
     $point = new Point($x, $y);
     $ship->addCoordinate($point);
     // Get coords for ship given x,y
     switch ($ship->getOrientation()) {
         case Ship::ORIENTATION_VERTICAL:
             for ($i = 1; $i < $ship->getLength(); $i++) {
                 $newX = $x;
                 $newY = $y + $i;
                 $this->fitShip($newX, $newY);
                 $point = new Point($newX, $newY);
                 $ship->addCoordinate($point);
             }
             break;
         case Ship::ORIENTATION_HORIZTONAL:
             for ($i = 1; $i < $ship->getLength(); $i++) {
                 $newX = $x + $i;
                 $newY = $y;
                 $this->fitShip($newX, $newY);
                 $point = new Point($newX, $newY);
                 $ship->addCoordinate($point);
             }
             break;
     }
     // If ship makes it this far placement is valid.
     $this->addShip($ship);
     return true;
 }
Пример #2
0
 public function testShipNotHalfHitsSunk()
 {
     $ship = new Ship(2);
     // No Hits
     $this->assertFalse($ship->isSunk());
 }