Class: Amaze::Algorithm::GrowingTree
- Inherits:
-
Amaze::Algorithm
- Object
- Amaze::Algorithm
- Amaze::Algorithm::GrowingTree
- Defined in:
- lib/amaze/algorithm/growing_tree.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
The configuration of the algorithm In this instance the way the next cell gets picked from the list of active cells.
Attributes inherited from Amaze::Algorithm
Instance Method Summary collapse
- #configure(description, block) ⇒ Object
-
#initialize ⇒ GrowingTree
constructor
A new instance of GrowingTree.
- #status ⇒ Object
- #work(grid) ⇒ Object
Methods inherited from Amaze::Algorithm
Constructor Details
#initialize ⇒ GrowingTree
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
#config ⇒ Object (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 |
#status ⇒ Object
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 |