description = $description; $width = 0; foreach ($description as $rowIndex => $row) { foreach ($row as $columnIndex => $tileRepresentation) { // Convert indices to positions $tile = new Tile($rowIndex + 1, $columnIndex + 1, $tileRepresentation); $this->tiles[] = $tile; $this->structuredTiles[$rowIndex][$columnIndex] = $tile; // Find longest row $width = $columnIndex > $width ? $columnIndex : $width; } } // Convert indices to dimensions $this->width = $width + 1; $this->height = count($this->getDescription()); } /** * @return array */ public function getDescription() { return $this->description; } /** * @return Tile[] */ public function getTiles() { return $this->tiles; } /** * @return Tile[][] */ public function getStructuredTiles() { return $this->structuredTiles; } /** * @param Position $position * @return Tile|null */ public function getTileByPosition(Position $position) { if (isset($this->structuredTiles[$position->getRowIndex()][$position->getColumnIndex()])) { return $this->structuredTiles[$position->getRowIndex()][$position->getColumnIndex()]; } return null; } /** * @param Tile $tile * @return Tile[] */ public function getNeighboursOfTile(Tile $tile) { $neighbouringTiles = []; foreach ($tile->getNeighbouringPositions() as $neighbouringPosition) { $neighbouringTiles[] = $this->getTileByPosition($neighbouringPosition); } return array_values(array_filter($neighbouringTiles)); } /** * @return int */ public function getHeight() { return $this->height; } /** * @return int */ public function getWidth() { return $this->width; } }