The project is written in lua
, using the Löve 2D
framework. It is a simulation of two maze solving algorithms, and a maze generating algorithm.
To generate the maze, the program uses Prim's
algorithm, and to solve it there are two implemented algorithms: A*
and Breadth-first serach (BFS)
.
When you choose an algorithm, it immediately starts to solve the generated maze. On the top-left part of the screen you are going to see a timer counting how much time the algorithm takes to solve the maze. This is important because if you go back and choose the other algorithm, it is going to start solving the same maze, so you can compare the two algorithms with the same settings.
To launch the program you need to have Löve 2D
installed and in the main menu you'll have to select the algorithm to solve the maze. If you hit ESC
while one algorithm is running you are going to return to the main menu, where you can select the same or the other algorithm.
main.lua
: This file connects all parts of the code with theLöve 2D
framework.menu.lua
: Handles the main menu: Text and buttons.timer.lua
: File that handles the timer of the top-left part of the screensettings.lua
: File to save settings and state of the program.utils.lua
: File that contains functions used throughout various parts.
cell.lua
: This file contains the blueprint for the basic cell of the maze.maze.lua
: This file is in charge of generating the maze.
astar.lua
: This file handles solving the maze with theA*
algorithm.bfs.lua
: This file handles solving the maze with theBFS
algorithm.
This algoritm takes a grid of cell previously generated. From there it takes this steps:
- Pick the starting cell and add it to the
maze
list, then add its neighbors to thefrontier
list. - Enter a loop until the
frontier
list is empty.- Choose a random cell from the
frontier
list, let's call it current. - Connect current to the closest cell that is in the
maze
list. - Add current's neighbors to the
frontier
list.
- Choose a random cell from the
This algorithm work by minimizing the f
score of the path. f = g + h
, where g
is the distance traveled and h
is a heuristics function.
We'll have two sets, openset
and closedset
.
- Do while
openset
is not empty- Select the cell from ``openset
with the lowest
f``` score, call it current. - If current is the goal cell, we are done.
- Remove the current cell from the
openset
. - For each neighbor of current that is not on the
closedset
:- Give a tentative
g
score to the neighbor, which is current'sg
score + the distance between current and it's neighbor. - If the neighbor is in the
openset
calculate if the tentativeg
score is less than theg
score of the neighbor.- If it is less than, update the neighbor's
g
score and set current as the neighbors's "previous" cell.
- If it is less than, update the neighbor's
- If it is not on the
openset
, set the neighbor'sg
score to the tentative one, set current as the previous cell of neighbor, and add the neighbor to theopenset
. - If neighbor's previous cell was updated, set it's
f
score accordingly.
- Give a tentative
- Select the cell from ``openset
- If
openset
is empty and the algorithm has not found a solution, there is no solution.
When you finish, the path is given by following the goal cell's previous cell and so on.
This algorithm works by calculating every possible path of the maze.
You start with two sets, explored
and queue
.
- Do while
queue
is not empty.- Pop the first element in the queue and call it current.
- If current is the goal cell, we are done.
- For each neighbor:
- If the neighbor is not in
explored
.- Add the neighbor to
explored
. - Set current as the neighbor's previous cell.
- Add the neighbor to the
queue
.
- Add the neighbor to
- If the neighbor is not in
When you finish, the path is given by following the goal cell's previous cell and so on.