Rework Tile constructor

master
Peter Babič 8 years ago
parent fc82f617e9
commit 760d641bae
Signed by: peter.babic
GPG Key ID: 4BB075BC1884BA40
  1. 19
      index.php
  2. 14
      src/Coffee/Map.php
  3. 24
      src/Coffee/Spot.php
  4. 16
      src/Coffee/Tile.php
  5. 16
      tests/Coffee/MapTest.php
  6. 3
      tests/Coffee/SpotTest.php
  7. 6
      tests/Coffee/TableTest.php

@ -12,18 +12,31 @@ try {
// [0, 0, 1, 1]
// ]);
//
//
// $table = new Table();
//
// foreach ($map->getUnVisitedTiles() as $currentTile) {
// // Foreach cannot be used, we need re-evaluation
//// while (list(, $currentTile) = each($map->getUnVisitedTiles())) {
//// while (($currentTile = $map->getUnvisitedTile()) == true) {
// while (isset($map->getUnVisitedTiles()[0]) && ($currentTile = $map->getUnVisitedTiles()[0]) == true) {
// $currentTile->visit();
//
// if ($currentTile->isRepresentingSpot()) {
// foreach ($currentTile->getNeighbouringPositions() as $neighbouringPosition) {
// $neighbouringTile = $map->getTileByPosition($neighbouringPosition);
// $spot = new Spot($currentTile->getPosition());
//
// foreach ($map->getNeighboursOfTile($currentTile) as $neighbouringTile) {
// $neighbouringTile->visit();
//
// if ($neighbouringTile->isRepresentingSpot()) {
// $spot->addPosition($neighbouringTile->getPosition());
// }
// }
//
// $table->addSpot($spot);
// }
// }
//
// var_dump($table);
}
catch (\Exception $e) {
echo 'Caught exception: ' . $e->getMessage() . "\n";

@ -83,6 +83,16 @@ class Map {
return $array;
}
public function getUnvisitedTile() {
foreach ($this->getTiles() as $tile) {
if (!$tile->isVisited()) {
return $tile;
}
}
return false;
}
/**
* @param Position $position
* @return Tile|null
@ -110,6 +120,10 @@ class Map {
return $array;
}
/**
* @param Tile $tile
* @return Tile[]
*/
public function getNeighboursOfTile(Tile $tile) {
$neighbouringTiles = [];
foreach ($tile->getNeighbouringPositions() as $neighbouringPosition) {

@ -25,22 +25,14 @@ class Spot {
*/
private $positions = [];
// /**
// * Spot constructor.
// *
// * @param Position|Position[] $positions
// */
// public function __construct($positions = null) {
// if ($positions instanceof Position) {
// $this->addPosition($positions);
// }
//
// if (is_array($positions)) {
// foreach ($positions as $position) {
// $this->addPosition($position);
// }
// }
// }
/**
* Spot constructor.
*
* @param Position $position
*/
public function __construct(Position $position) {
$this->positions[] = $position;
}
/**
* @param Position $position

@ -23,9 +23,9 @@ class Tile extends Position {
*/
const REPRESENTS_VOID = 0;
/**
* @var bool
* @var integer
*/
private $representsElement = false;
private $representation;
/**
* Tiles are inherently unvisited
@ -49,7 +49,7 @@ class Tile extends Position {
parent::__construct($row, $column);
$this->representsElement = $tileRepresentation;
$this->representation = $tileRepresentation;
}
/**
@ -61,23 +61,29 @@ class Tile extends Position {
/**
* Flags this Tile as "visited"
*
* @return bool
*/
public function visit() {
if ($this->isVisited())
return false;
$this->visited = true;
return true;
}
/**
* @return boolean
*/
public function isRepresentingSpot() {
return $this->representsElement == self::REPRESENTS_SPOT;
return $this->representation == self::REPRESENTS_SPOT;
}
/**
* @return bool
*/
public function isRepresentingVoid() {
return $this->representsElement == self::REPRESENTS_VOID;
return $this->representation == self::REPRESENTS_VOID;
}
/**

@ -15,6 +15,22 @@ class MapTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals([$tileA, $tileB], $map->getTiles());
}
public function testUnvisitedTile() {
$description = [
[1, 0],
[0, 1],
];
$map = new Map($description);
$tiles = $map->getTiles();
$tiles[0]->visit();
$tiles[3]->visit();
$tile = new Tile(1, 2, Tile::REPRESENTS_VOID);
$this->assertEquals($tile, $map->getUnvisitedTile());
}
public function testUnVisitedTiles() {
$description = [
[1, 0],

@ -9,8 +9,7 @@ class SpotTest extends \PHPUnit_Framework_TestCase {
public function testGetPositions() {
$position = new Position(2, 1);
$spot = new Spot();
$spot->addPosition($position);
$spot = new Spot($position);
$this->assertEquals([$position], $spot->getPositions());
}

@ -10,9 +10,8 @@ class TableTest extends \PHPUnit_Framework_TestCase {
public function testGetSpots() {
$position = new Position(1, 1);
$spot = new Spot();
$spot = new Spot($position);
$table = new Table();
$spot->addPosition($position);
$table->addSpot($spot);
$this->assertEquals([$spot], $table->getSpots());
}
@ -20,9 +19,8 @@ class TableTest extends \PHPUnit_Framework_TestCase {
public function testSpotsCount() {
$position = new Position(1, 1);
$spot = new Spot();
$spot = new Spot($position);
$table = new Table();
$spot->addPosition($position);
$table->addSpot($spot);
$table->addSpot($spot);
$this->assertEquals(2, $table->getSpotsCount());

Loading…
Cancel
Save