Sie sind auf Seite 1von 6

Roskilde University

Final Assignment for Essential Computing II

MAZE GENERATOR AND


SOLVER
Cuong Hung Ly Tran

Teacher :
Dr.Ebbe Vang

June 29, 2016

Introduction

This assignment I have made is about generating a maze by using Java. It can
print both in the console and has GUI. The GUI has a button to regenerate and
solve the maze. The algorithm I used in this program called RandomWalkMaze.

Description

The generate maze algorithm is Recursive backtracking. First I set the start
point is the cell on the top left of the matrix( with coordinate 0,0) and mark this
position as visited. Then it looks at its neighboring cells and it visit the next
cell that is not visited yet in a random order, and is inside the maze. There are
4 choices of visit: TOP, BOTTOM, LEFT, RIGHT. When there is no neighbor
to visit then the recursive method will just return to the previous cell. Every
step it walk then it put the new cooridinate in a list until the recursive method
comes back to the starting cell. The maze then consists of the walls that are
still standing after the algorithm has visited all cells.
The maze solver is made by using steps, rst I make reference to the maze
that has been created. An d then I start at the entrance of the maze on the left
side.
There are position and the direction, rst it look to the right to see wall or
not, if there's no wall, it turn right and step ahead, else it checks if there's wall
ahead or not if there's no wall ahead it takes next step if there's wall then turn
left but don't step. This is repeated until the maze is solved, or it has done
1000 steps (so it does not loop forever).
Then I put my maze and maze solver on Java swing. The wall is painted
with blue lines. The cell walls are stored as lines in a list, so a cell can become
up to 4 small lines in the list. A wall belongs to 2 cells so it is drawn 2 times with
this method. Everytime it walks then it leaves space, the steps of mazesolver
are lled with pink.

Best Features

These are my 2 best features in my program. The rst is the method of how I
generate maze.
public void generateMaze ( int x , int y ) {
xstart = x ; ystart = y ; // set the start point
with coordinate x , y
Point c = new Point (x , y ) ; // inside the
algorithm it uses point in stead of x , y

coordinate
visitList . add ( c ) ; // this is the starting point
of the maze
traceMaze ( c ) ; // start recursive algorithm
// the following is for solving the maze
get ( c ) . clearWall ( LEFT ) ; // make the entrance to
the maze
get ( new Point ( sizeX -1 ,
sizeY -1) ) . clearWall ( RIGHT ) ; // make the exit
}
/* * this is the actual recursive backtracking
method */
void traceMaze ( Point c ) {
System . out . println ( " traceMaze : Point " + c . x +
" ," + c . y + " has walls " + get ( c ) . wallsAsText () ) ;
get ( c ) . visit () ; // set this cell is visited
Vector < Integer > vect = new
Vector < Integer >() ; // List of neighbor to visit
randList ( vect ) ; // make a random list to visit
for ( int i =0; i <4; i ++) {
int w = vect . get ( i ) ;
System . out . println ( " Cell " + c . x + " ," + c . y + "
Now I look at wall " + Cell . wallName ( w ) ) ;
Point next = neighbour (c , w ) ;
if ( isLegal ( next ) ) {
if ( get ( next ) . isVisited () ) {
System . out . println ( " That neighbour
was already visited " ) ;
} else {
get ( c ) . clearWall ( w ) ;
System . out . println ( " Now this cell
has walls " +
get ( c ) . wallsAsText () ) ;
get ( next ) . clearWall ( opposite ( w ) ) ; // every
wall belong to 2 cells
System . out . printf ( " From cell %d ,% d
Go to %d ,% d \ n " , c .x , c .y , next .x ,
next . y ) ;
visitList . add ( next ) ;
traceMaze ( next ) ;
}
} else {
System . out . println ( " That neighbour was
not legal " ) ;
}

}
System . out . printf ( " Done with cell %d ,% d .
Stepping out .\ n " , c .x , c . y ) ;
}

Here is the method of solving the maze, while is is not nish, there is no wall
ahead or on the right, it keeps stepping.
public void solveMaze ( int x , int y ) {
column = x ; /* * set the position where to start */
row = y ;
dir = SOUTH ;
count = 0;
/* * for this simple maze I assume we start on the
left border and exit on the right border */
boolean finish = false ;
System . out . printf ( " row % d column % d
\ n " ,row , column ) ;
while (! finish ) {
if (! isWallToTheRight () &&
isCellRightLegal ( column , row , dir ) ) {
turnRight () ;
nextStep () ;
} else if (! isWallAhead ( dir ) &&
isCellLegal ( column , row , dir ) ) {
nextStep () ;
} else {
/* * just turn , no step */
turnLeft () ;
}
count = count + 1;
if ( column == maze . getWidth () -1 ||
count >1000) {
/* * if count is 1000 I can not solve
this maze ! To avoid infinite loop */
finish = true ;
}
System . out . printf ( " row % d column % d
\ n " ,row , column ) ;
showmaze . pinkSquare ( column , row ) ;
}
if ( count < 1000) {
System . out . println ( " Maze solved " ) ;
} else {
System . out . println ( " I give up " ) ;
}
}

Code Organization

Here is the picture of my maza class diagram:

Figure 1: Maze class diagram

The organization of the codes in my program is devided into 5 classes: Main,


Maze, Cell, SolveMaze, MazeDIsplay class and a MazeInterface interface.
- MazeInterface: attributes include TOP,RIGHT,BOTTOM,LEFT,ALLWALLS;
methods include setSize, generateMaze,clear, getWidth,getHeight,startX,startY,
isLegal, hasWall,getWalls, getMazePath, printList.
-Maze: attributes include sizeX, xstart, ystart, cells, visitList rand; methods include Maze, setSize, clear, getWidth, getHeight,startY, Cell, isLegal, hasWall,
getWalls, hasanyWall, randomWall,randList, opposite, neighbor, generateMaze,
traceMaze, printlist, getMazePath. - Cell: attributes include walls, visited,
TOP, RIGHT, BOTTOM, LEFT, ALLWALLS; methods include: Cell, clearWalls, hasWall, isVisited, visit,randomWall, wallName, wallAsText. - SolveMaze: attributes include row, column, dir, count,showMaze is a reference to
MazeDisplay class ,maze is a reference to Maze object through an interface
called MazeInterface class; methods include mazeswing, setMaze, solveMaze,
isWallAhead, isWallToTheRight, turnRight,turnLeft,NextStep
- MazeDisplay: attributes include RegenBut, h, blocksX, matrix, solve is a reference to SolveMaze class, labyrinth is a reference to Maze object through an
interface called MazeInterface class; methods include makeSquare, addSquare,
makeSquare, pinkSquare.
- Main: is the class to operate other 3 classes.

Conclusion

bigO notation: eciency is O(n) for the maze generator, because the algoritm
visits every cell in the maze exactly once. Here I assume N is the number of
cells in the maze, inside each cell the algorithm will look at 4 neighbors so that
operation is 4*O(1). But 4*0(1) is much smaller than 0(n) so the nal answer
is O(n).
Since I didn't have time to elaborate more on solving maze, I would like to do
the shortest path(Dijkstra algorithm ). So I decided to reuse the solving method
from my last assignment. I couldn't gure out how to let it solve when it start
at the 1st time. First I could let it solve at the beginning but then it couldnt
solve when I press "Regenerate" button because the "Regenerate" button call
the generate function but it doesn't include solvemaze, but if I put solvemaze to
the main class so it can start to solve at the rst time it run but then it solves
2 times(1 in Main class and 1 in Regenerate button) ,it is not ecient. So I
decided to put the solve and regenerate maze function in "Regenerate" button
and the main class is just to call GUI and show maze.

Das könnte Ihnen auch gefallen