Skip to content

Generating with Wilson's Path Carver

Alex Lopez edited this page Oct 20, 2023 · 2 revisions

wilson-loop

The wilson algorithms are my favorite. They are a much more visually interesting and fun to implement approach at producing perfect mazes. This algorithm, as I have implemented it goes as follows.

pick a random square and make it a path that is part of a maze

pick a random WALK point for a random walk

creat a cell called PREVIOUS that starts as nil.

while we have selected a starting square for a random walk

	for each neighbor NEXT in random order

		if the NEXT != PREVIOUS and is in bounds

			select NEXT for consideration

			if NEXT is part of our own walk

				erase the loop we have formed using backtracking

			else if NEXT is part of the maze

				join our walk to the maze using backtracking to carve a path.

			else

				mark NEXT with the direction it needs for backtracking

				PREVIOUS = WALK

				WALK = NEXT

		continue outer while loop

Watching this is one live is very fun and frustrating. Sometimes, you wish the flailing walk path would just find the square sitting out there in the grid but it won't. The starting maze point is a needle in a haystack and we eventually find it but it can just take some time. These are very well balanced mazes with interesting twists and turns and unexpected long paths that can sometimes weave through the maze.