Class: Knossos::Algorithm::BinaryTree

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ BinaryTree

Returns a new instance of BinaryTree.



6
7
8
9
10
# File 'lib/knossos/algorithm/binary_tree.rb', line 6

def initialize(**options)
  options = defaults.merge(options)

  @bias = options[:bias]
end

Instance Attribute Details

#biasObject (readonly)

Returns the value of attribute bias.



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

def bias
  @bias
end

Instance Method Details

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



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/knossos/algorithm/binary_tree.rb', line 12

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

  grid.each_cell do |cell|
    neighbors = bias.map { |direction| grid.send(direction, cell) }.compact

    neighbor = neighbors.sample
    grid.build_passage(cell, neighbor) if neighbor
  end

  grid
end