July 2015
Intermediate to advanced
286 pages
6h 31m
English
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) ... |