<?php
error_reporting
(E_ALL);
set_time_limit(360);

if (!empty(
$_GET['x']) && !empty($_GET['y']))
{
    
$map createMap((int) $_GET['x'], (int) $_GET['y'], 'wideAreasNextPos');
    
printMap($map);
}

if (!empty(
$_GET['createRandom']))
{
    for(
$i 0$i $_GET['createRandom']; $i++)
    {
        
$map createMap(rand(20100), rand(20100), 'wideAreasNextPos');
        
printMap($map);
    }
}

function 
createMap($sizex$sizey$positionCallback)
{
    
$map = array();

    for(
$y 0$y $sizey$y++)
    {
        
$map[$y] = array();

        for (
$x 0$x $sizex$x++)
        {
            
$map[$y][$x] = '#';
        }
    }

    
// place random start and end
    
$startX rand(1$sizex-1);
    
$startY rand(1$sizey-1);
    
$endY rand(1$sizey-1);

    if (
$endY == $startY)
    {
        while((
$endX rand(1$sizex-1)) != $startX);
    }
    else
    {
        
$endX rand(1$sizex-1);
    }

    
$map[$startY][$startX] = '.';
    
$map[$endY][$endX] = 'X';

    
// create a random route from start to end
    
$posX $startX;
    
$posY $startY;

    while((
$endX != $posX) || ($endY != $posY))
    {
        
$positionCallback($posY$posX$map);

        if(
$map[$posY][$posX] == '#')
        {
            
$map[$posY][$posX] = ' ';
        }
    }

    return 
$map;
}

function 
wideAreasNextPos(&$posY, &$posX, &$map)
{
    
$way rand(05);
    
$sizex count($map[0]);
    
$sizey count($map);

    switch(
$way)
    {
        case 
1:
            if (
$posX < ($sizex-2))
            {
                
$posX++;
            }
            break;
        case 
2:
            if (
$posX 1)
            {
                
$posX--;
            }
            break;
        case 
3:
            if (
$posY < ($sizey-2))
            {
                
$posY++;
            }
            break;
        case 
4:
            if (
$posY 1)
            {
                
$posY--;
            }
            break;
    }
}

function 
wideAreasNextPosNotSpace(&$posY, &$posX, &$map)
{
    
$way rand(05);
    
$sizex count($map[0]);
    
$sizey count($map);

    switch(
$way)
    {
        case 
1:
            if (
$posX < ($sizex-2))
            {
                if(
$map[$posY][$posX+1] != ' ')
                {
                    
$posX++;
                }
            }
            break;
        case 
2:
            if (
$posX 1)
            {
                if(
$map[$posY][$posX-1] != ' ')
                {
                    
$posX--;
                }
            }
            break;
        case 
3:
            if (
$posY < ($sizey-2))
            {
                if(
$map[$posY+1][$posX] != ' ')
                {
                    
$posY++;
                }
            }
            break;
        case 
4:
            if (
$posY 1)
            {
                if(
$map[$posY-1][$posX] != ' ')
                {
                    
$posY--;
                }
            }
            break;
    }
}

function 
printMap(&$map)
{
    print(
"<pre>");

    foreach(
$map as $line)
    {
        foreach(
$line as $char)
        {
            print(
$char);
        }

        print(
"\n");
    }

    print(
"</pre>");
}
?>

<form method='get'>
    x: <input type='text' name='x' value='50' /><br />
    y: <input type='text' name='y' value='50' /><br />
    <input type='submit' name='gen' value='Create labyrinth' />
</form>