diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 973eaba..ed54236 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,10 +4,9 @@
-
-
-
+
+
@@ -32,42 +31,42 @@
-
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
@@ -76,8 +75,8 @@
-
-
+
+
@@ -86,48 +85,8 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
@@ -186,17 +145,17 @@
-
-
-
-
-
-
+
+
+
+
+
+
@@ -424,6 +383,9 @@
+
+
+
@@ -471,8 +433,8 @@
-
-
+
+
@@ -500,21 +462,7 @@
-
-
-
- 1458141252466
-
-
-
- 1458141252466
-
-
- 1458150107686
-
-
-
- 1458150107686
+
1458150506101
@@ -845,43 +793,57 @@
1458675641510
-
+
+ 1458740079474
+
+
+
+ 1458740079474
+
+
+ 1458825316172
+
+
+
+ 1458825316172
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
@@ -898,7 +860,8 @@
-
+
+
@@ -906,9 +869,8 @@
-
+
-
@@ -917,28 +879,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -957,8 +897,6 @@
-
-
@@ -982,7 +920,9 @@
-
+
+
+
@@ -991,48 +931,38 @@
- file://$PROJECT_DIR$/index.php
- 53
-
+ file://$PROJECT_DIR$/tests/Coffee/TableTest.php
+ 102
+
+
+
+ file://$PROJECT_DIR$/src/Coffee/Table.php
+ 139
+
-
+
-
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -1045,6 +975,7 @@
+
@@ -1181,13 +1112,6 @@
-
-
-
-
-
-
-
@@ -1223,20 +1147,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -1258,13 +1168,6 @@
-
-
-
-
-
-
-
@@ -1276,6 +1179,7 @@
+
@@ -1294,81 +1198,137 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
+
-
-
+
+
+
-
+
-
-
+
+
-
+
-
-
+
+
-
+
-
-
+
+
-
+
-
-
+
+
-
+
-
-
+
+
-
+
+
+
+
+
+
+
+
+
-
-
+
+
-
-
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
diff --git a/index.php b/index.php
index 250e2ed..17a80c3 100644
--- a/index.php
+++ b/index.php
@@ -71,11 +71,17 @@ try {
echo '' . "\n";
}
echo '' . "\n";
-
echo '' . "\n";
- echo 'Najväčšia kávová kaluž je s číslom ' . $table->getLargestSpot()->getNumber() . '
' . "\n";
- echo 'Kaluž je veľká ' . $table->getLargestSpot()->getSize() . ' políčok.' . "\n";
+ echo 'Najväčšia kávová kaluž je s číslom ';
+ $numbers = '';
+ $delimiter = ', ';
+ foreach ($table->getLargestSpots() as $spot) {
+ $numbers .= $spot->getNumber() . $delimiter;
+ }
+ echo rtrim($numbers, $delimiter) . '
' . "\n";
+
+ echo 'Kaluž je veľká ' . $table->getFirstLargestSpot()->getSize() . ' políčok.' . "\n";
echo 'Počet kaluží je: ' . $table->getSpotsCount() . '
' . "\n";
}
catch (\Exception $e) {
diff --git a/src/Coffee/Queue.php b/src/Coffee/Queue.php
deleted file mode 100644
index b83dca0..0000000
--- a/src/Coffee/Queue.php
+++ /dev/null
@@ -1,30 +0,0 @@
-queue[] = $element;
- }
-
- /**
- * @return mixed
- */
- public function pop() {
- return array_shift($this->queue);
- }
-}
\ No newline at end of file
diff --git a/src/Coffee/Stack.php b/src/Coffee/Stack.php
new file mode 100644
index 0000000..4764500
--- /dev/null
+++ b/src/Coffee/Stack.php
@@ -0,0 +1,43 @@
+stack[] = $item;
+ }
+ }
+ else {
+ $this->stack[] = $var;
+ }
+ }
+
+ /**
+ * @return mixed
+ */
+ public function pop() {
+ return array_pop($this->stack);
+ }
+}
\ No newline at end of file
diff --git a/src/Coffee/Table.php b/src/Coffee/Table.php
index ad3b367..0de33f9 100644
--- a/src/Coffee/Table.php
+++ b/src/Coffee/Table.php
@@ -2,11 +2,6 @@
namespace Coffee;
- /**
- * Class Table
- *
- * @package Coffee
- */
/**
* Class Table
*
@@ -25,9 +20,14 @@ class Table extends Map {
private $currentSpot = null;
/**
- * @var Spot
+ * @var Spot[]
+ */
+ private $largestSpots = [];
+
+ /**
+ * @var Stack|null
*/
- private $largestSpot = null;
+ private $tileStack = null;
/**
* Table constructor.
@@ -38,72 +38,70 @@ class Table extends Map {
parent::__construct($description);
- $this->expandSpotsRecursively();
+ $this->tileStack = new Stack();
+ $this->tileStack->push(array_reverse($this->getTiles()));
+
+ /** @var Tile $tile */
+ while (($tile = $this->tileStack->pop()) !== null) {
+
+ // We have finished the spot
+ if ($tile->isRepresentingVoid()) {
+ $tile->visit();
+ $this->finishCurrentSpot();
+ continue;
+ }
+
+ // We have a Spot representing Tile
+ if (!$tile->isVisited()) {
+ $tile->visit();
+ $this->updateCurrentSpot($tile);
+
+ // Add all the neighbouring, unvisited, Spot representing Tiles
+ // of the current one to the Stack, so it is picked in next iteration
+ foreach ($this->getNeighboursOfTile($tile) as $neighbourTile) {
+ if (!$neighbourTile->isVisited() && $neighbourTile->isRepresentingSpot()) {
+ $this->tileStack->push($neighbourTile);
+ }
+ }
+ }
+
+ }
+
}
/**
* @return Spot[]
*/
- public function getSpots() {
+ public
+ function getSpots() {
return $this->spots;
}
/**
* @return int
*/
- public function getSpotsCount() {
+ public
+ function getSpotsCount() {
return count($this->spots);
}
/**
- * @return Spot
+ * @return Spot[]
*/
- public function getLargestSpot() {
- return $this->largestSpot;
+ public
+ function getLargestSpots() {
+ return $this->largestSpots;
}
/**
- * @param Tile|null $tile
+ * @return Spot
*/
- protected function expandSpotsRecursively($tile = null) {
-
- // 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()) {
- $currentTile->visit();
-
- 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);
-
- // No other neighbouring tiles were found
- $this->finishCurrentSpot();
-
- }
- }
-
+ public function getFirstLargestSpot() {
+ if (empty($this->largestSpots)) {
+ return null;
}
- }
- /**
- * @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;
- }
+ return $this->largestSpots[0];
}
/**
@@ -118,13 +116,16 @@ class Table extends Map {
}
}
+ /**
+ *
+ */
private function finishCurrentSpot() {
// Do the finishing process only on the started Spots
if (is_null($this->currentSpot)) {
return;
}
- $this->updateLargestSpot();
+ $this->updateLargestSpots();
// The Spots are numbered from 1
$this->currentSpot->setNumber($this->getSpotsCount() + 1);
@@ -132,15 +133,26 @@ class Table extends Map {
$this->currentSpot = null;
}
- private function updateLargestSpot() {
- if (is_null($this->largestSpot)) {
- $this->largestSpot = $this->currentSpot;
+ /**
+ *
+ */
+ private function updateLargestSpots() {
+ if (empty($this->largestSpots)) {
+ $this->largestSpots[0] = $this->currentSpot;
}
- $currentSize = is_null($this->largestSpot) ? 0 : $this->currentSpot->getSize();
- $largestSize = is_null($this->largestSpot) ? 0 : $this->largestSpot->getSize();
- // Maximum size
- $this->largestSpot = $currentSize > $largestSize ? $this->currentSpot : $this->largestSpot;
+ $currentSize = is_null($this->getFirstLargestSpot()) ? 0 : $this->currentSpot->getSize();
+ $largestSize = is_null($this->getFirstLargestSpot()) ? 0 : $this->getFirstLargestSpot()->getSize();
+
+ if ($largestSize < $currentSize) {
+ // Replace
+ $this->largestSpots = [];
+ $this->largestSpots[] = $this->currentSpot;
+ }
+ else if ($largestSize == $currentSize && !in_array($this->currentSpot, $this->largestSpots)) {
+ // Append only if not present
+ $this->largestSpots[] = $this->currentSpot;
+ }
}
}
\ No newline at end of file
diff --git a/tests/Coffee/QueueTest.php b/tests/Coffee/QueueTest.php
deleted file mode 100644
index 9ee9e50..0000000
--- a/tests/Coffee/QueueTest.php
+++ /dev/null
@@ -1,18 +0,0 @@
-push('1');
- $queue->push('2');
- $queue->push('3');
-
- $this->assertEquals('1', $queue->pop());
- }
-}
diff --git a/tests/Coffee/StackTest.php b/tests/Coffee/StackTest.php
new file mode 100644
index 0000000..4990fcb
--- /dev/null
+++ b/tests/Coffee/StackTest.php
@@ -0,0 +1,27 @@
+push('1');
+ $stack->push('2');
+ $stack->push('3');
+
+ $this->assertEquals('3', $stack->pop());
+ }
+
+ public function testArrayPushPop() {
+ $stack = new Stack();
+ $stack->push(['1', '2']);
+ $stack->push('3');
+ $stack->push(['4', '5', '6']);
+
+ $this->assertEquals('6', $stack->pop());
+ }
+}
diff --git a/tests/Coffee/TableTest.php b/tests/Coffee/TableTest.php
index 8f884aa..fbfc223 100644
--- a/tests/Coffee/TableTest.php
+++ b/tests/Coffee/TableTest.php
@@ -56,25 +56,25 @@ class TableTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals([$spot], $table->getSpots());
}
-// public function testOneLargeSpot() {
-// $table = new Table([
-// [0, 0, 1, 0],
-// [0, 0, 1, 0],
-// [0, 0, 1, 0],
-// [0, 1, 0, 1],
-// [1, 0, 0, 0],
-// ]);
-//
-// $spot = new Spot($this->newVisitedTile(1, 3));
-// $spot->addTile($this->newVisitedTile(2, 3));
-// $spot->addTile($this->newVisitedTile(3, 3));
-// $spot->addTile($this->newVisitedTile(4, 2));
-// $spot->addTile($this->newVisitedTile(4, 4));
-// $spot->addTile($this->newVisitedTile(5, 1));
-// $spot->setNumber(1);
-//
-// $this->assertEquals([$spot], $table->getSpots());
-// }
+ public function testOneLargeSpot() {
+ $table = new Table([
+ [0, 0, 1, 0],
+ [0, 0, 1, 0],
+ [0, 0, 1, 0],
+ [0, 1, 0, 1],
+ [1, 0, 0, 0],
+ ]);
+
+ $spot = new Spot($this->newVisitedTile(1, 3));
+ $spot->addTile($this->newVisitedTile(2, 3));
+ $spot->addTile($this->newVisitedTile(3, 3));
+ $spot->addTile($this->newVisitedTile(4, 2));
+ $spot->addTile($this->newVisitedTile(5, 1));
+ $spot->addTile($this->newVisitedTile(4, 4));
+ $spot->setNumber(1);
+
+ $this->assertEquals([$spot], $table->getSpots());
+ }
public function testMultipleLargeSpots() {
$table = new Table([
@@ -85,8 +85,8 @@ class TableTest extends \PHPUnit_Framework_TestCase {
]);
$spotA = new Spot($this->newVisitedTile(1, 2));
- $spotA->addTile($this->newVisitedTile(1, 3));
$spotA->addTile($this->newVisitedTile(2, 3));
+ $spotA->addTile($this->newVisitedTile(1, 3));
$spotA->addTile($this->newVisitedTile(3, 2));
$spotA->addTile($this->newVisitedTile(4, 1));
$spotA->setNumber(1);
@@ -99,20 +99,23 @@ class TableTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals([$spotA, $spotB], $table->getSpots());
}
-// public function testLargestSpotSize() {
-// $table = new Table([
-// [0, 1, 1, 0, 0],
-// [1, 1, 1, 0, 1],
-// [0, 0, 1, 0, 1],
-// [1, 0, 0, 0, 0],
-// [0, 0, 1, 1, 1],
-// [0, 0, 1, 0, 0],
-// [0, 1, 0, 1, 0],
-// [1, 0, 0, 0, 0],
-// ]);
-//
-// $this->assertEquals(7, $table->getLargestSpot()->getSize());
-// }
+ public function testLargestSpot() {
+ $table = new Table([
+ [0, 1, 1, 0, 0],
+ [0, 0, 1, 0, 1],
+ [0, 1, 0, 0, 1],
+ [1, 0, 0, 1, 0],
+ ]);
+
+ $spot = new Spot($this->newVisitedTile(1, 2));
+ $spot->addTile($this->newVisitedTile(2, 3));
+ $spot->addTile($this->newVisitedTile(1, 3));
+ $spot->addTile($this->newVisitedTile(3, 2));
+ $spot->addTile($this->newVisitedTile(4, 1));
+ $spot->setNumber(1);
+
+ $this->assertEquals([$spot], $table->getLargestSpots());
+ }
/**
* Wrapper for a visited tile to reduce repetition. This logic does not fit into the Spot class.