Implementing a Third Dimension

Learn to implement a 3D maze in Ruby.


When we first worked on the code behind the grid, we implemented the prepare_grid method using a two-dimensional array. That’s not just coincidence—that’s exactly what determined the geometry of our grid! We’ll add a third dimension to the grid by adding a third dimension to that array.

However, it's not that easy. The good news, though, is that the other changes are minor related to that array modification. The cells need to be told about their new up/down neighbors, and the grid needs to know how to iterate over cells in the other levels, and so on.

It’s really not too hard, and best of all is that once we’ve made those changes, most of the algorithms we’ve looked at will automatically work. Even those that break with these changes can be brought into line with a few simple modifications. Let's implement this. For simplicity's sake, we'll put grid and cell in the same file, called grid3d.rb. We'll start with the Cell3D class.

The Cell3D class

Get hands-on with 1200+ tech skills courses.