Modify Spot so it now contains Tiles instead of Positions

master
Peter Babič 8 years ago
parent 1880ebe1aa
commit dfd8d2105d
Signed by: peter.babic
GPG Key ID: 4BB075BC1884BA40
  1. 2
      index.php
  2. 4
      src/Coffee/Map.php
  3. 32
      src/Coffee/Position.php
  4. 26
      src/Coffee/Spot.php
  5. 12
      src/Coffee/Table.php
  6. 7
      src/Coffee/Tile.php
  7. 4
      tests/Coffee/MapTest.php
  8. 74
      tests/Coffee/PositionTest.php
  9. 16
      tests/Coffee/SpotTest.php
  10. 32
      tests/Coffee/TableTest.php
  11. 2
      tests/Coffee/TileTest.php

@ -18,7 +18,7 @@ try {
foreach ($table->getDescription() as $rowIndex => $row) {
echo '<tr>' . "\n";
foreach ($row as $columnIndex => $column) {
$position = new Position($rowIndex + 1, $columnIndex + 1);
$position = new Tile($rowIndex + 1, $columnIndex + 1);
echo '<td>' . $table->getSpotNumberByPosition($position) . '</td>' . "\n";
}
echo '</tr>' . "\n";

@ -76,11 +76,11 @@ class Map {
}
/**
* @param Position $position
* @param Tile $position
* @return Tile|null
* TODO: this might be ambiguous
*/
public function getTileByPosition(Position $position) {
public function getTileByPosition(Tile $position) {
foreach ($this->getTiles() as $tile) {
if ($tile->isTheSamePosition($position)) {
return $tile;

@ -71,80 +71,80 @@ class Position {
}
/**
* @param Position $position
* @param Tile $position
* @return bool
*/
public function isTheSameColumn(Position $position) {
public function isTheSameColumn(Tile $position) {
return $this->getColumn() == $position->getColumn();
}
/**
* @param Position $position
* @param Tile $position
* @return bool
*/
public function isTheSameRow(Position $position) {
public function isTheSameRow(Tile $position) {
return $this->getRow() == $position->getRow();
}
/**
* @param Position $position
* @param Tile $position
* @return bool
*/
public function isTheSamePosition(Position $position) {
public function isTheSamePosition(Tile $position) {
return $this->isTheSameColumn($position) && $this->isTheSameRow($position);
}
/**
* @return Position
* @return Tile
*/
public function getNorthEastPosition() {
return $this->getSafeNeighbourPosition($this->getRow() - 1, $this->getColumn() + 1);
}
/**
* @return Position
* @return Tile
*/
public function getEastPosition() {
return $this->getSafeNeighbourPosition($this->getRow(), $this->getColumn() + 1);
}
/**
* @return Position
* @return Tile
*/
public function getSouthEastPosition() {
return $this->getSafeNeighbourPosition($this->getRow() + 1, $this->getColumn() + 1);
}
/**
* @return Position
* @return Tile
*/
public function getSouthPosition() {
return $this->getSafeNeighbourPosition($this->getRow() + 1, $this->getColumn());
}
/**
* @return Position
* @return Tile
*/
public function getSouthWestPosition() {
return $this->getSafeNeighbourPosition($this->getRow() + 1, $this->getColumn() - 1);
}
/**
* @return Position
* @return Tile
*/
public function getWestPosition() {
return $this->getSafeNeighbourPosition($this->getRow(), $this->getColumn() - 1);
}
/**
* @return Position
* @return Tile
*/
public function getNorthWestPosition() {
return $this->getSafeNeighbourPosition($this->getRow() - 1, $this->getColumn() - 1);
}
/**
* @return Position
* @return Tile
*/
public function getNorthPosition() {
return $this->getSafeNeighbourPosition($this->getRow() - 1, $this->getColumn());
@ -184,11 +184,11 @@ class Position {
/**
* @param $row
* @param $column
* @return Position|null
* @return Tile|null
*/
protected function getSafeNeighbourPosition($row, $column) {
if ($this->fitsIntoLowerBound($row, $column)) {
return new Position($row, $column);
return new Tile($row, $column);
}
return null;

@ -16,45 +16,45 @@ namespace Coffee;
class Spot {
/**
* @var Position[]
* @var Tile[]
*/
private $positions = [];
private $tiles = [];
/**
* Spot constructor.
*
* @param Position $position
* @param Tile $tile
*/
public function __construct(Position $position) {
$this->addPosition($position);
public function __construct(Tile $tile) {
$this->addTile($tile);
}
/**
* @param Position $position
* @param Tile $tile
* @return bool
*/
public function addPosition(Position $position) {
if (is_null($position)) {
public function addTile(Tile $tile) {
if (is_null($tile)) {
return false;
}
$this->positions[] = $position;
$this->tiles[] = $tile;
return true;
}
/**
* @return Position[]
* @return Tile[]
*/
public function getPositions() {
return $this->positions;
public function getTiles() {
return $this->tiles;
}
/**
* @return int
*/
public function getSize() {
return count($this->positions);
return count($this->tiles);
}
}

@ -62,14 +62,14 @@ class Table extends Map {
}
/**
* @param Position $searchedPosition
* @param Tile $searchedPosition
* @return int|string
*/
public function getSpotNumberByPosition(Position $searchedPosition) {
public function getSpotNumberByPosition(Tile $searchedPosition) {
// Linear search
// TODO: try to find a faster way
foreach ($this->getSpots() as $spotIndex => $spot) {
foreach ($spot->getPositions() as $position) {
foreach ($spot->getTiles() as $position) {
if ($searchedPosition->isTheSamePosition($position)) {
return $spotIndex;
}
@ -105,14 +105,14 @@ class Table extends Map {
}
/**
* @param Position $position
* @param Tile $position
*/
protected function updateCurrentSpot(Position $position) {
protected function updateCurrentSpot(Tile $position) {
if (is_null($this->currentSpot)) {
$this->currentSpot = new Spot($position);
}
else {
$this->currentSpot->addPosition($position);
$this->currentSpot->addTile($position);
}
}

@ -20,6 +20,7 @@ class Tile extends Position {
const REPRESENTS_SPOT = 1;
/**
* Representation on the map, that element does not exist on the given tile
* Default option
*/
const REPRESENTS_VOID = 0;
/**
@ -48,7 +49,7 @@ class Tile extends Position {
* @param $tileRepresentation
* @throws \Exception
*/
public function __construct($row, $column, $tileRepresentation) {
public function __construct($row, $column, $tileRepresentation = self::REPRESENTS_VOID) {
if (!$this->isRepresentingSpot() && !$this->isRepresentingVoid()) {
throw new \Exception('The map contains invalid representations');
}
@ -94,10 +95,10 @@ class Tile extends Position {
}
/**
* @return Position
* @return Tile
*/
public function getPosition() {
return new Position($this->getRow(), $this->getColumn());
return new Tile($this->getRow(), $this->getColumn());
}
/**

@ -26,7 +26,7 @@ class MapTest extends \PHPUnit_Framework_TestCase {
$row = 1;
$column = 1;
$position = new Position($row, $column);
$position = new Tile($row, $column);
$tile = new Tile($row, $column, Tile::REPRESENTS_SPOT);
$this->assertEquals($tile, $map->getTileByPosition($position));
@ -65,7 +65,7 @@ class MapTest extends \PHPUnit_Framework_TestCase {
];
$map = new Map($description);
$position = new Position(4, 4);
$position = new Tile(4, 4);
$tile = $map->getTileByPosition($position);
$neighbours = [

@ -8,126 +8,126 @@ require __DIR__ . '/../../vendor/autoload.php';
class PositionTest extends \PHPUnit_Framework_TestCase {
public function testColumnIndex() {
$position = new Position(2, 2);
$position = new Tile(2, 2);
$this->assertEquals(1, $position->getColumnIndex());
}
public function testRowIndex() {
$position = new Position(2, 2);
$position = new Tile(2, 2);
$this->assertEquals(1, $position->getRowIndex());
}
public function testTheSameColumn() {
$positionA = new Position(2, 2);
$positionB = new Position(2, 2);
$positionA = new Tile(2, 2);
$positionB = new Tile(2, 2);
$this->assertTrue($positionA->isTheSameColumn($positionB));
}
public function testTheSameRow() {
$positionA = new Position(2, 2);
$positionB = new Position(2, 2);
$positionA = new Tile(2, 2);
$positionB = new Tile(2, 2);
$this->assertTrue($positionA->isTheSameRow($positionB));
}
public function testTheSamePosition() {
$positionA = new Position(2, 2);
$positionB = new Position(2, 2);
$positionA = new Tile(2, 2);
$positionB = new Tile(2, 2);
$this->assertTrue($positionA->isTheSamePosition($positionB));
}
public function testNorthEastPosition() {
$position = new Position(2, 2);
$position = new Tile(2, 2);
$northEastPositionA = $position->getNorthEastPosition();
$northEastPositionB = new Position(1, 3);
$northEastPositionB = new Tile(1, 3);
$this->assertTrue($northEastPositionA->isTheSamePosition($northEastPositionB));
}
public function testEastPosition() {
$position = new Position(2, 2);
$position = new Tile(2, 2);
$northPositionA = $position->getEastPosition();
$northPositionB = new Position(2, 3);
$northPositionB = new Tile(2, 3);
$this->assertTrue($northPositionA->isTheSamePosition($northPositionB));
}
public function testSouthEastPosition() {
$position = new Position(2, 2);
$position = new Tile(2, 2);
$northPositionA = $position->getSouthEastPosition();
$northPositionB = new Position(3, 3);
$northPositionB = new Tile(3, 3);
$this->assertTrue($northPositionA->isTheSamePosition($northPositionB));
}
public function testSouthPosition() {
$position = new Position(2, 2);
$position = new Tile(2, 2);
$northPositionA = $position->getSouthPosition();
$northPositionB = new Position(3, 2);
$northPositionB = new Tile(3, 2);
$this->assertTrue($northPositionA->isTheSamePosition($northPositionB));
}
public function testsSouthWestPosition() {
$position = new Position(2, 2);
$position = new Tile(2, 2);
$northPositionA = $position->getSouthWestPosition();
$northPositionB = new Position(3, 1);
$northPositionB = new Tile(3, 1);
$this->assertTrue($northPositionA->isTheSamePosition($northPositionB));
}
public function testWestPosition() {
$position = new Position(2, 2);
$position = new Tile(2, 2);
$northPositionA = $position->getWestPosition();
$northPositionB = new Position(2, 1);
$northPositionB = new Tile(2, 1);
$this->assertTrue($northPositionA->isTheSamePosition($northPositionB));
}
public function testNorthWestPosition() {
$position = new Position(2, 2);
$position = new Tile(2, 2);
$northPositionA = $position->getNorthWestPosition();
$northPositionB = new Position(1, 1);
$northPositionB = new Tile(1, 1);
$this->assertTrue($northPositionA->isTheSamePosition($northPositionB));
}
public function testNorthPosition() {
$position = new Position(2, 2);
$position = new Tile(2, 2);
$northPositionA = $position->getNorthPosition();
$northPositionB = new Position(1, 2);
$northPositionB = new Tile(1, 2);
$this->assertTrue($northPositionA->isTheSamePosition($northPositionB));
}
public function testOpenSpaceNeighbours() {
$position = new Position(2, 2);
$position = new Tile(2, 2);
// Neighbours from NE to N, in CW direction
$neighbours = [
new Position(1, 3),
new Position(2, 3),
new Position(3, 3),
new Position(3, 2),
new Position(3, 1),
new Position(2, 1),
new Position(1, 1),
new Position(1, 2),
new Tile(1, 3),
new Tile(2, 3),
new Tile(3, 3),
new Tile(3, 2),
new Tile(3, 1),
new Tile(2, 1),
new Tile(1, 1),
new Tile(1, 2),
];
$this->assertEquals($neighbours, $position->getNeighbouringPositions());
}
public function testCornerNeighbours() {
$position = new Position(1, 1);
$position = new Tile(1, 1);
// Return just E, SE and S neighbours
$neighbours = [
new Position(1, 2),
new Position(2, 2),
new Position(2, 1),
new Tile(1, 2),
new Tile(2, 2),
new Tile(2, 1),
];
$this->assertEquals($neighbours, $position->getNeighbouringPositions());

@ -8,18 +8,18 @@ require __DIR__ . '/../../vendor/autoload.php';
class SpotTest extends \PHPUnit_Framework_TestCase {
public function testGetPositions() {
$position = new Position(2, 1);
$spot = new Spot($position);
$tile = new Tile(2, 1);
$spot = new Spot($tile);
$this->assertEquals([$position], $spot->getPositions());
$this->assertEquals([$tile], $spot->getTiles());
}
public function testSize() {
$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));
$spotA = new Spot(new Tile(1, 2));
$spotA->addTile(new Tile(1, 3));
$spotA->addTile(new Tile(2, 3));
$spotA->addTile(new Tile(3, 2));
$spotA->addTile(new Tile(4, 1));
$this->assertEquals(5, $spotA->getSize());
}

@ -57,8 +57,8 @@ class TableTest extends \PHPUnit_Framework_TestCase {
];
$table = new Table($description);
$spot = new Spot(new Position(1, 2));
$spot->addPosition(new Position(1, 3));
$spot = new Spot(new Tile(1, 2));
$spot->addTile(new Tile(1, 3));
$this->assertEquals([$spot], $table->getSpots());
}
@ -72,11 +72,11 @@ class TableTest extends \PHPUnit_Framework_TestCase {
];
$table = new Table($description);
$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));
$spot = new Spot(new Tile(1, 2));
$spot->addTile(new Tile(1, 3));
$spot->addTile(new Tile(2, 3));
$spot->addTile(new Tile(3, 2));
$spot->addTile(new Tile(4, 1));
$this->assertEquals([$spot], $table->getSpots());
}
@ -91,15 +91,15 @@ class TableTest extends \PHPUnit_Framework_TestCase {
$table = new Table($description);
$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));
$spotA = new Spot(new Tile(1, 2));
$spotA->addTile(new Tile(1, 3));
$spotA->addTile(new Tile(2, 3));
$spotA->addTile(new Tile(3, 2));
$spotA->addTile(new Tile(4, 1));
$spotB = new Spot(new Position(2, 5));
$spotB->addPosition(new Position(3, 5));
$spotB->addPosition(new Position(4, 4));
$spotB = new Spot(new Tile(2, 5));
$spotB->addTile(new Tile(3, 5));
$spotB->addTile(new Tile(4, 4));
$this->assertEquals([$spotA, $spotB], $table->getSpots());
}
@ -126,7 +126,7 @@ class TableTest extends \PHPUnit_Framework_TestCase {
];
$table = new Table($description);
$position = new Position(4, 1);
$position = new Tile(4, 1);
$this->assertEquals(2, $table->getSpotNumberByPosition($position));
}

@ -24,7 +24,7 @@ class TileTest extends \PHPUnit_Framework_TestCase {
$column = 2;
$tile = new Tile($row, $column, Tile::REPRESENTS_VOID);
$position = new Position($row, $column);
$position = new Tile($row, $column);
$this->assertEquals($position, $tile->getPosition());

Loading…
Cancel
Save