Friday, January 24, 2014

Taking the MSDN maze tutorial up a notch

I thought I had finished all that MSDN had to offer as far as C# tutorials go, but I stumbled across this maze tutorial and figured I might as well work through it for completion sake.  Started out pretty simple, much like the others. The we got to the part where we are building the maze... and he's manually laying out labels as blocks. Are you kidding me?



Seriously? You expect me to spend an hour laying out label blocks? Well forget that...
So I thought to myself "There has got to be a way to automatically generate a maze...". I fooled around in excel with some random number stuff and quickly decided I should just look up an algorithm and implement it rather than trying to reinvent the wheel. Turns out there is a whole Wikipedia article on maze algorithms. The "depth first search" algorithm appealed to me, so I got to work.

I watched a couple YouTube videos of the algorithm at work, and was able to work out its basic operation. Through a bit of trial and error, I was able to get it to generate a maze.  This involved a couple of funny missteps along the way. At one point it would just do one passage straight along the top and stop well short of finishing, another just ran back and forth to the end.
Something isn't right here...
Because I was curious about the process, I ran it once with the maze generation stopped at 200 moves (was taking about 480 moves to finish a maze). I found the results interesting.
Blue squares are virgin wall. Yellow squares are passageways that are not finalized.
The result when I got the kinks worked out was a maze game that regenerated on start up and every time you successfully finished. What dooms this game is the simple fact that it is boring and annoying. I played through a couple times and that was plenty. Goes to show that effective execution won't help a doomed concept.
The final product. Does everything you would expect... except entertain.
[EDIT] Note to self: Do NOT keep multiple document editing windows open... recipe for re-written blog posts...

[UPDATE 1/30/2014] Tweaked it so that the maze generation process is animated. While this probably defeats the purpose of the game (given that its easy to see the solution as you watch the maze get built) it would be easy enough to have a checkbox disable refreshing the panel.

No comments:

Post a Comment