' . "\n";
foreach ($row as $columnIndex => $column) {
$position = new Position($rowIndex + 1, $columnIndex + 1);
- echo '' . $table->getSpotIndexByPosition($position) . ' | ' . "\n";
+ echo '' . $table->getSpotNumberByPosition($position) . ' | ' . "\n";
}
echo '
' . "\n";
}
diff --git a/src/Coffee/Map.php b/src/Coffee/Map.php
index 67ba5eb..5ab4438 100644
--- a/src/Coffee/Map.php
+++ b/src/Coffee/Map.php
@@ -2,15 +2,11 @@
namespace Coffee;
- /**
- * Class Map
- *
- * @package Coffee
- */
/**
* Class Map
*
* @package Coffee
+ * TODO: make class Map abstract
*/
class Map {
@@ -28,6 +24,11 @@ class Map {
*/
private $width = 0;
+ /**
+ * @var array[]
+ */
+ private $description = [];
+
/**
* Map constructor.
*
@@ -40,27 +41,31 @@ class Map {
throw new \Exception('The Coffee Table map could not be loaded.');
}
- $height = 0;
- $width = 0;
+ $this->description = $description;
- // This data processing could be written more read-ably in multiple cycles / private methods,
- // but at a cost of reduced performance (minor).
+ $width = 0;
foreach ($description as $rowIndex => $row) {
foreach ($row as $columnIndex => $tileRepresentation) {
- // W need to convert indices to positions
+ // Convert indices to positions
$this->tiles[] = new Tile($rowIndex + 1, $columnIndex + 1, $tileRepresentation);
- // Find maximum
+ // Find longest row
$width = $columnIndex > $width ? $columnIndex : $width;
}
- // Find maximum
- $height = $rowIndex > $height ? $rowIndex : $height;
}
// Convert indices to dimensions
- $this->height = $height + 1;
$this->width = $width + 1;
+ $this->height = count($this->getDescription());
+
+ }
+
+ /**
+ * @return array
+ */
+ public function getDescription() {
+ return $this->description;
}
/**
@@ -70,10 +75,10 @@ class Map {
return $this->tiles;
}
-
/**
* @param Position $position
* @return Tile|null
+ * TODO: this might be ambiguous
*/
public function getTileByPosition(Position $position) {
foreach ($this->getTiles() as $tile) {
@@ -85,22 +90,10 @@ class Map {
return null;
}
- /**
- * @return array
- */
- public function describedByArray() {
- $array = [];
-
- foreach ($this->getTiles() as $tile) {
- $array[$tile->getRowIndex()][$tile->getColumnIndex()] = $tile->isRepresentingSpot();
- }
-
- return $array;
- }
-
/**
* @param Tile $tile
* @return Tile[]
+ * TODO: this might be ambiguous
*/
public function getNeighboursOfTile(Tile $tile) {
$neighbouringTiles = [];
diff --git a/src/Coffee/Table.php b/src/Coffee/Table.php
index 0fed0a8..37c0efc 100644
--- a/src/Coffee/Table.php
+++ b/src/Coffee/Table.php
@@ -12,18 +12,13 @@ namespace Coffee;
*
* @package Coffee
*/
-class Table {
+class Table extends Map {
/**
* @var Spot[]
*/
private $spots = [];
- /**
- * @var Map
- */
- private $map;
-
/**
* @var Spot
*/
@@ -37,11 +32,11 @@ class Table {
/**
* Table constructor.
*
- * @param Map $map
+ * @param $description [][]
*/
- public function __construct(Map $map) {
- $this->map = $map;
+ public function __construct($description) {
+ parent::__construct($description);
$this->recurseMap();
}
@@ -70,7 +65,7 @@ class Table {
* @param Position $searchedPosition
* @return int|string
*/
- public function getSpotIndexByPosition(Position $searchedPosition) {
+ public function getSpotNumberByPosition(Position $searchedPosition) {
// Linear search
// TODO: try to find a faster way
foreach ($this->getSpots() as $spotIndex => $spot) {
@@ -142,11 +137,11 @@ class Table {
*/
protected function getNextTile($tile = null) {
if (is_null($tile)) {
- $tiles = $this->map->getTiles();
+ $tiles = $this->getTiles();
return $tiles;
}
else {
- $tiles = $this->map->getNeighboursOfTile($tile);
+ $tiles = $this->getNeighboursOfTile($tile);
return $tiles;
}
}
diff --git a/tests/Coffee/MapTest.php b/tests/Coffee/MapTest.php
index 4612c16..d75a0cc 100644
--- a/tests/Coffee/MapTest.php
+++ b/tests/Coffee/MapTest.php
@@ -15,17 +15,6 @@ class MapTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals([$tileA, $tileB], $map->getTiles());
}
-
- public function testDescribedByArray() {
- $description = [
- [1, 0],
- [0, 1],
- ];
-
- $map = new Map($description);
- $this->assertEquals($description, $map->describedByArray());
- }
-
public function testTileByPosition() {
$description = [
[1, 0],
diff --git a/tests/Coffee/TableTest.php b/tests/Coffee/TableTest.php
index 11b417f..4f5e68d 100644
--- a/tests/Coffee/TableTest.php
+++ b/tests/Coffee/TableTest.php
@@ -9,31 +9,31 @@ class TableTest extends \PHPUnit_Framework_TestCase {
public function testZeroSpotsCount() {
- $map = new Map([
+ $description = [
[0, 0],
[0, 0],
- ]);
+ ];
- $table = new Table($map);
+ $table = new Table($description);
$this->assertEquals(0, $table->getSpotsCount());
}
public function testTwoSingleSpotsCount() {
- $map = new Map([
+ $description = [
[0, 1, 0],
[0, 0, 1],
[1, 0, 0],
[0, 1, 0],
- ]);
+ ];
- $table = new Table($map);
+ $table = new Table($description);
$this->assertEquals(2, $table->getSpotsCount());
}
public function testMultipleLargeSpotsCount() {
- $map = new Map([
+ $description = [
[0, 1, 1, 0, 0],
[0, 0, 1, 0, 1],
[0, 1, 0, 0, 1],
@@ -41,22 +41,22 @@ class TableTest extends \PHPUnit_Framework_TestCase {
[1, 0, 0, 1, 1],
[0, 0, 0, 0, 1],
[0, 1, 0, 0, 0],
- ]);
+ ];
- $table = new Table($map);
+ $table = new Table($description);
$this->assertEquals(4, $table->getSpotsCount());
}
public function testOneSmallSpot() {
- $map = new Map([
+ $description = [
[0, 1, 1],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
- ]);
+ ];
- $table = new Table($map);
+ $table = new Table($description);
$spot = new Spot(new Position(1, 2));
$spot->addPosition(new Position(1, 3));
@@ -64,14 +64,14 @@ class TableTest extends \PHPUnit_Framework_TestCase {
}
public function testOneLargeSpot() {
- $map = new Map([
+ $description = [
[0, 1, 1],
[0, 0, 1],
[0, 1, 0],
[1, 0, 0],
- ]);
+ ];
- $table = new Table($map);
+ $table = new Table($description);
$spot = new Spot(new Position(1, 2));
$spot->addPosition(new Position(1, 3));
$spot->addPosition(new Position(2, 3));
@@ -82,14 +82,14 @@ class TableTest extends \PHPUnit_Framework_TestCase {
}
public function testMultipleLargeSpots() {
- $map = new Map([
+ $description = [
[0, 1, 1, 0, 0],
[0, 0, 1, 0, 1],
[0, 1, 0, 0, 1],
[1, 0, 0, 1, 0],
- ]);
+ ];
- $table = new Table($map);
+ $table = new Table($description);
$spotA = new Spot(new Position(1, 2));
$spotA->addPosition(new Position(1, 3));
@@ -105,29 +105,29 @@ class TableTest extends \PHPUnit_Framework_TestCase {
}
public function testLargestSpotSize() {
- $map = new Map([
+ $description = [
[0, 1, 1, 0, 0],
[1, 1, 1, 0, 1],
[0, 0, 1, 0, 1],
[1, 0, 0, 0, 0],
- ]);
+ ];
- $table = new Table($map);
+ $table = new Table($description);
$this->assertEquals(6, $table->getLargestSpot()->getSize());
}
- public function testSpotIndexByPosition() {
- $map = new Map([
+ public function testSpotNumberByPosition() {
+ $description = [
[0, 1, 1, 0, 0],
[1, 1, 1, 0, 1],
[0, 0, 1, 0, 1],
[1, 0, 0, 0, 0],
- ]);
+ ];
- $table = new Table($map);
+ $table = new Table($description);
$position = new Position(4, 1);
- $this->assertEquals(2, $table->getSpotIndexByPosition($position));
+ $this->assertEquals(2, $table->getSpotNumberByPosition($position));
}
}