diff --git a/src/Coffee/Table.php b/src/Coffee/Table.php index 400b3f5..7fdcf0e 100644 --- a/src/Coffee/Table.php +++ b/src/Coffee/Table.php @@ -67,10 +67,21 @@ class Table { } /** - * @param Position $position + * @param Position $searchedPosition + * @return int|null */ - public function getSpotIndexByPosition(Position $position) { + public function getSpotIndexByPosition(Position $searchedPosition) { + // Linear search + // TODO: try to find a faster way + foreach ($this->getSpots() as $spotIndex => $spot) { + foreach ($spot->getPositions() as $position) { + if ($searchedPosition->isTheSamePosition($position)) { + return $spotIndex; + } + } + } + return null; } /** diff --git a/tests/Coffee/TableTest.php b/tests/Coffee/TableTest.php index 80219c8..11b417f 100644 --- a/tests/Coffee/TableTest.php +++ b/tests/Coffee/TableTest.php @@ -117,17 +117,17 @@ class TableTest extends \PHPUnit_Framework_TestCase { $this->assertEquals(6, $table->getLargestSpot()->getSize()); } -// public function testSpotIndexByPosition() { -// $map = new Map([ -// [0, 1, 1, 0, 0], -// [1, 1, 1, 0, 1], -// [0, 0, 1, 0, 1], -// [1, 0, 0, 0, 0], -// ]); -// -// $table = new Table($map); -// $position = new Position(1, 2); -// -// $this->assertEquals(1, $table->getSpotIndexByPosition($position)); -// } + public function testSpotIndexByPosition() { + $map = new Map([ + [0, 1, 1, 0, 0], + [1, 1, 1, 0, 1], + [0, 0, 1, 0, 1], + [1, 0, 0, 0, 0], + ]); + + $table = new Table($map); + $position = new Position(4, 1); + + $this->assertEquals(2, $table->getSpotIndexByPosition($position)); + } }