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"); } }
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'; } } }