Maze Generation Code and Diamond Maze

The following is the current iteration of the maze generation algorithm I’m using.  Currently, I am working on a remake of Diamond Maze with XNA.

Yes, it will be using the same ASCII style graphics as it did originally.  This is more of a “ramp up on how I’m going to do stuff in XNA” than anything else.

A colleague of mine once referred to Diamond Maze as a roguelike, which I had never really thought of it as, but I guess the term does accurately describe the game.  I usually associate a way more elaborate combat system and inventory system than what I used in DM.

This code is my implementation of Pim’s algorithm.  It isn’t terribly useful without the associated classes, but I just wanted to share it.

public void Generate(int randomSeed)
{
Random random = new Random(randomSeed);
Clear();
List<MazeCell> frontier = new List<MazeCell>();
MazeCell cell = GetCell(random.Next(Width), random.Next(Height));
cell.Frontier = false;
int direction;
for (direction = MazeDirections.First; direction <= MazeDirections.Last; ++direction)
{
if (cell.Neighbors[direction] != null && cell.Neighbors[direction].Frontier)
{
frontier.Add(cell.Neighbors[direction]);
}
}
while (frontier.Count > 0)
{
cell = frontier[random.Next(frontier.Count)];
frontier.Remove(cell);
cell.Frontier = false;
do
{
direction = random.Next(MazeDirections.Count);
}while(cell.Neighbors[direction]==null || cell.Neighbors[direction].Frontier);
cell.Portals[direction].Open = true;
for (direction = MazeDirections.First; direction <= MazeDirections.Last; ++direction)
{
if (cell.Neighbors[direction] != null && cell.Neighbors[direction].Frontier && !frontier.Contains(cell.Neighbors[direction]))
{
frontier.Add(cell.Neighbors[direction]);
}
}
}
}

Advertisements

Tags: , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: