Implement Table.getSpotIndexByPosition

master
Peter Babič 8 years ago
parent 7b620b11b2
commit 1b83057a7e
Signed by: peter.babic
GPG Key ID: 4BB075BC1884BA40
  1. 15
      src/Coffee/Table.php
  2. 26
      tests/Coffee/TableTest.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;
}
/**

@ -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));
}
}

Loading…
Cancel
Save