Randomized Maze Generator using Prim's Algorithm

Was fooling around today and decided to try this out, looks pretty cool as it builds the maze. Tips and tricks to improve my code design are very welcome!

#pragma rtGlobals=3     // Use modern global access method and strict wave access.
//PRIM's ALGORITHM: RANDOMIZED MAZE GENERATOR
//coded by Ian Allen, 13, June, 2013


function maze_maker(dim_x,dim_y)
variable dim_x, dim_y // maze dimensions, recommended to use odd values but even does work

//initiate the maze
make/o/d/n=(dim_x,dim_y) maze = 1
make/o/d/n=(dim_x,dim_y) cell_logic = 0
make/o/d/n=(1,4) wall_list = 0 // [x_coord,y_coord,delta_x,delta_y] delta is where to check for the adjacent cell
variable/g num_walls = 0

variable i
for(i=0;i<dim_x;i+=2)
    maze[i][] = 0
    maze[][i] = 0
endfor
//newimage cell_logic
newimage maze
dowindow/c/w=graph0 maze_win


//pick a seed cell to begin process
variable x_coord = floor((enoise(.4999) + .5)*((dim_x - 2)/2)) * 2 + 1
variable y_coord = floor((enoise(.4999) + .5)*((dim_y - 2)/2)) * 2 + 1
variable wall_num
print x_coord,y_coord
cell_logic[x_coord][y_coord] = 1
//add the walls to the list
maze_maker_add_walls(x_coord,y_coord,dim_x,dim_y)
    do
        //roll for a wall and check it
        wall_num = floor((enoise(.4999) + .5)*num_walls)
        maze_maker_check_wall(wall_num,dim_x,dim_y)
       
                                   
    while (num_walls > 0)              
end

//---subroutine 1----//

function maze_maker_add_walls(x_coord,y_coord, dim_x,dim_y)
variable x_coord,y_coord, dim_x,dim_y //x y coord are cell coordinates not wall coordinates
nvar num_walls
wave wall_list,maze
//add the walls to the list if they exist and are not edge walls
if ( x_coord -1 != 0 && maze[x_coord-1][y_coord] == 0)  // LEFT wall
    insertpoints 0,1, wall_list
    wall_list[0][0] =  x_coord - 1 
    wall_list[0][1] = y_coord  
    wall_list[0][2] = -1   
    wall_list[0][3] = 0
    num_walls +=1
endif
if ( x_coord + 1 != (dim_x -1) && maze[x_coord+1][y_coord] == 0) // RIGHT wall
    insertpoints 0,1, wall_list
    wall_list[0][0] =  x_coord + 1
    wall_list[0][1] = y_coord
    wall_list[0][2] = 1
    wall_list[0][3] = 0
    num_walls +=1
endif
if ( y_coord - 1 != 0 && maze[x_coord][y_coord -1] == 0) // TOP wall
    insertpoints 0,1, wall_list
    wall_list[0][0] =  x_coord
    wall_list[0][1] = y_coord -1
    wall_list[0][2] = 0
    wall_list[0][3] = -1
    num_walls +=1
endif
if ( y_coord + 1 != (dim_y -1) && maze[x_coord][y_coord +1] ==0) // BOTTOM wall
    insertpoints 0,1, wall_list
    wall_list[0][0] =  x_coord
    wall_list[0][1] = y_coord + 1
    wall_list[0][2] = 0
    wall_list[0][3] = 1
    num_walls +=1
endif
print num_walls
end

//---subroutine2---//

function maze_maker_check_wall(wall_num,dim_x,dim_y)
variable wall_num,dim_x,dim_y
nvar num_walls
wave wall_list,cell_logic,maze
variable x_coord = wall_list[wall_num][0] // wall coordinates
variable y_coord = wall_list[wall_num][1]
variable new_x = x_coord + wall_list[wall_num][2]  // adjacent room coordinates
variable new_y = y_coord + wall_list[wall_num][3]

if (cell_logic[new_x][new_y] == 0)
    maze[x_coord][y_coord] = 1
    //insert update command here when you figure it out
    doupdate/w=maze_win
    cell_logic[new_x][new_y] = 1
    deletepoints (wall_num),1,wall_list
    num_walls -= 1
    maze_maker_add_walls(new_x,new_y, dim_x,dim_y)
else
    deletepoints (wall_num),1,wall_list
    num_walls -=1
endif



end
Try:
maze_maker(10,20)

and you will discover a problem on the Right wall (rtGlobals=3).

Forum

Support

Gallery

Igor Pro 8

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More