module Kernel
def obj_info
"#{self.class} -- #{self.__id__}"
end
end
MaxLevel = 5
MaxChildren = 4
class RedBull
class Node
attr_reader :processed, :level
def initialize(level=0)
@children = []
@level = level
@processed = false #<--- set to true when we have processed this node
end
def add ()
@children << Node.new(@level+1)
puts "Added Child: #{@children.last.obj_info} -- Level: #{@children.last.level}"
end
def grow?()
#returns true if this node is allowed to grow or not.
@level < MaxLevel and @children.length < MaxChildren
end
def grow_node()
puts "Growing Node: #{self.obj_info}"
MaxChildren.times do add end
end
def next_node()
return self if !@processed
return false if @children.empty?
found = @children.find {|child| !child.processed}
#how do I iterate down through level deeper than 1
#here and return next node of say level 2 that has
#not Processed.. As an addition, how could I
#implement enumerable so I could just do an
# .each and detect if !processed?
end
def run()
@processed = true
grow_node if grow?
end
end
end
class RedBull
def initialize()
@root = Node.new
end
def next_node
@root.next_node
end
def run()
while next_node do
next_node.run
end
puts "Finished LW Processing on: #{self.obj_info}"
end
end
lw_processor = RedBull.new
lw_processor.run