mazes_2018/sidewinder.rb

26 lines
541 B
Ruby
Raw Normal View History

2018-04-11 02:31:01 +00:00
class Sidewinder
def self.on(grid)
grid.each_row do |row|
run = []
row.each do |cell|
run << cell
at_eastern_boundary = (cell.east == nil)
at_northern_boundary = (cell.north == nil)
should_close_out = at_eastern_boundary || (!at_northern_boundary && rand(2) == 0)
if should_close_out
member = run.sample
member.link(member.north) if member.north
run.clear
else
cell.link(cell.east)
end
end
end
grid
end
end