Solution: Masking Weave Mazes
Understand the solution to the “Masking Weave Mazes” challenge.
We'll cover the following...
We'll cover the following...
Solution
Let's execute the following solution code and see how it works:
Press + to interact
C++
require 'weave_grid'require 'mask'require 'kruskals'class SimpleOverCell < OverCelldef neighborslist = []list << north if northlist << south if southlist << east if eastlist << west if westlistendendclass MaskedWeaveGrid < WeaveGridattr_reader :maskdef initialize(mask)@mask = masksuper(@mask.rows, @mask.columns)enddef prepare_gridArray.new(rows) do |row|Array.new(columns) do |column|SimpleOverCell.new(row, column, self) if mask[row, column]endendenddef random_cellrow, col = mask.random_locationself[row, col]enddef sizemask.countendendmask = Mask.from_png("10-teardrops-mask.png")grid = MaskedWeaveGrid.new(mask)state = Kruskals::State.new(grid)# find the candidate locationscandidates = []grid.each_cell do |cell|candidates.push cell if cell.north && cell.south && cell.east && cell.westend# attempt to add crossings to all the candidates, in random ordercandidates.shuffle.each do |candidate|state.add_crossing(candidate)endKruskals.on(grid, state)grid.to_png.save("10.png")filename="10.png"grid.to_png.save("/usercode/output/"+filename)