My love of programming mazes started with the purchase of “Mazes for Programmers: code your own twisty little passages,” see A-mazing books. For almost two years I have often returned to mazes, programming just for fun.
This Colaboratory notebook cuts maze programming to its basics. To build the notebook, I have taken my current python maze packages and removed all code except for that required for this project. By minimizing the code, I can show it all in the notebook; I have no hidden tricks. My goal was to produce a maze puzzle, a perfect maze with only one solution.
To open this notebook click the following icon:
The easiest way to run the maze program is to select “Run all” from the “Runtime” menu. The program will generate and print to PDF a maze of the size specified on the Maze Parameters form. To download this PDF check the “downloadPDF” box on the “Output” form and select “Run the focused cell.”
For more detail Continue.
For an overview of my colabortory python notebooks take a look at Non Photo-Realistic Mona Lisa, Revisited. This previous post introduces the manner in which I have structured my notebooks, for this maze notebook, I have used a similar structure.
Regarding copyrights and license, The maze generation code originated in the Ruby programming language in the book “Mazes for Programmers: code your own twisty little passages” by Jamis Buck. Sami Salkosuo translated these into Python. I have added a new concept to the Distances class, that is distance off the solution path. Adapted the cell class to more directly support other cell shapes such as polar, hex, and 3D. Finally, I added the Cairo drawing routines to both the Cell and Grid classes.
Section 1: is a direct lift from Non Photo-Realistic Mona Lisa, Revisited. to install the Cairo drawing package
Section 2 defines the Cell class. The class describes the data and methods implementing each cell of the maze. This data includes the cell’s neighbors and which of the neighbors are connected by missing walls. When drawing the maze, fillCell is used to color the background, and drawCell constructs the walls. Note, since the maze is drawn top to bottom, west to east, only the east and south walls are considered by drawCell.
Section 3 defines the Distances class. Jamis Buck and Sami Salkosuo both defined distances as the distance from a cell to all other cells. I added defininitions for add and sub so that algebraic combinations such as distance from the solution path are possible.
Section 4 defines the Grid class including the data and methods for implementing each maze. My main additions are getDistancesFromPath which uses distance algebra to define distance off the path as the distance from start + distance from goal – path length, and the drawing routine drawGrid and draw Opening. Unlike other maze programs I have written this one produces a PDF document. First, it illustrates the unsolved maze and then on a second page provides the solution. Most of the drawing code is directly from my work with scalable vector graphics. To produce page filling pdf files, I used the Cairo scale and translate functions. These provided the transformation from my arbitrary SVG space to the fixed paper mapping of PDF.
Section 5 implements the Wilson algorithm for maze generation. For Jamis Buck’s description of this algorithm and his original Ruby code follow this link.
Finally the main routine imports the required python packages, reads the user input from a form, Generates a “Grid,” initializes it with “initWilsonMaze,” chooses random entry and exit columns with ” randint” and produces the PDF file with “drawGrid.” displayMaze reads the binary png image from the Cairo surface and displays it in the notebook.
The last two cells are provided to download the pdf to your computer and to download setup.log if problems occur.
If you haven’t launched the app yet, click the icon.
