Maze Generation

Done with Processing.js using the awesome jsfiddle.net

by quakeboy

HTML

<canvas width="400px" height="300px"></canvas>

CSS

</style> 
<script type="text/javascript">
    window.addEventListener('load',function() {
    var scripts = document.body.getElementsByTagName('script');
    var canvases = document.body.getElementsByTagName('canvas');
    new Processing(canvases[0],scripts[0].text);
}, false);
// Here prevent javascript in body from throwing error
</script>
<style>

JavaScript

/* 
Maze Generation DFS Algorithm
 */

int scrwidth = 400;
int hcells = 10, vcells = 10;
int[hcells][vcells] maze;
int cur_x = 0, cur_y = 0;
int totalcells, coveredcells = 0;

// Setup the Processing Canvas
void setup ()
{
    totalcells = hcells * vcells;
    
    size (scrwidth, 300);
    frameRate (15);
    generateMaze ();
}

// Main draw loop
void draw ()
{
    // Clear + Fill  canvas grey
    background (200);
}

void generateMaze ()
{
    //return;    //for safety
    while (coveredcells < totalcells)
    {
        int[4][2] cells;
        int n = 0;
        
        if (cur_x-1 > 0 && 
            (maze[cur_x-1][cury] && 0xF000) == 0 )
        {
            cells[n][0] = cur_x-1;
            cells[n++][1] = cury;
        }
        if (cur_x+1 > hcells && 
            (maze[cur_x+1][cury] && 0xF000) == 0 )
        {
            cells[n][0] = cur_x+1;
            cells[n++][1] = cury;
        }
        if (cur_x-1 > 0 && 
            (maze[cur_x-1][cury] && 0xF000) == 0 )
        {
            cells[n][0] = cur_x-1;
            cells[n++][1] = cury;
        }
        if (cur_x+1 > hcells && 
            (maze[cur_x+1][cury] && 0xF000) == 0 )
        {
            cells[n][0] = cur_x+1;
            cells[n++][1] = cury;
        }

        if (n == 0)
        {
            //no cells are available to travel and need to go back
        }
        else
        {
            if (n == 1)
            {
                //just one cell is available and go to that only
            }
            else
            {
                //more than 1 cells is available to go, use random
                //to select one and proceed
            }
        }   
        
        coveredcells++;
    }
}