Fractal Maze Test Proof of concept recursively sub dividing maze regions in a fractal like manner so they have a similar characteristic to the parent html head title Maze Fractal title style canvas border 1px solid black style head body canvas id mazeCanvas width 600 height 400 canvas script custom random that can be seeded for repeatability let random function let x 2000 return next function x x 16807 2147483647 return x 1 2147483646 return Math random reset seed x seed 0 random reset 2 test random number generator console log random next console log random next console log random next console log random next const canvas document getElementById mazeCanvas const ctx canvas getContext 2d Function to draw a rectangle function drawRect x y width height ctx fillRect x y width height Function to create a maze using recursive division algorithm function createMaze x y width height d if width 2 height 2 return if d 10 return random reset 3 Divide the maze either horizontally or vertically const horizontal random next 0 5 const wallX x horizontal 0 random next width 1 const wallY y horizontal Math floor random next height 1 0 const passageX wallX horizontal Math floor random next width 0 const passageY wallY horizontal 0 Math floor random next height Draw the dividing wall if horizontal drawRect x wallY width 1 else drawRect wallX y 1 height Create passages createMaze x y horizontal width wallX x 1 horizontal wallY y 1 height d 1 createMaze horizontal x passageX 1 horizontal passageY 1 y horizontal width x width passageX 1 horizontal y height passageY 1 height d 1 Clear the canvas ctx clearRect 0 0 canvas width canvas height Set background color ctx fillStyle white ctx fillRect 0 0 canvas width canvas height Set wall color ctx fillStyle black Create the maze starting from the top left corner createMaze 0 0 canvas width canvas height 0 script body html
orizontally or vertically const horizontal random next 0 5 const wallX x horizontal 0 random next width 1 const wallY y horizontal Math floor random next height 1 0 const passageX wallX horizontal Math floor random next width 0 const passageY wallY horizontal 0 Math floor random next height Draw the dividing wall if horizontal drawRect x wallY width 1 else drawRect wallX y 1 height Create passages createMaze x y horizontal width wallX x 1 horizontal wallY y 1 height d 1 createMaze horizontal x passageX 1 horizontal passageY 1 y horizontal width x width passageX 1 horizontal y height passageY 1 height d 1 Clear the canvas ctx clearRect 0 0 canvas width canvas height Set background color ctx fillStyle white ctx fillRect 0 0 canvas width canvas height Set wall color ctx fillStyle black Create the maze starting from the top left corner createMaze 0 0 canvas width canvas height 0 script body html