Mazes

Community Forums/Showcase/Mazes

TomToad(Posted March) [#1]
Found a book called Mazes for Programmers. A book devoted to programming mazes. about half way through the book and below is the results. The only negative thing I have to say about the book is that all the examples are written in Ruby. Makes it a bit difficult to convert the code to BlitzMax, but not impossible.

First, a standard maze.


none of these mazes I am uploading have a start or finish. One can be added easily enough. Here are a couple of mazes generated inside shapes.



Here is a circular maze. This was a bit more challenging as each ring has a different number of cells.


There is more in the book, such as multi level mazes, mapping a maze to the surface of a cube and sphere, and mazes with different shaped cells. I'll post more as I get more into the book.


Flanker(Posted March) [#2]
Very nice results ! What algorithm did you use for the first maze ? I played with mazes on sphere recently (here), but I can't get a nice maze path using backtracking + growing tree, ruining an eventual gameplay. Yours seems to be well balanced.


Grisu(Posted March) [#3]
I am amazed. :) There has to be a lot of math involved.


TomToad(Posted March) [#4]
@Flanker, the algorithm used is called Recursive Backtracker from the book. It is very similar to what I have used in the past. Basically, you create a stack and place a random cell on it. You then pick a direction from that cell, and if that new cell hasn't been visited before, you push that onto the stack, otherwise you pick a different direction. You continue until you reach a cell that has no unvisited neighboring cells, you then pop that cell off the stack and proceed from the previous cell. When all the cells are popped from the stack, the maze is complete.

@Grisu, the math is surprisingly simple. So far, the most complex math I have run into is with the circle maze. Just a little trigonometry when converting polar coordinates to Cartesian.
x = distance * Cos(angle)
y = distance * Sin(angle)

Of course, I am talking about the math in the maze generation. I did have to write a line drawing method and a circle/arc drawing method in order to render the maze to a pixmap since pixmaps do not have those functions already. The line drawing was easy, I just had to make a couple of minor adjustments to this code archive entryBesenham's LineDraw routine (integer math only). The arc drawing was a little more difficult, but managed to get it in the end Circle/Arc drawing to pixmap.


Flanker(Posted March) [#5]
@TomToad
Thanks for the answer, I don't remember having such results with backtracking, I'll give it another try.


TomToad(Posted March) [#6]
Added a hex maze and a triangle maze. Algorithm also finds the longest path in the maze and adds dots from the start and finish. Try and go from the blue dot to the green dot.

hex

triangle



Pakz(Posted April) [#7]
I think I will also order that book. It is on sale at the moment at the store I usually order my books. Might be a good learning book.


TomToad(Posted April) [#8]
Be forewarned though, the samples are in Ruby, it's a bit different from BlitzMAX. I had a bit of trouble following in some areas, but after reading the explanation, I was able to figure it out.


Pakz(Posted April) [#9]
I just ordered it. Should be here <=Saterday.

I hope I wil be able to understand the code. Usually the books I buy use different languages, The last book I bought had examples in the C language. It was a book on 2d game collision detection routines.


Blitzplotter(Posted April) [#10]
@TomToad, Amazing ;) I used to like drawing mazes in my youth, this is very intriguing. do you think you might share any of your Maze code in the coding archives, great stuff anyways ;)


Pakz(Posted April) [#11]
@blitzplotter, If you want to see the maze code in MonkeyX? I am working on recreating all 12 methods from the book. At the moment I have 4 working with flash example on my blog.

Linked from here :
https://plus.google.com/u/0/109515261652216390114

The book is pretty good so far. I am glad I bought it. The programming language used is not very easy to convert. The author luckely has more information on the internet.


Mainsworthy(Posted April) [#12]
this was incredibly interesting to see, I don't see I would apply it yet, but If I wanted a maze this looks fun


Pakz(Posted April) [#13]
In the book there are usefull algorithms. The code in there shows how to do those.

I learned a couple of new things from the book already. There is some good code techniques in there you will be able to use for other things.

It also has a part on the dijkstra algorithm which is a very useful algorithm for artificial intelligence. You can turn this into a pathfinding routine for instance.

I also noticed parts in the book on different map types. Useful if you want to make different kinds of game map displays.

The last thing I made from the book is a map generator with rooms. It uses the recursive division algorithm. This method keeps dividing the map into halves so you end up with either a maze or a maze with rooms or rooms entirely.

The book is not cheap at full price (38 dollars) but it contains information(knowledge) that game programmers can use for their future projects.

One kind of games I like to learn to make is Role Playing Games and this book has a lot of the things that I looked up on the internet to learn more about it. All in one place.

I wish I could find more good books like this.


Blitzplotter(Posted April) [#14]
@Pakz, thanks for sharing ;) Having a look now.

[EDIT] Just tried the sidewinder code - intriguing stuff - compiling in HTML5 - great stuff.