/** * @see BattlefieldModel::flagWaterAroundShip * @test */ public function flagWaterAroundShipOnA1AlreadyDead() { $battlefield = MockFactory::getBattlefieldMock(); $battlefield->getCellByCoordinate('A1')->setFlags(CellModel::FLAG_DEAD); $cell = $battlefield->getCellByCoordinate('B2')->setFlags(CellModel::FLAG_SHIP); BattlefieldModel::flagWaterAroundShip($cell); foreach (['A2', 'A3', 'B1', 'B3', 'C1', 'C2', 'C3'] as $coordinate) { $this->assertTrue($battlefield->getCellByCoordinate($coordinate)->hasFlag(CellModel::FLAG_SKIP)); } }
public function AdjacentCellsProvider() : array { $battlefield = MockFactory::getBattlefieldMock(); $battlefield->getCellByCoordinate('A2')->setFlags(CellModel::FLAG_DEAD); $battlefield->getCellByCoordinate('A1')->setFlags(CellModel::FLAG_DEAD_SHIP); return ['defaults' => ['B2', ['A1', 'A2', 'A3', 'B1', 'B3', 'C1', 'C2', 'C3'], MockFactory::getBattlefieldMock(), 1, CellModel::FLAG_NONE, CellModel::FLAG_NONE], 'defaults: 2 dept' => ['B2', ['A1', 'A2', 'A3', 'B1', 'B3', 'B4', 'C1', 'C2', 'C3', 'D2', 'D4'], MockFactory::getBattlefieldMock(), 2, CellModel::FLAG_NONE, CellModel::FLAG_NONE], 'defaults: $onlyFlag: CellModel::FLAG_DEAD' => ['B2', ['A1', 'A2'], $battlefield, 1, CellModel::FLAG_DEAD, CellModel::FLAG_NONE], 'defaults: $excludeFlag: CellModel::FLAG_DEAD' => ['B2', ['A3', 'B1', 'B3', 'C1', 'C2', 'C3'], $battlefield, 1, CellModel::FLAG_NONE, CellModel::FLAG_DEAD]]; }
/** * @see AIStrategyService::chooseCells * @test */ public function chooseCellsOnNoDeadCellsInBattlefield() { $this->assertEmpty(static::$aiStrategyService->chooseCells(MockFactory::getBattlefieldMock())); }
/** * @see AIService::processCPUTurn * * @param array $coordinatesCollection * @param array $expectedFlags */ private function assertProcessCPUTurnResult(array $coordinatesCollection, array $expectedFlags) { $battlefield = MockFactory::getBattlefieldMock(); foreach ($coordinatesCollection as $flag => $coordinates) { foreach ($coordinates as $coordinate) { $battlefield->getCellByCoordinate($coordinate)->setFlags($flag); } } static::$ai->processCPUTurn($battlefield); foreach ($battlefield->getCells() as $cell) { $this->assertEquals($expectedFlags[$cell->getCoordinate()] ?? CellModel::FLAG_NONE, $cell->getFlags()); } }
}); it('should return 48 cells from 7x7 battlefield 48 of them not flagged with CellModel::FLAG_DEAD', function () { $this->battlefield->getCellByCoordinate('A1')->addFlag(CellModel::FLAG_DEAD); $cells = BattlefieldModel::getLiveCells($this->battlefield); expect($cells)->toBeA('array')->toHaveLength(48); $this->iterateRecievedCells($cells); }); it('should return empty array from 7x7 battlefield all of them flagged with CellModel::FLAG_DEAD', function () { /** @var Cell $cell */ foreach ($this->battlefield->getCells() as $cell) { $cell->addFlag(CellModel::FLAG_DEAD); } $cells = BattlefieldModel::getLiveCells($this->battlefield); expect($cells)->toBeA('array')->toHaveLength(0); }); }); /** * @see BattlefieldModel::hasUnfinishedShips */ describe('::hasUnfinishedShips - should return true if Battlefield contains cells flagged with CellModel::FLAG_SHIP and not flagged with CellModel::FLAG_DEAD', function () { it('should return false', function () { $battlefield = MockFactory::getBattlefieldMock(); expect(BattlefieldModel::hasUnfinishedShips($battlefield))->toBe(false); }); it('should return true', function () { $battlefield = MockFactory::getBattlefieldMock(); $battlefield->getCellByCoordinate('A1')->addFlag(CellModel::FLAG_SHIP); expect(BattlefieldModel::hasUnfinishedShips($battlefield))->toBe(true); }); }); });
/** * @see GameProcessor::processPlayerTurnOnBattlefield * @test */ public function processPlayerTurnOnBattlefieldToWin() { $battlefield = MockFactory::getBattlefieldMock()->setPlayer(MockFactory::getAIPlayerMock('')); $battlefield->getCellByCoordinate('A1')->setFlags(CellModel::FLAG_SHIP); $this->assertTrue($this->processPlayerTurnOnBattlefield($battlefield, MockFactory::getPlayerMock(''))); }
private function verifyCellsByStrategy(array $expectedCoordinates, int $strategy) { $cells = static::$strategyProcessor->process(MockFactory::getBattlefieldMock()->getCellByCoordinate('B2'), $strategy); $this->assertCount(count($expectedCoordinates), $cells); $this->assertContainsOnlyInstancesOf(Cell::class, $cells); foreach ($cells as $cell) { $this->assertContains($cell->getCoordinate(), $expectedCoordinates); } }