Decouple Map, Position and Tile even more

master
Peter Babič 8 years ago
parent af3b3b7633
commit be1bb7ba9b
Signed by: peter.babic
GPG Key ID: 4BB075BC1884BA40
  1. 12
      index.php
  2. 101
      src/Coffee/Map.php
  3. 13
      src/Coffee/Position.php
  4. 22
      src/Coffee/Tile.php
  5. 2
      tests/Coffee/MapTest.php
  6. 1
      tests/Coffee/TileTest.php

@ -12,17 +12,7 @@ try {
[0, 0, 0, 1]
]);
$table = new Table();
foreach ($map->describedByArray() as $mapRowIndex => $mapRow) {
foreach ($mapRow as $mapColumnIndex => $containsCoffee) {
if ($containsCoffee == true) {
$position = new Position($mapColumnIndex, $mapRowIndex);
$spot = new Spot($position);
$table->addSpot($spot);
}
}
}
// $table = new Table();
var_dump($map->describedByArray());
}

@ -2,8 +2,6 @@
namespace Coffee;
use Exception;
/**
* Class Map
*
@ -11,20 +9,18 @@ use Exception;
*/
class Map {
/**
* @var array
* @var Tile[]
*/
private $description = [];
private $unVisitedTiles = [];
/**
* @var array
* @var Tile[]
*/
private $visited = [];
private $visitedTiles = [];
/**
* @var int
*/
private $height = 0;
/**
* @var int
*/
@ -33,34 +29,74 @@ class Map {
/**
* Map constructor.
*
* @param $description
* @throws Exception
* @param $description [][] The description must be 2D array containing at least one element
* @throws \Exception
*/
public function __construct($description) {
if (is_null($description) || !is_array($description) || !is_array($description[0])) {
throw new Exception('The Coffee Table map could not be loaded.');
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.');
}
$height = 0;
$width = 0;
// This data processing could be written more read-ably in multiple cycles,
// but at a cost of reduced performance (minor).
foreach ($description as $mapRowIndex => $mapRow) {
foreach ($mapRow as $mapColumnIndex => $mapTileRepresentation) {
// All tiles are inherently unvisited at first
$this->unVisitedTiles[] = new Tile($mapRowIndex, $mapColumnIndex, $mapTileRepresentation);
$width = $mapColumnIndex > $width ? $mapColumnIndex : $width;
}
$height = $mapRowIndex > $height ? $mapRowIndex : $height;
}
$this->width = $this->calculateMapWidth($description);
$this->height = $this->calculateMapHeight($description);
// We are in a business of one off
$this->height = $height + 1;
$this->width = $width + 1;
}
/**
* @return Tile[]
*/
public function getUnVisitedTiles() {
return $this->unVisitedTiles;
}
$this->description = $description;
/**
* @return Tile[]
*/
public function getVisitedTiles() {
return $this->visitedTiles;
}
/**
* @return array
*/
public function describedByArray() {
return $this->description;
// return $this->unVisited;
$array = [];
foreach ($this->getUnVisitedTiles() as $tile) {
$array[$tile->getRow()][$tile->getColumn()] = $tile->containsElement();
}
foreach ($this->getVisitedTiles() as $tile) {
$array[$tile->getRow()][$tile->getColumn()] = $tile->containsElement();
}
return $array;
}
/**
* @param Position $position
* @return bool
*/
public function visitPosition(Position $position) {
public function visitTile(Position $position) {
if ($this->isValidPosition($position)) {
$this->visited[$position->getRow()][$position->getColumn()] = true;
$this->visitedTiles[$position->getRow()][$position->getColumn()] = true;
return true;
}
@ -103,37 +139,12 @@ class Map {
* @return bool
*/
public function isVisitedPosition(Position $position) {
if (!isset($this->visited[$position->getRow()][$position->getColumn()])) {
if (!isset($this->visitedTiles[$position->getRow()][$position->getColumn()])) {
return false;
}
return ($this->visited[$position->getRow()][$position->getColumn()] == true);
}
/**
* @param $description
* @return int
*/
private function calculateMapWidth($description) {
$widestRow = 0;
foreach ($description as $row) {
// Count the level 2 array elements
$colWidth = count($row);
if ($colWidth > $widestRow) {
$widestRow = $colWidth;
}
}
return $widestRow;
return ($this->visitedTiles[$position->getRow()][$position->getColumn()] == true);
}
/**
* @param $description
* @return int
*/
private function calculateMapHeight($description) {
// Count the level 1 array elements
return count($description);
}
}

@ -21,8 +21,17 @@ class Position {
/**
* @param $row
* @param $column
* @throws \Exception
*/
function __construct($row, $column) {
// if ($row <= 0) {
// throw new \Exception('The row argument must be positive non-zero number.');
// }
//
// if ($column <= 0) {
// throw new \Exception('The columns argument must be positive non-zero number.');
// }
$this->column = $column;
$this->row = $row;
}
@ -77,14 +86,14 @@ class Position {
*/
public function getEastPosition() {
return new Position($this->getRow(), $this->getColumn() + 1);
}
}
/**
* @return Position
*/
public function getSouthEastPosition() {
return new Position($this->getRow() + 1, $this->getColumn() + 1);
}
}
/**
* @return Position

@ -8,6 +8,15 @@ namespace Coffee;
* @package Coffee
*/
class Tile extends Position {
/**
* Representation on the map, that element exists on the given tile
*/
const CONTAINS_ELEMENT = 1;
/**
* Representation on the map, that element does not exist on the given tile
*/
const NOT_CONTAINS_ELEMENT = 0;
/**
* @var bool
*/
@ -18,19 +27,24 @@ class Tile extends Position {
*
* @param $row
* @param $column
* @param $containsElement
* @param $mapTileRepresentation
* @throws \Exception
*/
public function __construct($row, $column, $containsElement) {
public function __construct($row, $column, $mapTileRepresentation) {
if ($mapTileRepresentation != self::CONTAINS_ELEMENT && $mapTileRepresentation != self::NOT_CONTAINS_ELEMENT) {
throw new \Exception('The map contains invalid representations');
}
parent::__construct($row, $column);
$this->containsElement = $containsElement;
$this->containsElement = $mapTileRepresentation;
}
/**
* @return boolean
*/
public function containsElement() {
return $this->containsElement;
return $this->containsElement == self::CONTAINS_ELEMENT;
}
}

@ -91,7 +91,7 @@ class MapTest extends \PHPUnit_Framework_TestCase {
$map = new Map($description);
$position = new Position(0, 0);
$map->visitPosition($position);
$map->visitTile($position);
$this->assertTrue($map->isVisitedPosition($position));
}

@ -6,6 +6,7 @@ namespace Coffee;
require __DIR__ . '/../../vendor/autoload.php';
class TileTest extends \PHPUnit_Framework_TestCase {
public function testContainsElement() {
$tile = new Tile(2, 2, true);

Loading…
Cancel
Save