commit bbe64c9ff646cf4fdb403607ad16e52a126b8a97 Author: osmal Date: Mon Nov 20 00:18:02 2023 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1523fb7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +### Composer ### +/vendor diff --git a/LICENCE b/LICENCE new file mode 100644 index 0000000..1fc7112 --- /dev/null +++ b/LICENCE @@ -0,0 +1,19 @@ +Copyright (c) 2023 OSMAL + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..8c53c42 --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# PHP Maze Generator +A simple and minimalistic maze generator + +## Installation +```shell +composer require Osmal/Maze +``` + +## Usage +**Basic Usage:** +```php +require __DIR__ . '/vendor/autoload.php'; + +$width = 15; +$height = 15; +$maze = new Osmal\Maze\Generator($width,$height); + +$maze->print(); +``` + +**You can also make your own display logic:** +```php +echo '
';
+foreach ($maze->getMaze() as $row) {
+    foreach ($row as $cell) {
+        echo $cell ? '  ' : '  ';
+    }
+    echo '
'; +} +echo '
'; +``` \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..d8e50d2 --- /dev/null +++ b/composer.json @@ -0,0 +1,17 @@ +{ + "name": "osmal/maze", + "description": "A simple PHP maze generator", + "license": "MIT", + "autoload": { + "psr-4": { + "Osmal\\Maze\\": "src/" + } + }, + "authors": [ + { + "name": "osmal", + "email": "contact@osmal.dev" + } + ], + "require": {} +} diff --git a/src/Generator.php b/src/Generator.php new file mode 100644 index 0000000..dd6d208 --- /dev/null +++ b/src/Generator.php @@ -0,0 +1,63 @@ +width = $width; + $this->height = $height; + $this->maze = $this->generate(); + } + + public function getMaze() { + return $this->maze; + } + + private function generate() { + $maze = array_fill(0, $this->height * 2 + 1, array_fill(0, $this->width *2 + 1, 1)); + $this->carveMaze(1, 1, $maze); + + // Opening Start/End Walls + $maze[0][1] = 0; + $maze[$this->height *2][($this->width *2)-1] = 0; + return $maze; + } + + private function carveMaze($x, $y, &$maze) { + $maze[$y][$x] = 0; + $directions = [ + [0, 0], + [-2, 0], + [2, 0], + [0, -2], + [0, 2] + ]; + shuffle($directions); + + foreach ($directions as $dir) { + $nx = $x + $dir[0]; + $ny = $y + $dir[1]; + + if ($nx > 0 && $nx < count($maze[0]) && $ny > 0 && $ny < count($maze) && $maze[$ny][$nx] === 1) { + $mx = $x + $dir[0] / 2; + $my = $y + $dir[1] / 2; + $maze[$my][$mx] = 0; + $this->carveMaze($nx, $ny, $maze); + } + } + } + + public function print() { + foreach ($this->maze as $row) { + foreach ($row as $cell) { + echo $cell ? '⬛' : '⬜'; + } + echo PHP_EOL; + } + } +}