The constants there represent bits to set within a bitfield—e.g. N is the first bit (2^0==1), S is the second, E is the third, and W is the fourth. (2^3 == 8). DX indicates the direction in X that a given compass direction takes you–DX[E] == 1, DX[W] == -1, and N and S don’t go any distance in X. DY is the same, but for Y. (I hope that makes sense!)
The code in the book is much more intuitive and object-oriented. Instead of using bitfields to represent cells, I’m using actual classes and objects with named accessors. It’s not as memory-efficient as the bitfields, but it’s a lot easier to understand and work with!
Locking a maze to a random seed happens as a side-effect of how pseudo-random number generators (PRNG) work. It’ll work this way in any language, and not just Ruby. If you give a PRNG the same starting seed, it will always produce the same sequence of “random” numbers. Because the maze algorithm itself simply consumes these random numbers, you’ll always get the same maze if you give it the same sequence of random values. This means that (as long as you don’t change the algorithm at all) you can represent an entire maze with just the dimensions of the grid, and the random seed used to generate it.
Pretty cool! It’s really, REALLY handy for debugging these programs. Just print the seed you’re using, and when you see a maze that isn’t being generated or displayed correctly, you lock the seed to the one that was used, and then track down the bug.
And for licensing: the code I used on those blog posts is all in the public domain—you can use it for whatever you want, however you want, without attribution (though attribution would be appreciated).
The code from the book, though, is a bit more restrictive. Each file includes the following header:
# Excerpted from "Mazes for Programmers",
# published by The Pragmatic Bookshelf.
# Copyrights apply to this code. It may not be used to create training material,
# courses, books, articles, and the like. Contact us if you are in doubt.
# We make no guarantees that this code is fit for any purpose.
# Visit http://www.pragmaticprogrammer.com/titles/jbmaze for more book information.
Aside from the restrictions mentioned there, though, you may use the code however you like, as long as it retains that copyright notice.