Short maze on puz.com

short mazeshort maze

The image you see above was generated real time when you visited this page. To get a different maze, please refresh your browser by pressing the F5 key. The maze is created by two files: short_maze.htm and short_maze.php. The only important line in the htm file is the line:

    <img src="short_maze.php" alt="short maze"/>

The lines starting with the <?php and ending with the ?> are the content of the php file, and show the entire maze source code in PHP. Enjoy!

<?php
header('Content-type: image/png'); // Short PHP Maze (C) 2013 Russell Sasamori
$k=10;$w=$k*2+3;$r=10;$c=1;$a=array();
$im = @imagecreatetruecolor($w*$r,$w*$r) or die('error');
imagefilledrectangle($im,0,0,$w*$r,$w*$r,imagecolorallocate($im,255,255,255));
for($i=0;$i<$w*$w;$i++) $a[$i]=($i%$w%($w-1)==0||intval($i/$w)%($w-1)==0)?2:1;
$e=array(1,-$w,-1,$w);$a[$i=$w*2+2]=0;
while(1) {
    do {
        for($d2=0,$d=rand(0,3);$d2<4;$d2++,$d=($d+1)%4) 
            if($i%$w%2==0 && intval($i/$w)%2==0 && $a[$i]==0 && $a[$i+$e[$d]*2]==1) {
                $a[$i+=$e[$d]]=$a[$i+=$e[$d]]=0;$c++;break;
       }
    } while($d2!=4);
    if($c>=$k*$k) break;
    while($a[$i]!=0||$i%$w%2!=0||intval($i/$w)%2!=0||$a[$i]==0&&
            $a[$i+$e[0]*2]!=1&&$a[$i+$e[1]*2]!=1&&$a[$i+$e[2]*2]!=1&&$a[$i+$e[3]*2]!=1)
            $i=($i+1)%($w*$w);
}
$a[$w+2]=$a[$w*$w-$w-3]=0;
for($y=0;$y<$w;$y++)
    for($x=0;$x<$w;$x++)
        if($a[$y*$w+$x]==1) 
            imagefilledrectangle($im,$x*$r,$y*$r,($x+1)*$r-1,($y+1)*$r-1,
            imagecolorallocate($im,0,170,0));
imagepng($im);imagedestroy($im);
?>

 

Last modified 07/01/2013

Up Home