Você está na página 1de 4

Maze generation and solving

solving: a puzzle game using real-time maze


generation
BLIND REVIEW
Figura 1: A maze generated by Eller's algorithm

ABSTRACT
Mobile games are becoming more and more common nowadays,
this may be due to the fact that we have easier access to them and
their development is simple compared to modern console games,
one video game type that works well with mo
mobile games is the
puzzle type and they are expected to exercise the brain. Our game
will exercise the spatial intelligence, exploring a deep part of
brain little used nowadays in games by making the player solve
the maze mentally first then applying the inputs
puts to solve. Our
purpose here is to find the most appropriate algorithm to use on a
game that needs to create and solve mazes in real time, hence, the
algorithms need time and space (memory) efficiency, which will
be key aspects in our decision. We will use the method of
calculating the complexity by primitive operation analysis to
make sure what the best is algorithm to implement in the game.
Future works will be the game design and the game development
itself.
Keywords: maze generation, maze solving, mobile games, eller's
algorithm, union-find
1

INTRODUCTION

A maze generation algorithm is any algorithm that generates data


structures that can be further represented as mazes, those
structures can be 2D matrices, disjoint sets, graphs, etc. and their
complexity may vary from O(n) to no certainty of conclusion.
Another interesting fact about maze generation algorithms is
they tend to follow some patterns, they can be biased in some axis
or have more or less dead ends, these characteristics can be
advantageous depending on the context,, for example, having a
maze that tends to have more horizontal hallways, is generally
more difficult to solve.
The research will be focused only on perfect maze generation
algorithms, and from the most popular algorithms, we will
highlight, recursive backtracking, Kruskal's
ruskal's algorithm, Prims
algorithm and Eller's algorithm.

2.1

Generation algorithms

cking algorithm [1]


2.1.1 Recursive backtracking
The recursive backtracker is an iterative, stack-based
stack
algorithm
which applies a process to a stack of cells. The stack of cells starts
off empty and the process is listed below.
1. Select random cell in grid and add it to the stack S.
S
2. Checks if S is empty. If S is empty then terminate the
algorithm.
3. Select the last cell C from S.
4. Select one of the neighboring cells N that has not
been carved into yet and carve a passage from C to N. The Von
Neumann neighborhood is used in this algorithm with a radius of
1.
5. Add N to S. Y
6. If C has no more unvisited neighbors, remove it from
S.
7. Repeat from step 2.
2.1.2 Eller's algorithm [4]
Ellers algorithm is very interesting because it creates perfect
mazes with infinite size, vertically speaking, and in linear time
[4],, also, it requires only memory equivalent to 2 rows.
1.
2.

3.

Initialize the cells of the first row to each exist in their


own set.
Now, randomly join adjacent cells, but only if they are
not in the same set. When joining adjacent cells, merge
the cells of both sets into a single set, indicating that all
cells in both sets are now connected (there is a path that
connects any two cells in the set).
For each set, randomly create vertical connections
downward to the next
ext row. Each remaining set must
have at least one vertical connection. The cells in the
next row thus connected must share the set of the cell
above them.

4.
5.
6.

Flesh out the next row by putting any remaining cells


into their own sets.
Repeat until the last row is reached.
For the last row, join all adjacent cells that do not share
a set, and omit the vertical connections, and youre
done!

2.1.3 Randomized Kruskal's algorithm [5]


The original Kruskals algorithm is based on a graph data
structure and uses an ordered list of edge weights to create a
minimal spanning tree, but when modified, it can create perfect
mazes. Instead of using the global minimum edge weight, it will
randomly check if the edge creates any kind of cycle, if not, then
add it to the maze, the algorithm stops when every one of the cells
are connected.
1.
2.

3.

Start with a list of every possible "edge" (that is, each


wall between adjacent cells).
Choose an edge from the list. Does that edge separate
two cells that are already connected?
a. YES: Choose a new one.
b. NO: Remove that wallnoting what cells are
connected to one another as a resultandreturn to
step 2.
When all cells are connected, the maze is done.

2.1.4 Randomized Prim's algorithm [3]


Randomized Prims algorithm, like Kruskals, is modified to
choose a random edge, but in this case the algorithm chooses one
of the cells that are adjacent to the starting cell, and then one of
the cells that are adjacent to the chosen cell, always respecting the
rule of not creating any cycle.
1.
2.
3.

4.

Choose a starting cell, and put it in "the list."


Remove from the list any cells with no available
neighbors.
Choose a random cell from the list, then choose a
random neighbor of that cell, remove the intervening
wall, and add the new cell to the list.
When the list is empty, the maze is done.

2.1.5 Growing tree algorithm [7]


1.
2.

3.

Let C be a list of cells, initially empty. Add one cell to


C, at random.
Choose a cell from C, and carve a passage to any
unvisited neighbor of that cell, adding that neighbor to
C as well. If there are no unvisited neighbors, remove
the cell from C.
Repeat #2 until C is empty.

2.2 Solving Algorithms


The main object to use the solving algorithm is to calculate the
score of each stage that the player will play.
Below we list some algorithms that will bring 100% of accuracy
to solve the maze, seeing that there are some algorithms that
cannot reach a solution to this problem and have an inefficient
asymptotic time of execution.
In this case was choose the best-performance and precise
algorithm: A*

2.2.1 Breadth-First Search [8]


Breadth-first search (BFS) is an algorithm that traverses a graph in
search of one or more goal nodes. As we will discover in a few
weeks, a maze is a special instance of the mathematical object
known as a "graph". In the meantime, however, we will use
"maze" and "graph" interchangeably.
The defining characteristic of this search is that, whenever BFS
examines a maze cell c, it adds the neighbours of c to a set of cells
which it will to examine later. In contrast to DFS, these cells are
removed from this set in the order in which they were visited; that
is, BFS maintains a queue of cells which have been visited but not
yet examined (an examination of a cell c consists of visiting all of
its neighbours). Thus, a cell can have three states:
1. Unvisited. The cell has not yet been visited by BFS.
2. Visited but Not Examined. The cell has been discovered,
but BFS has not evaluated whether its neighbours
should be added to the Queue.
3. Examined. The cell has been visited, and all its
neighbours are/have been in the queue (ie, they are
already "Visited but Not Examined" or "Examined").
2.2.2 Wall Following [9]
The wall following, the best-known rule for traversing mazes, is
also known as either the left-hand rule or the right-hand rule. If
the maze is simply connected, that is, all its walls are connected
together or to the maze's outer boundary, then by keeping one
hand in contact with one wall of the maze the solver is guaranteed
not to get lost and will reach a different exit if there is one;
otherwise, he or she will return to the entrance having traversed
every corridor next to that connected section of walls at least
once.
1.
Find
the
closest
wall
2.
Move to a desired distance from the wall
3. Turn and start moving along the wall, staying the
desired
distance
away
4a.
Avoid running into a wall ahead of you
4b. Turn into gaps to your right or left
2.2.3 Recursive Backtracking [10]

This algorithm is a bit different, because if you always choose the


newest cell (the one most recently added), youll get the recursive
backtracker, butif you choose a cell at random, you get Prims.
And then you can use characteristics of both algorithms at the
same time to generate more interesting mazes.

A Maze is given as N*N binary matrix of blocks where source


block is the upper left most block i.e., maze[0][0] and destination
block is lower rightmost block i.e., maze[N-1][N-1]. The
algorithm starts from source and has to reach destination.
In the maze matrix, 0 means the block is dead end and 1 means
the block can be used in the path from source to destination
1.
2.

If the algorithm reaches the des-ti-na-tion, print the


solu-tion matrix.
Else, mark the cur-rent cell in solu-tion matrix as 1

3.

4.

5.

6.

7.

If pre-vi-ous step is not in ver-ti-cal direc-tion


(upwards) then move for-ward in the ver-ti-cal
direction(downwards) and recur-sively check if this
move-ment leads to solution.
If move-ment in above step doesnt lead to the solu-tion
and If pre-vi-ous step is not in hor-i-zon-tal direc-tion
(towards left) then move for-ward in the hor-i-zon-tal
direction(towards right) and recur-sively check if this
move-ment leads to solution.
If move-ment in above step doesnt lead to the solu-tion
and If pre-vi-ous step is not in ver-ti-cal direc-tion
(down-wards) then move for-ward in the hor-i-zon-tal
direction(upwards) and recur-sively check if this
move-ment leads to solution.
If move-ment in above step doesnt lead to the solu-tion
and If pre-vi-ous step is not in hor-i-zon-tal direc-tion
(towards right) then move for-ward in the hor-i-zon-tal
direction(towards left) and recur-sively check if this
move-ment leads to solution.
If none of the above solu-tion works then
BACKTRACK and mark the cur-rent cell as 0.

2.2.4 Dead end filling [11]


Dead-end filling is an algorithm for solving mazes that fills all
dead ends, leaving only the correct ways unfilled. This method
looks at the entire maze at once. Dead-end filling cannot
accidentally "cut off" the start from the finish since each step of
the process preserves the topology of the maze. Furthermore, the
process won't stop "too soon" since the end result cannot contain
any dead-ends. Thus if dead-end filling is done on a perfect maze
(maze with no loops), then only the solution will remain.
1.
2.

Find all of the dead-ends in the maze


Fill in the path from each dead-end until the
first junction is met. Note that some passages
won't become parts of dead end passages until
other dead ends are removed first.

2.2.5 Dead end filling [12]


A* is an informed search algorithm, meaning that it solves
problems by searching among all possible paths to the solution for
the one that incurs the smallest, and among these paths it first
considers the ones that appear to lead most quickly to the solution.
It is formulated in terms of weighted graphs: starting from an
initial node of a graph, it constructs a tree of paths starting from
that node, expanding paths one step at a time, until one of its paths
ends at the predetermined goal node.
1.
2.
3.
4.

5.

Initialize Qas a search node and S as single


entry
If Q is empty, stop. Else choose a better
element from Q.
If the element that you are looking for is n,
return n. Else remove n from Q.
Searching the parents of the element n that
was not visited yet, and create all the
expansions of n for each parent
Add the extends paths to Q and repeat the step
2

2.3 The data structure


All the aforementioned algorithms are compatible with the very
efficient disjoint-set data structure, also called union-find,and
therefore it will be used on the game development.
Furthermore, a find operation complexity is always O(1) for each
node contains the name of the list to which it belongs, and a union
operation complexity can reach ((, [6] with correct
tuning, called union-by-rank and path compression. (, is the
inverse of the extremely fast growing ackermann function and
grows slower than log(N).
3

DISCUSSION

For maze generation, we chose Ellers algorithm for its time


complexity being the most efficient, O(n) [6]. For maze solving,
we chose A* algorithm, because it is more efficient when the
maze exit is known. The reason is A* uses heuristics that can
conduct its search based on the exit relative x and y position.
4

CONCLUSION

The game will be a mobile puzzle game and its main


characteristic will be the generation of puzzles from one stage to
another. For example, on survival mode, if the player loses, he/her
will have to start over, and memorizing the five steps he took to
complete the first stage won't work, since the maze will be
entirely different.
The solving algorithm will give us the shortest path between start
and finish, also called the solution, and if one manages to input
exactly the solution, one will be given bonus points.
REFERENCES
[1]

Pech, A. (2013). Using genetic algorithms to find cellular automata


rule sets capable of generating maze-like game level layouts.
Retrieved from http://ro.ecu.edu.au/theses_hons/95 [acessed 2016
May 10]
[2] Nester, D.: Maze Profiler, Bluffton University, Bluffton, Ohio,
available at URL http://www.bluffton.edu/~nesterd/java/maze.html.
[3] Jamis Buck, Maze generation: Prims algorithm, January 2011,
http://weblog.jamisbuck.org/2011/1/10/maze-generation-prim-salgorithm [acessed 2016 May 10]
[4] Jamis Buck, Maze generation: Ellerss algorithm, December
2010,http://weblog.jamisbuck.org/2010/12/29/maze-generationeller-s-algorithm.html[acessed 2016 May 10]
[5] Jamis Buck, "Maze Generation: Kruskal's, algorithm", January,
2011,
http://weblog.jamisbuck.org/2011/1/3/maze-generationkruskal-s-algorithm.html[acessed 2016 May 10]
[6] Weiss, MA.1994. Data Structures and Algorithm Analysis, second
edition.Pages 266 - 267, The Benjamin/Cummings Publishing
Company.
[7] Jamis Buck, Maze generation: Growing tree algorithm, January
2011,
http://weblog.jamisbuck.org/2011/1/27/maze-generationgrowing-tree-algorithm [acessed 2016 May 13]
[8] Leiserson, Charles E.; Schardl, Tao B, "A Work-Efficient Parallel
Breadth-First
Search
Algorithm",
(2010).
http://supertech.csail.mit.edu/papers/pbfs.pdf
[9] Bayer, Karl, "Wall Following for Autonomous Navigation" , Summer
2012, https://www.seas.upenn.edu/sunfest/docs/papers/12-bayer.pdf
[10] Robert I. Pitts, "Rercursion: Solving a Maze", 1993,
https://www.cs.bu.edu/teaching/alg/maze/
[11] Willardson M. David, "Analysis of Micromouse Maze Solution
Algorithm", Spring 2001
http://web.cecs.pdx.edu/~edam/Reports/2001/DWillardson.pdf
[12] Becker K. Solving Mazes with AI Pathfinding Techniques: A* vs
Tremaux, May 2013

http://www.primaryobjects.com/2013/05/13/solving-mazes-with-aipathfinding-techniques-a-vs-tremaux/

Você também pode gostar