Class: Knossos::Algorithm::AldousBroder

Inherits:
Object
  • Object
show all
Defined in:
lib/knossos/algorithm/aldous_broder.rb

Instance Method Summary collapse

Constructor Details

#initializeAldousBroder

Returns a new instance of AldousBroder.



4
5
6
# File 'lib/knossos/algorithm/aldous_broder.rb', line 4

def initialize
  # nothing to do
end

Instance Method Details

#carve(grid:, seed: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/knossos/algorithm/aldous_broder.rb', line 8

def carve(grid:, seed: nil)
  srand(seed || Kernel.srand)

  cell = grid.random_cell
  unvisited = grid.cell_count - 1

  while unvisited > 0
    neighbor = grid.neighborhood(cell).sample

    if neighbor.links.empty?
      grid.build_passage(cell, neighbor)
      unvisited -= 1
    end

    cell = neighbor
  end

  grid
end