Simple MazeGenerator

Community Forums/Showcase/Simple MazeGenerator

Rixarn(Posted 2009) [#1]
Hi! My current proyect involves lot's of mini games, and some of them are mazes!

This little piece of code generates mazes using either DFS algorithm or Prim´s algorithm. Haven't implemented Kruskal's algorithm yet, but i don't think i'll be needing it.

This is the mazegen lib, still WIP


First example showing how to generate mazes.
SuperStrict

Include "mazelib.bmx"

SeedRnd MilliSecs()

Graphics 1024, 768

Local mazeDFS:TMaze = TMazeGenDFS.createDFSMaze(10, 10, 0, 0, 9, 9, 40)
mazeDFS.setMazeXY(50, 100)

Local mazePRim:TMaze = TMazeGenPrim.createPrimMaze(10, 10, 0, 0, 9, 9, 40)
mazePrim.setMazeXY(562, 100)

Local text1:String = "A simple demostration of two 10x10 mazes using each algorithm."
Local text2:String = "Press Return key to generate new mazes"

Repeat

	DrawText text1, (1024 - TextWidth(text1)) / 2, 20
	DrawText text2, (1024 - TextWidth(text2)) / 2, 40
		
	DrawText "A perfect maze using DFS algorithm", 50, 80
	mazeDFS.drawMaze()
	
	DrawText "A perfect maze using Prim's algorithm", 562, 80
	mazePrim.drawMaze()
	
	If KeyHit(KEY_RETURN)
		mazeDFS = TMazeGenDFS.createDFSMaze(10, 10, 0, 0, 9, 9, 40)
		mazeDFS.setMazeXY(50, 100)
		mazePRim = TMazeGenPrim.createPrimMaze(10, 10, 0, 0, 9, 9, 40)
		mazePrim.setMazeXY(562, 100)
	End If
	
	Flip(1) ;Cls()
Until KeyDown(KEY_ESCAPE) Or AppTerminate()




Second example let's user solve them.
SuperStrict

Include "mazelib.bmx"

SeedRnd MilliSecs()

Graphics 1024, 768

Local mazeGameDFS:TMazeGame = TMazeGame.Create(10, 10, 0, 0, 9, 9, 40, TMazeGame.MAZEDFS)
mazeGameDFS.maze.setMazeXY(50, 100)

Local mazeGamePRim:TMazeGame = TMazeGame.Create(10, 10, 0, 0, 9, 9, 40, TMazeGame.MAZEPRIM)
mazeGamePrim.maze.setMazeXY(562, 100)

Local text1:String = "A simple demostration of two 10x10 mazes using each algorithm."
Local text2:String = "Press Return key to generate new mazes"
Local text3:String = "Use the mouse to solve the mazes."

'Print mazegamedfs.maze.StartCell.CordX + ":" + mazegamedfs.maze.StartCell.drawx + "," + mazegamedfs.maze.StartCell.drawy
'Print mazegamedfs.maze.EndCell.CordX + ":" + mazegamedfs.maze.StartCell.drawx + "," + mazegamedfs.maze.EndCell.drawx

Repeat

	DrawText text1, (1024 - TextWidth(text1)) / 2, 20
	DrawText text2, (1024 - TextWidth(text2)) / 2, 34
	DrawText text3, (1024 - TextWidth(text3)) / 2, 46
	
	DrawText "A perfect maze using DFS algorithm", 50, 80
	mazegamedfs.update()
	mazeGameDFS.draw()
	
	Local S:TMazeCell = mazegameDFS.maze.StartCell
	Local E:TMazeCell = mazegameDFS.maze.EndCell
	
	DrawText "ENTRACE", S.drawx + s.drawxrelative, s.drawy + s.drawyrelative + 5
	DrawText "EXIT", E.drawx + E.drawxrelative, E.drawy + E.drawyrelative + 5
	
	DrawText "A perfect maze using Prim's algorithm", 562, 80
	mazeGamePrim.draw()
	mazegamePrim.update()
	
	 S = mazegamePRIM.maze.StartCell
	 E = mazegamePRIM.maze.EndCell
	
	DrawText "ENTRACE", S.drawx + s.drawxrelative, s.drawy + s.drawyrelative + 5
	DrawText "EXIT", E.drawx + E.drawxrelative, E.drawy + E.drawyrelative + 5
	
	If KeyHit(KEY_RETURN)
		mazegameDFS = TMazeGame.Create(10, 10, 0, 0, 9, 9, 40, TMazeGame.MAZEDFS)
		mazegameDFS.maze.setMazeXY(50, 100)
		mazegamePRim = TMazeGame.Create(10, 10, 0, 0, 9, 9, 40, TMazeGame.MAZEPRIM)
		mazegamePrim.maze.setMazeXY(562, 100)
	End If
	
	Flip(1) ;Cls()
Until KeyDown(KEY_ESCAPE) Or AppTerminate()



There´s also a function in the lib that solves those mazes, and returns a list of cells that are part of the solution.

Im using this maze generators in a tiled dungeon maze, (still haven't had the time to clean my messy tile engine code, but i will eventually.) And the main goal is to remember the path from finish to start (backwards ^^)...The camera hovers the maze from exit to start and then lets the player solve it, with a limited amount of time.


WERDNA(Posted 2009) [#2]
Hey this looks pretty cool Rixarn!

I'll check it out when I get a chance.


Rixarn(Posted 2009) [#3]
Cool! if you make any improvements let me know, thanks :)


WERDNA(Posted 2009) [#4]
This is really cool Rixarn!

I finally got around to taking a good look at this, and this would actually
prove quite useful in a few games that I have planned :)

Is there any way to scale the mazes to a bigger\smaller size?

if you make any improvements let me know,

A; your code is pretty good as it is, and not likely to be easily improved upon.
B; I only know a little bit about BlitzMax :)
Usually I stick with Blitz3D


Keep up the good work!

WERDNA