JLION.COM - A DotNet class for randomly generating mazes.
10/01/06 ASP.NET, Games, VB.NET

Many years ago I used to play a game called "asylum" on the TRS-80 model III that I had at the time. Asylum was a graphical adventure game and quite revolutionary for its time. The player used the keyboard arrow keys to navigate a maze rendered in first-person perspective 3D. There were simple objects in various rooms (keys, for one), some rooms had doors, and there were other inmates. The objective was to escape from the Asylum.


In the years since Asylum I have played much more sophisticated 3D adventure games such as Castle Wolfenstein, then Doom, and then Duke Nuke'em. Now such games are incredibly complex, rendered with photo-realistic graphics and allow for for up and down movement as well as left, right, forward and back. Gone are the days of simple hallways, rooms and mazes.

Recently I've been working on another project, a simple graphics class, and while writing code to draw lines and circles I found myself thinking back to those days of Asylum. It would be fun to create an Asylum-type adventure game in Dot Net, I thought, and not too terribly difficult. First I would need a maze.

Well, I already had written a program to draw mazes, although in Java not in Dot Net. Dot Net is wonderfully similar to Java so that wasn't a problem. In an hour or so I had the Java program converted to a Dot Net class and had created a web page to stream the completed maze out to an <IMG> tag.

The next step of course is to provide first-person perspective navigation of the maze. I don't think this will be difficult, but it may be a while before I find time to work on it so I'm posting what I have done so far. If I find time, I plan to describe here how the maze algorithm works. In the interim if you're interested you will need to download the source code and have a look through it yourself.

Displaying the maze is a several-step process. First you need to add an image tag to a web page. The image tag takes the following format: Page URL + these querystring parameters:
  1. H=Number of cells that should be displayed vertically
  2. W=Number of cells that should be displayed horizontally
  3. S=Either "Y" to display the maze solution (in red), or "N" to not display the solution.
  4. M=A number between 0 and 32767 that indentifies a maze.
    <img src="http://www.jlion.com/tools/jmaze.aspx?H=10&W=10&S=Y&M=353" />

Here is what the code-behind source of jMaze.aspx looks like. Here, you can see that I'm creating a 400x400 pixel graphic and determining the size of the cells in the maze based on the number of cells that should be displayed in the horizontal and vertical axes. The New method of the jMaze.MazeBrain class takes five parameters and generates the maze. The parameters are:
  1. The width of the maze, in pixels
  2. The height of the maze, in pixels
  3. The width of each cell in the maze, in pixels
  4. The height of each cell in the maze, in pixels
  5. A number between 0 and 32767 that identifies the maze to be displayed
Once the maze has been generated, the jMaze.paintMaze method draws the finished maze on a bitmap image. This method takes two parameters:
  1. A system.drawing.graphics object representing the image on which to draw the maze
  2. A boolean value indicating whether to also draw the solution to the maze (true) or not (false)
Finally, the bitmap, with maze drawn, is streamed back to the calling page.
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim lWidth As Integer = Request.QueryString("W")
        Dim lHeight As Integer = Request.QueryString("H")
        Dim lID As Long = Request.QueryString("M")
        Dim bShowSolution As Boolean = False
        If Request.QueryString("S") = "Y" Then bShowSolution = True

        Dim lCellWidth As Integer = 400 / lWidth
        Dim lCellHeight As Integer = 400 / lHeight

        Dim oMaze As New jMaze.MazeBrain(400, 400, lCellWidth, lCellHeight, lID)
        Dim oMazeImage As New Bitmap(400, 400)
        Dim oGraphics As Graphics

        oGraphics = Graphics.FromImage(oMazeImage)
        oGraphics.FillRectangle(New SolidBrush(Color.AntiqueWhite), New Rectangle(0, 0, lWidth, lHeight))

        oMaze.paintMaze(oGraphics, bShowSolution)

        Response.ContentType = "image/JPEG"
        oMazeImage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif)

    End Sub
A ZIP file with the VB.NET 2.0 source for the jMaze class can be downloaded here