<?php

// maze generation, random dfs



// generate maze
function print2d($array) {
    foreach($array as $row) {
        foreach($row as $col) {
            echo $col . ' ';
        }
        echo "\n";
    }

    echo "\n\n";
}


function isGoodPath($maze, $point) {
    if (!isset($maze[$point[0]][$point[1]]) )
        return false;

    $dirs = [
        [0,-1],
        [1,0],
        [0,1],
        [-1,0],
    ];

    $countPath = 0;
    foreach($dirs as $dir) {
        $newPoint = [$point[0] + $dir[0], $point[1] + $dir[1]];
        if (
            isset($maze[$newPoint[0]][$newPoint[1]]) &&
            $maze[$newPoint[0]][$newPoint[1]] == '.'
        ) {
            $countPath++;
        }

    }

    if ($countPath > 1)
        return false;

    return true;

}

function generateMaze(&$maze, $point) {
    $dirs = [
        [0,-1],
        [1,0],
        [0,1],
        [-1,0],
    ];

    shuffle($dirs);

    foreach($dirs as $dir) {
        $newPoint = [$point[0] + $dir[0], $point[1] + $dir[1]];

        if (isGoodPath($maze, $newPoint)) {
            $maze[$newPoint[0]][$newPoint[1]] = '.';
            generateMaze($maze, $newPoint);
        }
    }

    return $maze;
}


$size = 25;
$maze = array_fill(0, $size, array_fill(0, $size, '#'));

print2d(generateMaze($maze, [0, 0]));