commit
bbe64c9ff6
|
@ -0,0 +1,2 @@
|
|||
### Composer ###
|
||||
/vendor
|
|
@ -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.
|
|
@ -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 '<pre>';
|
||||
foreach ($maze->getMaze() as $row) {
|
||||
foreach ($row as $cell) {
|
||||
echo $cell ? '<span style="background-color: #000;"> </span>' : '<span style="background-color: #fff;"> </span>';
|
||||
}
|
||||
echo '<br>';
|
||||
}
|
||||
echo '</pre>';
|
||||
```
|
|
@ -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": {}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
namespace Osmal\Maze;
|
||||
|
||||
class Generator
|
||||
{
|
||||
private $maze;
|
||||
private $width;
|
||||
private $height;
|
||||
|
||||
public function __construct($width, $height) {
|
||||
$this->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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue