O'Reilly logo

Mazes for Programmers by Jamis Buck

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing Dijkstra’s

To bring this simplified version of Dijkstra’s together, we’re going to lean on a new class, called Distances, which will keep track of how far each cell is from the reference cell (the cell where we start counting from). Once we’ve implemented that class, we’ll add our actual implementation of Dijkstra’s to the Cell class, which will let us apply it pretty much anywhere we need to.

So, first off, let’s add that Distances class. For now, it’s just a simple wrapper around a Hash instance, but we’ll be making it more useful soon. Create a new file named distances.rb, and add the following to it.

distances.rb
 
class​ Distances
 
def​ initialize(root)
 
@root = root
 
@cells = {}
 
@cells[@root] = 0
 
end
 
 
def​ [](cell) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required