Metadata-Version: 2.4
Name: mazegen
Version: 0.0.1
Summary: A maze generator
Author: rox8, Amazing_esra
License-Expression: MIT
Project-URL: Homepage, https://codeberg.org/rox8/amazing
Project-URL: Issues, https://codeberg.org/rox8/amazing/issues
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Requires-Python: >=3.1
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

## Description

This directory carries a package to generate a maze. 

## Usage

The maze generator will return a list of list of Cell objects.

Example taken from __main__.py:


    from .maze import Maze

    def main() -> None:
        # pass parameters to Maze
        # width, height, entry, exit, perfect
        maze: Maze = Maze(20, 15, (1, 1), (20, 15), True)
        maze.initiate_grid()
        maze.draw_pattern()
        maze.dfs_carve()

        print(maze) # will print a maze object
        print(maze.grid) # will print the maze grid
        print(maze.grid[1][1]) # will print a cell object (1, 1)

    if __name__ == "__main__":
        main()

## Instructions

Create a virtual environment:

    $ Python3 -m venv .venv

Activate a virtual environment

    $ source .venv/bin/activate

Uncompress package:

    $ tar xvf mazegen-\*.tar.gz
    $ mazegen-*/src

Run the package:

    $ python -m mazegen

Deactivate a virtual environment

    $ deactivate

## License

See LICENSE.

## Algorithm for maze generation

### Carving the maze with Deep-First Search (DFS)

This program uses DFS to carve the maze. Depth-first search is a graph traversal algorithm that starts at a given root node and traverses the graph as long as it has unvisited reachable nodes. As soon as it hits a 'dead end', it backtracks to the latest visited nodes, that still has reachable unvisited nodes and continues from there.
It needs a stack data structure to be implemented, which can also be the internal function call stack of a recursive implementation.

How our implementation works:

The recursive helper function *dfs_recursion(self, curr_pos: tuple)* takes the position of any given cell in the grid (in our case the entry cell) and starts recursion from there. 
In each recursion, it performs the following steps: 

- set the current cell to 'visited'
- compute the coordinates of the surrounding (= reachable) cells in the grid (called up, down, left, right) and store them in the list 'reachable'
- It goes into a while-true loop to go into recursion for every reachable cell (true loop is exited if reachable list is empty)
- Inside the loop:
    - filter out all visited cells from reachable list
    - check break condition
    - choose random cell from reachable list
    -  depending on which surrounding cell was chosen (up, left, right or down) destroy the wall into that direction and also the adjacent wall from the surrounding cell
        - we do that by subtracting the amount from the wall configuration number, that will set the bit to 0 that represents the chosen direction (4 bit number is code for NESW -> e.g. 1011)
    - go into recursion with the position of the chosen surrounding cell
- algorithm will terminate, as soon as all recursive calls have been eliminated from the call stack (which happens, once all cells have been visited)

Side note: The maze has a padding of invisible visited cells surrounding its borders, which makes sure the DFS can't break out of the maze

### Finding the shortest path with Breadth First Search (BFS)

This program uses BFS to find the shortest path from ENTRY to EXIT. 

First we create a list 'queue' where cells will be added.

The algorithm start traversing the maze starting from ENTRY.

For each cell i.e. 'current', the function:

- mark the current cell as VISITED (VISITED is an attribute of a Cell object)
- add the current cell to the queue
- check whether there are walls blocking access to the adjacent cells of the current cell
- if there is no wall:
    - check whether the adjacent cell is VISITED
    - if the adjacent cell is not VISITED
        - add the adjacent cell to the queue
        - compute the shortest path to that cell
- repeat the above steps for all adjacent cells 
- remove the current cell from the queue
- repeat the above steps as long as the queue exist

**How do we check whether there is a wall?**

Each function has an attribute WALLS which holds an hexadecimal digit. For example, when cell.WALLS is 15 it means that the cell has 4 walls.

How can we tell that 15 means that there are 4 walls (North, East, South, West)?

Walls are represented under their binary form, that is: 1 1 1 1. Each binary corresponds to a cardinal direction, respectively North, East, South, West. 1 1 1 1 means that a cell has four walls, 1 0 1 0 means that a cell has walls on both, its North and South sides.

How does 15 relate to 1 1 1 1?

15 = 8 + 4 + 2 + 1

each digit relates to a binary 1 1 1 1.

For example the hexadecimal digit 11 is 8 + 0 + 2 + 1 so 1 0 1 1 meaning that a cell whose hexadecimal digit is 11 has walls on its North, South and West sides.

So far we can tell if a cell has walls by *looking* at the binaries, but how can we, for example, compute whether a cell has a wall on the West side?

We run modulus 2 on 15. If this is False, the least significant bit (the most right value under its binary form 1 1 1 1) is 1, so there is a wall.

If the value is 14 (8 + 4 + 2 + 0 so 1 1 1 0), modulus 2 returns 0, which tells us that there is no walls.

How do we check the subsequent walls moving to the left?

We run a floor division on the hexadecimal digit .

For example, if we run a floor division of 15 // 2 we get 7.

7 is 4 + 2 + 1 so 0 1 1 1. We omit the most left zero so we have 1 1 1, that is N E S.

We can now run modulus 2 on 7 (which return 1, so there is a wall on the South side).
