F# ThreeJs Maze

Three.js | Fable | F#

Another little maze generator, this time in Three.js and F#. Press one of the buttons below 👇 to generate some mazes...

Recursive subdivision

The code is using the Recursive subdivision algorithm to generate the maze. The big limitation with this approach is that it is easy to see the links between sections, which makes the maze easy to solve once these are spotted. Compared to my other maze generator which uses the "Sidewinder" algorithm, this one is more obviously composed of blocks, but it doesn't suffer from having a long corridor along the top of the maze.

The maze walls are rendered as a load of cubes (or no cubes, for the passages) using Three.js. This gives the maze some 3D depth, and also makes it easy to do the zoom in effect for a new maze by moving the camera out.

Literal Utilities

The maze logic is written in F#. I won't turn this into a tutorial on F#, but to illustrate how nice the syntax is, it is possible to very neatly define some utility functions that help with rearranging maze blocks to come up with more possible blocks. E.g. To rotate a piece of maze, the code just needs to 'spin' the parts round, and we can write this almost literally in F#:


// Rotate a large square clockwise by 90 degrees
let rotate a b c d e
           f g h i j
           k l m n o
           p q r s t
           u v w x y = ls u p k f a
                          v q l g b
                          w r m h c
                          x s n i d
                          y t o j e
    

Similarly, we can write a literal definition of what it means to flip a maze square horizontally (btw ls is just a utility function here to create a "large square" of maze - not the linux dir equivalent!)


// Flip a large square horizontally
let flip a b c d e
         f g h i j
         k l m n o
         p q r s t
         u v w x y = ls e d c b a
                        j i h g f
                        o n m l k
                        t s r q p
                        y x w v u
    

Going deeper into the maze

If you want to know more about how to recursively generate a maze in F# then I wrote a much more detailed article on my old blog here (sadly the styling no longer works)