![](https://s3images.coroflot.com/user_files/individual_files/598922_ggs0oig9gw_asclwslfnjufww.png)
The algorithm consists of 3 parts, a function and 2 scripts and was split for convenience in writing and troubleshooting. The generator works by creating a matrix of 0's, picking a location at random and turning the 0 into a 1, then creating branches of 1's outwards in random directions until no further branches can be made.
![](https://s3images.coroflot.com/user_files/individual_files/598922_qb9h_bo3wb33wuztfprhzua4f.png)
This script is the first one called in the function and is simply used to move the active coordinate 2 units in a random direction. By moving 2 units, a grid of coordinates is rendered incapable of turning into 1's. These coordinates become the maze walls.
![](https://s3images.coroflot.com/user_files/individual_files/598922_mvu0tcpx8mdtat9yfbfvu0vys.png)
This is the second script called in the function. It detects if the new active coordinate has already been activated (if it is a 1), and if so moves back along the path the active coordinate followed until an unactivated coordinate is available. When this condition is met, it activates the coordinate (changes a 0 to a 1) as well as the coordinate in between both the current and the last active coordinates.
![](https://s3images.coroflot.com/user_files/individual_files/598922_siaaicxqdsnpcoc7azewahaor.png)
The is the command window of MATLAB. From here, the function is called using an x and y dimension for the maze. This dimension is the size of the maze without walls. The matrix it generates is (2x-1) in size. The 10x10 maze generated here uses a 19x19 matrix.
![](https://s3images.coroflot.com/user_files/individual_files/598922_rhocbdyv5dxuuod9olqjmbf06.png)
When the maze can grow no farther, the generator stops. The matrix can then be copy pasted into excel.
![](https://s3images.coroflot.com/user_files/individual_files/598922_jqulm8mdwhbkuaysncjariiuz.png)
Using an Excel macro I wrote, each cell is filled with either black for 0 or white for 1.
![](https://s3images.coroflot.com/user_files/individual_files/598922_neec_5d8gb0jnwitrbqdgvgkw.png)
This is the matrix after being filled. The matrix is then given a border, the text removed, and the maze printed.
![](https://s3images.coroflot.com/user_files/individual_files/598922_b6gq59dvaoqcpp5ezsdtdsgma.png)
The maze at the time of printing. (Note the maze has been rotated 90deg clockwise)
![](https://s3images.coroflot.com/user_files/individual_files/598922_x0ukcxl2vqalrdwpefdxxvki6.png)
A second finished maze.
![](https://s3images.coroflot.com/user_files/individual_files/598922_ndp8bkzrrrfctw0wd3qp_b2x7.png)
A third finished maze.
gLike