diff --git a/src/Coffee/Map.php b/src/Coffee/Map.php index 33cbfea..80a0113 100644 --- a/src/Coffee/Map.php +++ b/src/Coffee/Map.php @@ -8,6 +8,7 @@ namespace Coffee; * @package Coffee */ class Map { + /** * @var Tile[] */ @@ -29,10 +30,11 @@ class Map { /** * Map constructor. * - * @param $description [][] The description must be 2D array containing at least one element + * @param $description [][] * @throws \Exception */ public function __construct($description) { + // The description must be 2D array containing at least one element if (is_null($description) || !is_array($description) || !is_array($description[0]) || count($description[0]) < 1) { throw new \Exception('The Coffee Table map could not be loaded.'); } @@ -79,7 +81,6 @@ class Map { * @return array */ public function describedByArray() { -// return $this->unVisited; $array = []; foreach ($this->getUnVisitedTiles() as $tile) { @@ -99,8 +100,16 @@ class Map { */ public function visitTile(Position $position) { if ($this->isValidPosition($position)) { - $this->visitedTiles[$position->getRow()][$position->getColumn()] = true; - return true; + foreach ($this->unVisitedTiles as $unVisitedTileIndex => $unVisitedTile) { + if ($unVisitedTile->isTheSamePosition($position)) { + + // Move Tile from one group to another and reorder + $this->visitedTiles[] = $unVisitedTile; + array_splice($this->unVisitedTiles, $unVisitedTileIndex, 1); + + return true; + } + } } return false; @@ -141,12 +150,13 @@ class Map { * @return bool */ public function isVisitedPosition(Position $position) { - if (!isset($this->visitedTiles[$position->getRow()][$position->getColumn()])) { - return false; + foreach ($this->visitedTiles as $visitedTile) { + if ($visitedTile->isTheSamePosition($position)) { + return true; + } } - return ($this->visitedTiles[$position->getRow()][$position->getColumn()] == true); + return false; } - } \ No newline at end of file diff --git a/tests/Coffee/MapTest.php b/tests/Coffee/MapTest.php index a4afad8..7f9d1f2 100644 --- a/tests/Coffee/MapTest.php +++ b/tests/Coffee/MapTest.php @@ -95,19 +95,4 @@ class MapTest extends \PHPUnit_Framework_TestCase { $this->assertTrue($map->isVisitedPosition($position)); } -// public function testNextUnvisitedPosition() { -// $description = [ -// [0, 1, 0, 1], -// [1, 0, 0, 0], -// [0, 0, 0, 1], -// [0, 0, 0, 1] -// ]; -// -// $map = new Map($description); -// $map->visitPosition(0, 0); -// $map->visitPosition(0, 1); -// -// $position = new Position(0, 2); -// $this->assertEquals($position, $map->getNextUnvisitedPosition()); -// } }