Skip to content

Instantly share code, notes, and snippets.

@AlephAlpha
Last active June 22, 2016 00:01
Show Gist options
  • Save AlephAlpha/7245182 to your computer and use it in GitHub Desktop.
Save AlephAlpha/7245182 to your computer and use it in GitHub Desktop.
Maze generator in Mathematica.
MazeGraphics[m_, n_] :=
Block[{$RecursionLimit = Infinity,
unvisited = Tuples[Range /@ {m, n}], maze},
maze = Graphics[{Line[{{#, # - {0, 1}}, {#, # - {1, 0}}}] & /@
unvisited,
Line[{{0, n}, {0, 0}, {m, 0}}]}]; {unvisited =
DeleteCases[unvisited, #];
Do[If[MemberQ[unvisited, neighbor],
maze = DeleteCases[
maze, {#,
neighbor - {1, 1}} | {neighbor, # - {1, 1}}, {5}]; #0@
neighbor], {neighbor,
RandomSample@{# + {0, 1}, # - {0, 1}, # + {1, 0}, # - {1,
0}}}]} &@RandomChoice@unvisited; maze];
maze = MazeGraphics[21, 13]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment