Skip to Main Content
Mazes for Programmers
book

Mazes for Programmers

by Jamis Buck
July 2015
Intermediate to advanced content levelIntermediate to advanced
286 pages
6h 31m
English
Pragmatic Bookshelf
Content preview from Mazes for Programmers

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) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Exercises for Programmers

Exercises for Programmers

Brian P. Hogan

Publisher Resources

ISBN: 9781680501315Errata Page