July 2015
Intermediate to advanced
286 pages
6h 31m
English
To make this work in code, we need two things: a cell that can be assigned a weight, and a grid composed of these weighted cells.
The cell is straightforward. We’re just going to subclass Cell and add weight information to it. Also, since our Dijkstra’s algorithm was implemented on Cell, we put our updated implementation in the subclass, too, by overriding the original distances method.
Put the following in weighted_cell.rb.
| weighted_cell.rb | |
| Line 1 | require 'cell' |
| - | |
| - | class WeightedCell < Cell |
| - | attr_accessor :weight |
| 5 | |
| - | def initialize(row, column) |
| - | super(row, column) |
| - | @weight = 1 |
| - | end |
| 10 | |
| - | def distances |
| - | weights = Distances.new(self) |
| - | pending = [ self ] |
| - | |
| 15 | while |