econrad003 / yaMazeImp

Yet another maze implementation
GNU General Public License v3.0
1 stars 0 forks source link

yaMazeImp

"Yet another maze implementation" by Eric Conrad.

Overview

1 Description

This is a collection of maze algorithms and scripts that were implemented primarily in Python 3. Most of these programs are based on suggestions and ideas found in an wonderful little book by Jamis Buck called Mazes for Programmers. See the references at the end for more information. (My recommendation: buy this book. In addition to learning about mazes, you will pick up some graph theory and ruby language programming.) I've made some departures from Buck's approaches, so my programs are not simply translations from ruby into python.

2 Scripts

The scripts entab.py and detab.py are respectively a script to replace spaces by tabs and a script to replace tabs by spaces. They have nothing to do with mazes. I use them because the text editor that I use (Gnome's TextEditor) cannot be configured to use spaces for Python and tabs for some other language. The space/tab configuration is all or nothing. Nor can it be configured correctly to do entabbing or detabbing.

2.1 Recent changes

The list of older changes, going back to 14 July 2020, has been moved to file CHANGELOG.md. A short list of recent changes will continue to appear here before being archived in CHANGELOG.md.

29 September 2020

Directed mazes -- Four directed maze generation algorithms have been implemented. These are binary tree (first version), sidewinder, recursive backtracker, and Aldous/Broder (both first entrance and last exit). A layout new program using matplotlib supports directed rectangular mazes. See the demonstration script directed_maze_demo.py for some examples and some details.

The python implementations of the directed maze generation algorithms are described briefly at the end of Section 3.1 below. They are: di_aldous_broder.py, di_binary_tree.py, di_recursive_backtracker.py, and di_sidewinder.py.

This closes issue #12 (Directed mazes).

10 September 2020

Inform 7 code -- The first steps in generating Inform 7 code from a maze on a 3-dimensional grid are complete. This is done in two steps. First, the maze is "saved" as an editable INI configuration file which contains grid and maze information. Then the configuration file is used to generate Inform 7 statements. Section 5.2 of the README.md file has more information.

At the moment the implementations in grid3d.py (to generate the INI file) and inform7.py (to generate the Inform 7 code) are both crude and preliminary, but they should at least be functional.

The Floyd-Warshall algorithm -- The Floyd-Warshall algorithm (known by a number of names, including, more simply, Floyd's algorithm) is an algorithm for finding all minimum weight paths in a directed graph, or, in our case, in a directed maze. (At the moment, we don't have directed maze generation algorithms, though several implemented algorithms, including Binary Tree (au Jamis Buck), Sidewinder, Aldous-Broder and Recursive Backtracker (DFS) can easily be adapted for that purpose, or so I hope.) It produces a state matrix with running time that is cubic in the number of vertices. It is implemented in floyds.py and there is a simple demonstration in floyds_demo.py.

It has a number of uses, including detecting negative weight cycles (in linear time), finding minimum weight paths (in linear time), and checking reachability (in linear time), and determining whether one given vertex is reachable from another given vertex (in constant time).

N.B. The adaptations of the four above-mentioned maze generation algorithms will (I hope!) be included in the next update. The demonstration program floyds_demo.py will probably be extended to include one or more of these algorithm implementations.

3 Algorithms

3.1 Maze Generation

In the descriptions, the terms spanning tree and perfect maze are used interchangeably. They do really mean exactly the same thing. They only differ in the underlying context.

3.2 Other Algorithms

The demonstration script konigsberg_demo.py produces a maze based on the Königsberg bridges problem (Leonhard Euler, 1736). It uses the recursive backtracker algorithm followed by one pass of simple braiding to insure that bridges are passable. Cell removal and cell coloring are governed by a template file input/königsberg.txt and sample output is in demos/königsberg.png. For background, see references [2] through [5].

4 Grids and cells

The programs grid.py and cell.py describe the basic grid and cell classes that underly all mazes and the grids that they span.

The function of the various cell programs should be mostly self-explanatory. Much of the actual work is done in the grid programs and in the layout programs. ASCII and unicode layout are handled in rectangular_grid.py.

5 Layouts

5.1 Graphical layouts

Several types of layouts are supported. ASCII and unicode layouts are supported on rectangular grids and on some grids derived from rectangular grids (for example, cylinder grids). All grids support GraphViz/dot layout, though the results usually leave a lot to be desired.

Most derived grids support plot layouts using MatPlotLib, though in most cases, support will require some direct use of MatPlotLib methods. See the scripts for examples.

GRAPHVIZ
MATPLOTLIB

These typically require a substantial amount of tweaking.

5.2 Inform 7 code generation (EXPERIMENTAL)

Inform 7 is a declarative language used primarily to produce interactive fiction (aka text adventures). A classic example of interactive fiction is the game Cave (aka Adventure) from the early 1980s. Modern versions of Cave can be found on the Interactive Fiction Archive at https://ifarchive.org under the names Adventure and Colossal Cave. Cave also inspired a commercial game called Zork. Information about Inform 7 can be found on the Inform 7 web site at http://inform7.com/.

Inform 7 code can be generated in two or three steps from a supported grid to represent a maze in Inform. Supported grids are 3-D grids (defined in *grid3d.py) and their subclasses. (Rectangular grids and weave grids may be backfitted with support sometime in the future.) The required steps are:

  1. Generate an INI configuration file to represent the maze. (The maze can be reconstructed using the INI file, so this is a SAVE/LOAD representation of the maze.)
  2. (Optional.) Edit the configuration file to provide additional information.
  3. Use the INI file to generate Inform 7 code using the Inform7 class in inform7.py.

For help generating the INI file or with generating the maze from the INI file, see the examples in inform7_demo.py.

The generator handles both one-way and two-way links. Typical generated code looks something like this:

        [definitions needed for one-way links]
    The verb to be eastward from means the reversed mapping east relation.

    Cell1 is a room.  The description is "There are exits east and northeast.".
        [defining a one-way link]
    Cell2 is a room.  It is a room eastward from Cell1.
      The description is "There is an exit north.".
        [defining two two-way links]
    Cell3 is a room.  It is northeast from Cell1.  It is north from Cell2.
      The description is "There are exits south and southwest."

References

  1. Buck (2015) - Jamis Buck. Mazes for Programmers. Pragmatic Bookshelf, 2015. ISBN-13 978-1-68050-055-4.

  2. Leonhard Euler (1736). "Solutio problematis ad geometriam situs pertinentis". Comment Acad Sci U Petrop 8, 128–40.

  3. Carl Hierholzer (1873), "Ueber die Möglichkeit, einen Linienzug ohne Wiederholung und ohne Unterbrechung zu umfahren", Mathematische Annalen, 6 (1): 30–32, doi:10.1007/BF01442866.

  4. "Eulerian path". Wikipedia, 5 Aug. 2020. Web, accessed 8 Aug. 2020. Wikipedia: https://en.wikipedia.org/wiki/Eulerian_path

  5. "Seven bridges of Königsberg". Wikipedia, 6 Jun. 2020. Web, accessed 8 Aug. 2020. Wikipedia: https://en.wikipedia.org/wiki/Seven_Bridges_of_K%C3%B6nigsberg

  6. Yiping Hu, Russell Lyons and Pengfei Tang. "A reverse Aldous/Broder algorithm". Preprint. Web: arXiv.org. 24 Jul 2019. Arxiv.org: http://arxiv.org/abs/1907.10196v1

  7. Bernard Chazelle. "A minimum spanning tree algorithm with inverse-Ackermann type complexity". J ACM 47 (2000), 1028-1047. Preprint: https://www.cs.princeton.edu/~chazelle/pubs/mst.pdf

  8. "Borůvka's algorithm". Wikipedia. 10 Jun. 2020. Web, accessed 12 Aug. 2020. Wikipedia: https://en.wikipedia.org/wiki/Bor%C5%AFvka%27s_algorithm