예제 #1
0
 public function testBattleshipAddingAfterShoot()
 {
     $this->setExpectedException(\Model\Battlefield\Exception\Exception::class);
     $battlefield = new Battlefield(5, 5);
     $battlefield->shoot(new Point(0, 0));
     $battlefield->addBattleship(new Placer(new Destroyer(), new Point(0, 0), new Point(0, 3)));
 }
예제 #2
0
 public function getLastShotStatus()
 {
     $lastShot = $this->battlefield->getShots()->getLastPoint();
     if (!$lastShot) {
         return null;
     }
     if ($this->battlefield->isPointFree($lastShot)) {
         return 'miss';
     } else {
         $sink = false;
         foreach ($this->battlefield->getPlacers() as $placer) {
             $placerPoints = $placer->getPoints();
             if ($placerPoints->hasPoint($lastShot)) {
                 $sinking = true;
                 foreach ($placerPoints as $battleshipPoint) {
                     if (Battlefield::POINT_STATUS_SHIP_HIT !== $this->battlefield->getPointStatus($battleshipPoint)) {
                         $sinking = false;
                         break;
                     }
                 }
                 if ($sinking) {
                     $sink = true;
                 }
                 break;
             }
         }
         if ($sink) {
             return 'sunk';
         } else {
             return 'hit';
         }
     }
 }
 public function testPlacer()
 {
     $field = new Battlefield(6, 6);
     $battleship = new Battleship();
     $validPlaces = $field->getValidPlaces($battleship);
     $expectedResults = [];
     for ($i = 0; $i < 6; $i++) {
         $expectedResults[] = ['start' => new Point(0, $i), 'end' => new Point(4, $i)];
         $expectedResults[] = ['start' => new Point(1, $i), 'end' => new Point(5, $i)];
         $expectedResults[] = ['start' => new Point($i, 0), 'end' => new Point($i, 4)];
         $expectedResults[] = ['start' => new Point($i, 1), 'end' => new Point($i, 5)];
     }
     $this->assertEquals(24, count($validPlaces));
     foreach ($expectedResults as $expectedValidPlaces) {
         $valid = false;
         foreach ($validPlaces as $validPlace) {
             if ($expectedValidPlaces['start']->isSameAs($validPlace->getStartPoint()) && $expectedValidPlaces['end']->isSameAs($validPlace->getEndPoint())) {
                 $valid = true;
                 break;
             }
         }
         if (!$valid) {
             $errorMsg = "Expected result not found: [{$validPlace->getStartPoint()->getX()},{$validPlace->getStartPoint()->getY()}";
             $errorMsg .= ";{$validPlace->getEndPoint()->getX()},{$validPlace->getEndPoint()->getY()}]";
             $this->assertTrue($valid, $errorMsg);
         }
     }
 }
예제 #4
0
 public function testBattleShipFreePoint()
 {
     $field = new Battlefield(10, 10);
     $battleShip = new Battleship();
     $placer = new Placer($battleShip, new Point(0, 0), new Point(0, 4));
     $field->addBattleship($placer);
     $validPoints = [new Point(5, 0), new Point(6, 0)];
     $invalidPoints = [new Point(0, 0), new Point(0, 1), new Point(0, 2), new Point(0, 3), new Point(0, 4)];
     foreach ($validPoints as $validPoint) {
         $this->assertTrue($field->isPointFree($validPoint), "Point [{$validPoint->getX()}, {$validPoint->getY()}] should be valid");
     }
     foreach ($invalidPoints as $invalidPoint) {
         $this->assertFalse($field->isPointFree($invalidPoint), "Point [{$invalidPoint->getX()}, {$invalidPoint->getY()}] should be invalid");
     }
 }
예제 #5
0
 /**
  * Force user to enter valid point
  * @param Battlefield $battlefield
  * @return \Model\Battlefield\Point\PointInterface
  */
 protected function shoot(Battlefield $battlefield)
 {
     $pointFactory = new \Model\Battlefield\Point\PointFactory();
     $shot = null;
     while ($shot === null) {
         echo "Enter point coordinates: ";
         $coordinates = trim(fgets(STDIN));
         try {
             $shot = $pointFactory->createPoint($coordinates);
             $battlefield->shoot($shot);
         } catch (\Model\Battlefield\Exception\HumanReadableException $e) {
             echo "Error. " . $e->getMessage() . PHP_EOL;
             $shot = null;
         }
     }
     return $shot;
 }
예제 #6
0
 public function testGetLastShotStatusSink()
 {
     $battlefield = new Battlefield(10, 10);
     $visualizer = new Visualizer($battlefield);
     $placer = new Placer(new \Model\Battleship\Destroyer(), new Point(0, 0), new Point(3, 0));
     $battlefield->addBattleship($placer);
     $battlefield->shoot(new Point(1, 1));
     $this->assertSame('miss', $visualizer->getLastShotStatus());
     $battlefield->shoot(new Point(0, 0));
     $this->assertSame('hit', $visualizer->getLastShotStatus());
     $battlefield->shoot(new Point(1, 0));
     $battlefield->shoot(new Point(2, 0));
     $battlefield->shoot(new Point(3, 0));
     $this->assertSame('sunk', $visualizer->getLastShotStatus());
 }