Implementing the WeaveGrid Class
Learn how to display the weave maze on the console in Ruby.
The WeaveGrid class
Let's implement our WeaveGrid class, which manages the two different cell types highlighted below.
Press + to interact
C++
require 'grid'require 'weave_cells'class WeaveGrid < Griddef initialize(rows, columns)@under_cells = []superenddef prepare_gridArray.new(rows) do |row|Array.new(columns) do |column|OverCell.new(row, column, self)endendenddef tunnel_under(over_cell)under_cell = UnderCell.new(over_cell)@under_cells.push under_cellenddef each_cellsuper@under_cells.each do |cell|yield cellendendend
Code explanation
Line 6: The constructor doesn’t add much, just initializing a new array, @under_cells, that will be used to hold the under-cells that get created.
Line 18: Here, the creation of those cells is managed by the tunnel_under method.
Line 26: We also need to make sure that our each_cell method doesn’t ignore under-cells. It needs to report every ...
Ask