Search⌘ K

Solution: Masking Weave Mazes

Explore how to generate well-structured weave mazes by using masking methods combined with Kruskal's algorithm. Understand the steps to select candidate cells, apply crossings, and create predictable maze patterns with this approach.

We'll cover the following...

Solution

Let's execute the following solution code and see how it works:

C++
require 'weave_grid'
require 'mask'
require 'kruskals'
class SimpleOverCell < OverCell
def neighbors
list = []
list << north if north
list << south if south
list << east if east
list << west if west
list
end
end
class MaskedWeaveGrid < WeaveGrid
attr_reader :mask
def initialize(mask)
@mask = mask
super(@mask.rows, @mask.columns)
end
def prepare_grid
Array.new(rows) do |row|
Array.new(columns) do |column|
SimpleOverCell.new(row, column, self) if mask[row, column]
end
end
end
def random_cell
row, col = mask.random_location
self[row, col]
end
def size
mask.count
end
end
mask = Mask.from_png("10-teardrops-mask.png")
grid = MaskedWeaveGrid.new(mask)
state = Kruskals::State.new(grid)
# find the candidate locations
candidates = []
grid.each_cell do |cell|
candidates.push cell if cell.north && cell.south && cell.east && cell.west
end
# attempt to add crossings to all the candidates, in random order
candidates.shuffle.each do |candidate|
state.add_crossing(candidate)
end
Kruskals.on(grid, state)
grid.to_png.save("10.png")
filename="10.png"
grid.to_png.save("/usercode/output/"+filename)

Code explanation

...