diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 654883b..a9d5d75 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,10 +4,13 @@ - + - - + + + + + @@ -36,21 +39,19 @@ - - + + - + - - - - - + + + @@ -58,18 +59,18 @@ - - + + - + - - + + @@ -78,18 +79,18 @@ - - + + - - + + - - + + @@ -108,8 +109,18 @@ - - + + + + + + + + + + + + @@ -155,22 +166,23 @@ - - - - - - + + + + + + + @@ -800,38 +812,44 @@ 1458516167633 - + + 1458516661818 + + + 1458516661818 + + - + - + - + - + - + - + - + - + - + - + @@ -850,7 +868,7 @@ - + @@ -887,8 +905,6 @@ - - @@ -912,7 +928,9 @@ - + + + @@ -921,36 +939,24 @@ - file://$PROJECT_DIR$/tests/Coffee/TableTest.php - 19 - + file://$PROJECT_DIR$/src/Coffee/Tile.php + 102 + + + + file://$PROJECT_DIR$/index.php + 22 + - + - - - - - - - - - - + - - - - - - - - @@ -987,9 +993,7 @@ - - - + @@ -1012,9 +1016,7 @@ - - - + @@ -1037,9 +1039,7 @@ - - - + @@ -1185,21 +1185,6 @@ - - - - - - - - - - - - - - - @@ -1248,92 +1233,114 @@ - + - - + + - + - - + + - + - - - - - + + + - + - - + + - + - - + + - + - - + + - + - - + + - + - + - - + + - + - - + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + diff --git a/index.php b/index.php index 78342b5..680a6dd 100644 --- a/index.php +++ b/index.php @@ -14,16 +14,19 @@ try { $table = new Table($map); - echo ''; - foreach ($table->getTiles() as $row) { + echo '' . "\n"; + foreach ($table->getStructuredTiles() as $row) { echo '' . "\n"; /** @var Tile $tile */ foreach ($row as $tile) { - echo '' . $tile->getSpotNumber() . '' . "\n"; + $formattedTile = $tile->getSpotNumber() == 0 ? '' : $tile->getSpotNumber(); + echo '' . $formattedTile . '' . "\n"; } echo '' . "\n"; } - echo ''; + echo '' . "\n"; + +// echo 'Najväčšia kávová kaluž je s číslom '.$table->getLargestSpot()->ge } catch (\Exception $e) { diff --git a/src/Coffee/Table.php b/src/Coffee/Table.php index 4a5aafd..ad3b367 100644 --- a/src/Coffee/Table.php +++ b/src/Coffee/Table.php @@ -38,39 +38,15 @@ class Table extends Map { parent::__construct($description); -// if (is_null($this->currentSpot)) { -// $tiles = $this->getTiles(); -// } -// else { -// $tiles = $this->currentSpot->getQueuedTiles(); -// } -// -// foreach ($tiles as $rowIndex => $row) { -// foreach ($row as $columnIndex => $tile) { -// /** @var Tile $tile */ -// $tile->visit(); -// if ($tile->isRepresentingSpot()) { -// foreach ($this->getNeighboursOfTile($tile) as $neighbourIndex => $neighbourTile) { -// $neighbourTile->visit(); -// -// if ($tile->isRepresentingSpot()) { -// -// } -// } -// } -// } -// -// } - $this->expandSpotsRecursively(); } -// /** -// * @return Spot[] -// */ -// public function getSpots() { -// return $this->spots; -// } + /** + * @return Spot[] + */ + public function getSpots() { + return $this->spots; + } /** * @return int @@ -86,31 +62,13 @@ class Table extends Map { return $this->largestSpot; } -// /** -// * @param Tile $searchedPosition -// * @return int|string -// */ -// public function getSpotNumberByPosition(Tile $searchedPosition) { -// // Linear search -// // TODO: try to find a faster way -// foreach ($this->getSpots() as $spotIndex => $spot) { -// foreach ($spot->getTiles() as $position) { -// if ($searchedPosition->isTheSamePosition($position)) { -// return $spotIndex; -// } -// } -// } -// -// return ''; -// } - /** * @param Tile|null $tile */ protected function expandSpotsRecursively($tile = null) { - $tiles = $this->getTilePool($tile); - foreach ($tiles as $currentTile) { + // Either get Tiles from Map if root or from neighbours if called recursively with argument + foreach ($this->getTilePool($tile) as $currentTile) { /** @var Tile $currentTile */ if (!$currentTile->isVisited()) { @@ -118,12 +76,14 @@ class Table extends Map { if ($currentTile->isRepresentingSpot()) { + // Either form a new Spot or add current Tile to the existing one $this->updateCurrentSpot($currentTile); // TODO: Replace recursion by iteration $this->expandSpotsRecursively($currentTile); - $this->addCurrentSpot(); // (to this table) + // No other neighbouring tiles were found + $this->finishCurrentSpot(); } } @@ -131,10 +91,25 @@ class Table extends Map { } } + /** + * @param $tile + * @return Tile[] + */ + protected function getTilePool($tile = null) { + if (is_null($tile)) { + $tiles = $this->getTiles(); + return $tiles; + } + else { + $tiles = $this->getNeighboursOfTile($tile); + return $tiles; + } + } + /** * @param Tile $tile */ - protected function updateCurrentSpot(Tile $tile) { + private function updateCurrentSpot(Tile $tile) { if (is_null($this->currentSpot)) { $this->currentSpot = new Spot($tile); } @@ -143,40 +118,21 @@ class Table extends Map { } } - /** - * @return bool - */ - protected function addCurrentSpot() { + private function finishCurrentSpot() { + // Do the finishing process only on the started Spots if (is_null($this->currentSpot)) { - return false; + return; } $this->updateLargestSpot(); + // The Spots are numbered from 1 + $this->currentSpot->setNumber($this->getSpotsCount() + 1); $this->spots[] = $this->currentSpot; $this->currentSpot = null; - return true; } - /** - * @param $tile - * @return Tile[] - */ - protected function getTilePool($tile = null) { - if (is_null($tile)) { - $tiles = $this->getTiles(); - return $tiles; - } - else { - $tiles = $this->getNeighboursOfTile($tile); - return $tiles; - } - } - - /** - * Just a wrapper to have it neat and clean - */ - protected function updateLargestSpot() { + private function updateLargestSpot() { if (is_null($this->largestSpot)) { $this->largestSpot = $this->currentSpot; } diff --git a/src/Coffee/Tile.php b/src/Coffee/Tile.php index 7278dc5..5967b1e 100644 --- a/src/Coffee/Tile.php +++ b/src/Coffee/Tile.php @@ -7,11 +7,6 @@ namespace Coffee; * * @package Coffee */ -/** - * Class Tile - * - * @package Coffee - */ class Tile extends Position { /** @@ -23,15 +18,19 @@ class Tile extends Position { * Default option */ const REPRESENTS_VOID = 0; + /** + * Tiles are inherently unvisited + */ + const DEFAULT_VISITED_STATE = false; /** * @var integer */ + private $representation; /** - * Tiles are inherently unvisited * @var bool */ - private $visited = false; + private $visited = self::DEFAULT_VISITED_STATE; /** * Zero means that the tile is not in the spot * @var int @@ -41,19 +40,25 @@ class Tile extends Position { /** * Tile constructor. * - * @param $row - * @param $column - * @param $tileRepresentation + * @param $row + * @param $column + * @param int $tileRepresentation + * @param bool $visited * @throws \Exception */ - public function __construct($row, $column, $tileRepresentation = self::REPRESENTS_VOID) { + public function __construct($row, $column, $tileRepresentation = self::REPRESENTS_VOID, $visited = self::DEFAULT_VISITED_STATE) { if (!$this->isRepresentingSpot() && !$this->isRepresentingVoid()) { - throw new \Exception('The map contains invalid representations'); + throw new \Exception('The map description contains invalid representations.'); + } + + if (!is_bool($visited)) { + throw new \Exception('The visited argument must be boolean.'); } parent::__construct($row, $column); $this->representation = $tileRepresentation; + $this->visited = $visited; } /** @@ -83,13 +88,13 @@ class Tile extends Position { public function isRepresentingVoid() { return $this->representation == self::REPRESENTS_VOID; } - - /** - * @return Tile - */ - public function getPosition() { - return new Tile($this->getRow(), $this->getColumn()); - } +// +// /** +// * @return Tile +// */ +// public function getPosition() { +// return new Tile($this->getRow(), $this->getColumn()); +// } /** * @return int diff --git a/tests/Coffee/TableTest.php b/tests/Coffee/TableTest.php index c5eb5de..20866fd 100644 --- a/tests/Coffee/TableTest.php +++ b/tests/Coffee/TableTest.php @@ -41,57 +41,61 @@ class TableTest extends \PHPUnit_Framework_TestCase { $this->assertEquals(4, $table->getSpotsCount()); } -// public function testOneSmallSpot() { -// $table = new Table([ -// [0, 1, 1], -// [0, 0, 0], -// [0, 0, 0], -// [0, 0, 0], -// ]); -// -// $spot = new Spot(new Tile(1, 2)); -// $spot->addTile(new Tile(1, 3)); -// -// $this->assertEquals([$spot], $table->getSpots()); -// } -// -// public function testOneLargeSpot() { -// $table = new Table([ -// [0, 1, 1], -// [0, 0, 1], -// [0, 1, 0], -// [1, 0, 0], -// ]); -// -// $spot = new Spot(new Tile(1, 2)); -// $spot->addTile(new Tile(1, 3)); -// $spot->addTile(new Tile(2, 3)); -// $spot->addTile(new Tile(3, 2)); -// $spot->addTile(new Tile(4, 1)); -// -// $this->assertEquals([$spot], $table->getSpots()); -// } -// -// public function testMultipleLargeSpots() { -// $table = new Table([ -// [0, 1, 1, 0, 0], -// [0, 0, 1, 0, 1], -// [0, 1, 0, 0, 1], -// [1, 0, 0, 1, 0], -// ]); -// -// $spotA = new Spot(new Tile(1, 2)); -// $spotA->addTile(new Tile(1, 3)); -// $spotA->addTile(new Tile(2, 3)); -// $spotA->addTile(new Tile(3, 2)); -// $spotA->addTile(new Tile(4, 1)); -// -// $spotB = new Spot(new Tile(2, 5)); -// $spotB->addTile(new Tile(3, 5)); -// $spotB->addTile(new Tile(4, 4)); -// -// $this->assertEquals([$spotA, $spotB], $table->getSpots()); -// } + public function testOneSmallSpot() { + $table = new Table([ + [0, 1, 1], + [0, 0, 0], + [0, 0, 0], + [0, 0, 0], + ]); + + $spot = new Spot($this->newVisitedTile(1, 2)); + $spot->addTile($this->newVisitedTile(1, 3)); + $spot->setNumber(1); + + $this->assertEquals([$spot], $table->getSpots()); + } + + public function testOneLargeSpot() { + $table = new Table([ + [0, 1, 1], + [0, 0, 1], + [0, 1, 0], + [1, 0, 0], + ]); + + $spot = new Spot($this->newVisitedTile(1, 2)); + $spot->addTile($this->newVisitedTile(1, 3)); + $spot->addTile($this->newVisitedTile(2, 3)); + $spot->addTile($this->newVisitedTile(3, 2)); + $spot->addTile($this->newVisitedTile(4, 1)); + $spot->setNumber(1); + + $this->assertEquals([$spot], $table->getSpots()); + } + + public function testMultipleLargeSpots() { + $table = new Table([ + [0, 1, 1, 0, 0], + [0, 0, 1, 0, 1], + [0, 1, 0, 0, 1], + [1, 0, 0, 1, 0], + ]); + + $spotA = new Spot($this->newVisitedTile(1, 2)); + $spotA->addTile($this->newVisitedTile(1, 3)); + $spotA->addTile($this->newVisitedTile(2, 3)); + $spotA->addTile($this->newVisitedTile(3, 2)); + $spotA->addTile($this->newVisitedTile(4, 1)); + $spotA->setNumber(1); + + $spotB = new Spot($this->newVisitedTile(2, 5)); + $spotB->addTile($this->newVisitedTile(3, 5)); + $spotB->addTile($this->newVisitedTile(4, 4)); + $spotB->setNumber(2); + + $this->assertEquals([$spotA, $spotB], $table->getSpots()); + } public function testLargestSpotSize() { $table = new Table([ @@ -104,16 +108,15 @@ class TableTest extends \PHPUnit_Framework_TestCase { $this->assertEquals(6, $table->getLargestSpot()->getSize()); } -// public function testSpotNumberByPosition() { -// $table = new Table([ -// [0, 1, 1, 0, 0], -// [1, 1, 1, 0, 1], -// [0, 0, 1, 0, 1], -// [1, 0, 0, 0, 0], -// ]); -// -// $position = new Tile(4, 1); -// -// $this->assertEquals(2, $table->getSpotNumberByPosition($position)); -// } + /** + * Wrapper for a visited tile to reduce repetition. This logic does not fit into the Spot class. + * + * @param $row + * @param $column + * @return Tile + */ + protected function newVisitedTile($row, $column) { + return new Tile($row, $column, Tile::REPRESENTS_SPOT, true); + } + } diff --git a/tests/Coffee/TileTest.php b/tests/Coffee/TileTest.php index d88660b..afe1a1a 100644 --- a/tests/Coffee/TileTest.php +++ b/tests/Coffee/TileTest.php @@ -19,16 +19,16 @@ class TileTest extends \PHPUnit_Framework_TestCase { $this->assertTrue($tile->isRepresentingVoid()); } - public function testGetPosition() { - $row = 2; - $column = 2; - - $tile = new Tile($row, $column, Tile::REPRESENTS_VOID); - $position = new Tile($row, $column); - - $this->assertEquals($position, $tile->getPosition()); - - } +// public function testGetPosition() { +// $row = 2; +// $column = 2; +// +// $tile = new Tile($row, $column, Tile::REPRESENTS_VOID); +// $position = new Tile($row, $column); +// +// $this->assertEquals($position, $tile->getPosition()); +// +// } public function testIsVisited() { $tile = new Tile(2, 2, Tile::REPRESENTS_SPOT);