LD Smith
March 22, 2021

# Mazes - Knox Game Design, February 2021

http://www.knoxgamedesign.org/1568/mazes-knox-game-design-february-2021/
How to generate mazes using Prim’s and Kruskal’s algorithm. Example implementations in Unity with C#. Methods for solving mazes. Demonstration of maze generators available on the web.

March 22, 2021

## Transcript

2. ### Maze Algorithms • Graph Theory • Nodes connected by edges

• Algorithms for random generation • Prim's Algorithm • Kruskal's Algorithm • Typical rectangular maze each node has four edges (N, S, E, W)
3. ### Prim's Algorithm • Two approaches 1. Cells have distinct walls

that connect to other cells • Thin walls • Wall is an "edge" which connects two cells (nodes) • Have to keep track of cells, walls, and the connection relationships • Should work with triangles, hexagons? 2. Cells are opened or closed • Closed cells are the walls • Thick walls • Only cells (easier to implement)
4. ### First Approach (Prim's) • Setup • Create cells • Create

walls (edges between all cells) • Pick random cell and add it to the maze • Mark adjacent (neighbor) cells as frontier cells • Loop until no more frontier cells • Pick random frontier cell • Remove wall between chosen frontier and maze cell • Set adjacent cells as frontier cells (unless it is already in the maze) • Add chosen frontier cell to maze (no longer a frontier cell) • http://weblog.jamisbuck.org/2011/1/10/maze- generation-prim-s-algorithm
5. ### Second Approach (Prim's) • Create a grid of cells with

random number values (0 to 100) • Pick a starting cell to open • For all of the closed cells • Find the cell with lowest value with one open adjacent cell • Open that cell • Repeat • Stop when there are no more closed cells matching the open condition

7. ### Kruskal's Algorithm • Assign set to each cell • Loop

through all the walls at random • If the wall connects two cells of different sets • Remove the wall from the maze • Add the cells from one set to the other set • Otherwise, leave the wall in the maze and continue • Can implement sets as tree, lists, or have set IDs for each cell • http://weblog.jamisbuck.org/2011/1/3/ma ze-generation-kruskal-s-algorithm
8. ### Solving a maze • DFS (depth first search) vs BFS

(breadth first search) • Questions • Visit all nodes or just find the shortest distance between two points (entrance and exit) • Find all paths, shortest path, or any path (greedy) • P, NP, NP hard, NP complete • Find shortest path between two nodes • Dijkstra's algorithm • A* (A-star) search algorithm • Best-first search • Built into GameMaker

10. ### References • Mazes for Programmers (book) - Jamis Buck •

Maze generation with Unity - https://www.youtube.com/watch?v=ucWX34Vrel8 • https://levidsmith.com/games/kittys- adventure/ • Prim's algorithm - https://www.tutorialspoint.com/data_structures_algorithms/prims_spanning_tree_algorithm.htm • Dijkstra's algorithm -https://www.geeksforgeeks.org/dijkstras-shortest-path- algorithm-greedy-algo-7/