diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 4324d30..5fc5a3d 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,23 +4,10 @@
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
+
-
@@ -46,71 +33,71 @@
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
@@ -119,8 +106,8 @@
-
-
+
+
@@ -136,16 +123,6 @@
-
-
-
-
-
-
-
-
-
-
@@ -179,7 +156,6 @@
-
@@ -190,18 +166,19 @@
-
-
-
-
-
+
+
+
+
+
+
@@ -466,7 +443,7 @@
-
+
@@ -495,7 +472,7 @@
-
+
@@ -735,38 +712,50 @@
1458293898715
-
+
+ 1458296131700
+
+
+ 1458296131700
+
+
+ 1458304427895
+
+
+ 1458304427895
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
@@ -795,28 +784,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -834,8 +801,6 @@
-
-
@@ -859,23 +824,36 @@
-
+
+
+
+
+
+ file://$PROJECT_DIR$/tests/Coffee/TableTest.php
+ 45
+
+
+
-
+
+
+
+
+
@@ -1012,13 +990,6 @@
-
-
-
-
-
-
-
@@ -1120,14 +1091,6 @@
-
-
-
-
-
-
-
-
@@ -1152,10 +1115,10 @@
-
+
-
-
+
+
@@ -1168,17 +1131,25 @@
+
+
+
+
+
+
+
+
-
-
+
+
-
+
@@ -1186,32 +1157,40 @@
-
-
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
+
-
-
+
+
-
+
-
-
+
+
diff --git a/index.php b/index.php
index 4a8cd8c..b6d6911 100644
--- a/index.php
+++ b/index.php
@@ -5,40 +5,20 @@ namespace Coffee;
require __DIR__ . '/vendor/autoload.php';
try {
- $map = new Map([
- [0, 1, 0, 1],
- [1, 0, 0, 0],
- [0, 0, 0, 1],
- [0, 0, 1, 1]
- ]);
+ $description = [
+ [0, 1, 0, 1],
+ [1, 0, 0, 0],
+ [0, 0, 0, 1],
+ [0, 0, 1, 1]
+ ];
- $table = new Table($map);
+ $map = new Map($description);
+
+ $table = new Table($map);
+
+ var_dump($table->getSpots());
- var_dump($table->getSpots());
-//
-// // Foreach cannot be used, we need re-evaluation
-//// while (list(, $currentTile) = each($map->getUnVisitedTiles())) {
-//// while (($currentTile = $map->getUnvisitedTile()) == true) {
-// while (isset($map->getUnVisitedTiles()[0]) && ($currentTile = $map->getUnVisitedTiles()[0]) == true) {
-// $currentTile->visit();
-//
-// if ($currentTile->isRepresentingSpot()) {
-// $spot = new Spot($currentTile->getPosition());
-//
-// foreach ($map->getNeighboursOfTile($currentTile) as $neighbouringTile) {
-// $neighbouringTile->visit();
-//
-// if ($neighbouringTile->isRepresentingSpot()) {
-// $spot->addPosition($neighbouringTile->getPosition());
-// }
-// }
-//
-// $table->addSpot($spot);
-// }
-// }
-//
-// var_dump($table);
}
catch (\Exception $e) {
- echo 'Caught exception: ' . $e->getMessage() . "\n";
+ echo 'Caught exception: ' . $e->getMessage() . "\n";
}
diff --git a/src/Coffee/Map.php b/src/Coffee/Map.php
index 525b086..d35b1de 100644
--- a/src/Coffee/Map.php
+++ b/src/Coffee/Map.php
@@ -145,54 +145,5 @@ class Map {
public function getWidth() {
return $this->width;
}
-//
-// /**
-// * @param Position $position
-// * @return bool
-// */
-// public function visitPosition(Position $position) {
-// if ($this->hasValidPosition($position)) {
-// foreach ($this->tiles as $unVisitedTileIndex => $unVisitedTile) {
-// if ($unVisitedTile->isTheSamePosition($position)) {
-//
-// // Move Tile from one group to another and reorder
-// $this->visitedTiles[] = $unVisitedTile;
-// array_splice($this->tiles, $unVisitedTileIndex, 1);
-//
-// return true;
-// }
-// }
-// }
-//
-// return false;
-// }
-//
-// /**
-// * Test the upper bound
-// *
-// * @param Position $position
-// * @return bool
-// */
-// public function hasValidPosition(Position $position) {
-// if ($position->getRow() > $this->getHeight() || $position->getColumn() > $this->getWidth()) {
-// return false;
-// }
-//
-// return true;
-// }
-//
-// /**
-// * @param $position
-// * @return bool
-// */
-// public function isVisitedPosition(Position $position) {
-// foreach ($this->visitedTiles as $visitedTile) {
-// if ($visitedTile->isTheSamePosition($position)) {
-// return true;
-// }
-// }
-//
-// return false;
-// }
}
\ No newline at end of file
diff --git a/src/Coffee/Spot.php b/src/Coffee/Spot.php
index 12e087b..c3db3f0 100644
--- a/src/Coffee/Spot.php
+++ b/src/Coffee/Spot.php
@@ -8,11 +8,6 @@
namespace Coffee;
- /**
- * Class Spot
- *
- * @package Coffee
- */
/**
* Class Spot
*
@@ -31,7 +26,9 @@ class Spot {
* @param Position $position
*/
public function __construct(Position $position) {
+// if (!is_null($position)) {
$this->positions[] = $position;
+// }
}
/**
diff --git a/src/Coffee/Table.php b/src/Coffee/Table.php
index 1999d94..ad05173 100644
--- a/src/Coffee/Table.php
+++ b/src/Coffee/Table.php
@@ -2,11 +2,11 @@
namespace Coffee;
-/**
- * Class Table
- *
- * @package Coffee
- */
+ /**
+ * Class Table
+ *
+ * @package Coffee
+ */
/**
* Class Table
*
@@ -19,33 +19,25 @@ class Table {
*/
private $spots = [];
+ /**
+ * @var Map
+ */
+ private $map;
+
+ /**
+ * @var Spot
+ */
+ private $currentSpot = null;
+
/**
* Table constructor.
*
* @param Map $map
*/
public function __construct(Map $map) {
- foreach ($map->getTiles() as $currentTile) {
-
- if (!$currentTile->isVisited()) {
- $currentTile->visit();
-
- if ($currentTile->isRepresentingSpot()) {
- $spot = new Spot($currentTile->getPosition());
-
- foreach ($map->getNeighboursOfTile($currentTile) as $neighbouringTile) {
- $neighbouringTile->visit();
+ $this->map = $map;
- if ($neighbouringTile->isRepresentingSpot()) {
- $spot->addPosition($neighbouringTile->getPosition());
- }
- }
-
- $this->addSpot($spot);
- }
- }
-
- }
+ $this->recur();
}
/**
@@ -63,16 +55,77 @@ class Table {
}
/**
- * @param Spot $spot
+ * @param Tile|null $tile
+ */
+ protected function recur($tile = null) {
+ $tiles = $this->getNextTile($tile);
+ foreach ($tiles as $currentTile) {
+
+ if (!$currentTile->isVisited()) {
+ $currentTile->visit();
+
+ if ($currentTile->isRepresentingSpot()) {
+
+ $this->updateCurrentSpot($currentTile->getPosition());
+
+ // Recursion?
+// $tiles = $this->getNextTile($currentTile);
+// foreach ($tiles as $neighbouringTile) {
+//
+// if (!$neighbouringTile->isVisited()) {
+// $neighbouringTile->visit();
+//
+// if ($neighbouringTile->isRepresentingSpot()) {
+// $this->updateCurrentSpot($neighbouringTile->getPosition());
+// }
+//
+// }
+// }
+ $this->recur($currentTile);
+
+ $this->addCurrentSpot();
+
+ }
+ }
+
+ }
+ }
+
+ protected function updateCurrentSpot(Position $position) {
+ if (is_null($this->currentSpot)) {
+ $this->currentSpot = new Spot($position);
+ }
+ else {
+ $this->currentSpot->addPosition($position);
+ }
+ }
+
+ /**
* @return bool
*/
- protected function addSpot(Spot $spot) {
- if (is_null($spot)) {
+ protected function addCurrentSpot() {
+ if (is_null($this->currentSpot)) {
return false;
}
- $this->spots[] = $spot;
+ $this->spots[] = $this->currentSpot;
+ $this->currentSpot = null;
return true;
}
+ /**
+ * @param $tile
+ * @return Tile[]
+ */
+ private function getNextTile($tile = null) {
+ if (is_null($tile)) {
+ $tiles = $this->map->getTiles();
+ return $tiles;
+ }
+ else {
+ $tiles = $this->map->getNeighboursOfTile($tile);
+ return $tiles;
+ }
+ }
+
}
\ No newline at end of file
diff --git a/tests/Coffee/TableTest.php b/tests/Coffee/TableTest.php
index 12dd496..52dc45c 100644
--- a/tests/Coffee/TableTest.php
+++ b/tests/Coffee/TableTest.php
@@ -7,37 +7,100 @@ require __DIR__ . '/../../vendor/autoload.php';
class TableTest extends \PHPUnit_Framework_TestCase {
-//
-// public function testGetSpots() {
-// $table = new Table();
-// $table->addSpot($spot);
-// $this->assertEquals([$spot], $table->getSpots());
-// }
public function testZeroSpotsCount() {
- $description = [
+ $map = new Map([
[0, 0],
[0, 0],
- ];
+ ]);
- $map = new Map($description);
$table = new Table($map);
$this->assertEquals(0, $table->getSpotsCount());
}
public function testTwoSingleSpotsCount() {
- $description = [
+ $map = new Map([
[0, 1, 0],
- [0, 0, 0],
+ [0, 0, 1],
[1, 0, 0],
- [0, 0, 0],
- ];
+ [0, 1, 0],
+ ]);
- $map = new Map($description);
$table = new Table($map);
$this->assertEquals(2, $table->getSpotsCount());
}
+ public function testMultipleLargeSpotsCount() {
+ $map = new Map([
+ [0, 1, 1, 0, 0],
+ [0, 0, 1, 0, 1],
+ [0, 1, 0, 0, 1],
+ [1, 1, 0, 0, 0],
+ [1, 0, 0, 1, 1],
+ [0, 0, 0, 0, 1],
+ [0, 1, 0, 0, 0],
+ ]);
+
+ $table = new Table($map);
+
+ $this->assertEquals(4, $table->getSpotsCount());
+ }
+
+ public function testOneSmallSpot() {
+ $map = new Map([
+ [0, 1, 1],
+ [0, 0, 0],
+ [0, 0, 0],
+ [0, 0, 0],
+ ]);
+
+ $table = new Table($map);
+ $spot = new Spot(new Position(1, 2));
+ $spot->addPosition(new Position(1, 3));
+
+ $this->assertEquals([$spot], $table->getSpots());
+ }
+
+ public function testOneLargeSpot() {
+ $map = new Map([
+ [0, 1, 1],
+ [0, 0, 1],
+ [0, 1, 0],
+ [1, 0, 0],
+ ]);
+
+ $table = new Table($map);
+ $spot = new Spot(new Position(1, 2));
+ $spot->addPosition(new Position(1, 3));
+ $spot->addPosition(new Position(2, 3));
+ $spot->addPosition(new Position(3, 2));
+ $spot->addPosition(new Position(4, 1));
+
+ $this->assertEquals([$spot], $table->getSpots());
+ }
+
+ public function testMultipleLargeSpots() {
+ $map = new Map([
+ [0, 1, 1, 0, 0],
+ [0, 0, 1, 0, 1],
+ [0, 1, 0, 0, 1],
+ [1, 0, 0, 1, 0],
+ ]);
+
+ $table = new Table($map);
+
+ $spotA = new Spot(new Position(1, 2));
+ $spotA->addPosition(new Position(1, 3));
+ $spotA->addPosition(new Position(2, 3));
+ $spotA->addPosition(new Position(3, 2));
+ $spotA->addPosition(new Position(4, 1));
+
+ $spotB = new Spot(new Position(2, 5));
+ $spotB->addPosition(new Position(3, 5));
+ $spotB->addPosition(new Position(4, 4));
+
+ $this->assertEquals([$spotA, $spotB], $table->getSpots());
+ }
}