Class: Amaze::Algorithm::GrowingTree

Inherits:
Amaze::Algorithm show all
Defined in:
lib/amaze/algorithm/growing_tree.rb

Instance Attribute Summary collapse

Attributes inherited from Amaze::Algorithm

#duration

Instance Method Summary collapse

Methods inherited from Amaze::Algorithm

#on, #speed

Constructor Details

#initializeGrowingTree

Returns a new instance of GrowingTree.



9
10
11
12
# File 'lib/amaze/algorithm/growing_tree.rb', line 9

def initialize
  @description = "last from list"
  @config = Proc.new {|active| active.last }
end

Instance Attribute Details

#configObject (readonly)

The configuration of the algorithm In this instance the way the next cell gets picked from the list of active cells.



7
8
9
# File 'lib/amaze/algorithm/growing_tree.rb', line 7

def config
  @config
end

Instance Method Details

#configure(description, block) ⇒ Object



14
15
16
17
# File 'lib/amaze/algorithm/growing_tree.rb', line 14

def configure description, block
  @description = description
  @config = block
end

#statusObject



49
50
51
# File 'lib/amaze/algorithm/growing_tree.rb', line 49

def status
  "Growing tree (#{@description}) algorithm: #{duration}s"
end

#work(grid) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/amaze/algorithm/growing_tree.rb', line 19

def work grid
  # true when active gets cells added
  # false when active gets cells deleted
  mode = true

  active = [grid.random_cell]
  
  while active.any?
    cell = config.call active
    
    neighbor = cell.neighbors.select {|neighbor| neighbor.links.empty? }.sample
    
    yield Stat.new(                                  # visualize
      current: active,                               #
      pause: mode ^ !!neighbor,                      #
      info: "Active #{active.size}") if block_given? #
    mode = !!neighbor                                #
    
    if neighbor
      cell.link neighbor
      active << neighbor
    else
      active.delete cell
    end
    
  end
  
  grid
end