Class: Node
- Inherits:
-
Object
- Object
- Node
- Defined in:
- lib/node.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#node_text ⇒ Object
readonly
Returns the value of attribute node_text.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #add_child(node) ⇒ Object
- #add_comment(comment) ⇒ Object
- #children_to_s(with_comments = false) ⇒ Object
- #color ⇒ Object
- #comments ⇒ Object
- #coordinate ⇒ Object
-
#initialize(parent, node_text = "") ⇒ Node
constructor
A new instance of Node.
- #pass_node? ⇒ Boolean
- #time_left ⇒ Object
- #time_left=(time_left) ⇒ Object
- #to_move_list ⇒ Object
- #to_s ⇒ Object
- #validate_node_format(node) ⇒ Object
- #x ⇒ Object
- #y ⇒ Object
Constructor Details
#initialize(parent, node_text = "") ⇒ Node
Returns a new instance of Node.
5 6 7 8 9 10 11 12 |
# File 'lib/node.rb', line 5 def initialize(parent,node_text= "") validate_node_format(node_text) @node_text = node_text @comments = [] @children = [] @parent = parent @parent.add_child(self) if parent end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
3 4 5 |
# File 'lib/node.rb', line 3 def children @children end |
#node_text ⇒ Object (readonly)
Returns the value of attribute node_text.
3 4 5 |
# File 'lib/node.rb', line 3 def node_text @node_text end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
3 4 5 |
# File 'lib/node.rb', line 3 def parent @parent end |
Instance Method Details
#add_child(node) ⇒ Object
14 15 16 |
# File 'lib/node.rb', line 14 def add_child(node) @children.push(node) end |
#add_comment(comment) ⇒ Object
39 40 41 |
# File 'lib/node.rb', line 39 def add_comment(comment) @comments << (comment + "\n") end |
#children_to_s(with_comments = false) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/node.rb', line 22 def children_to_s(with_comments=false) branches = "" if @children.size > 1 @children[1..-1].each {|node| branches += "(#{node.children_to_s(with_comments)})"} end children_text ="" if @children.first children_text = @children.first.children_to_s(with_comments) end comment_node = with_comments ? comments : "" node_text + comment_node + branches + children_text end |
#color ⇒ Object
46 47 48 |
# File 'lib/node.rb', line 46 def color @node_text[1] end |
#comments ⇒ Object
42 43 44 |
# File 'lib/node.rb', line 42 def comments @comments.empty? ? "" : "C[#{@comments.join.gsub("]","\\]").gsub(")","\\)")}]" end |
#coordinate ⇒ Object
56 57 58 |
# File 'lib/node.rb', line 56 def coordinate x+y end |
#pass_node? ⇒ Boolean
59 60 61 |
# File 'lib/node.rb', line 59 def pass_node? @node_text.match(/[BW]\[\]/) end |
#time_left ⇒ Object
71 72 73 |
# File 'lib/node.rb', line 71 def time_left @node_text.match(/[#{color}]L\[\d{0,6}.\d{3}\]/).to_s[3..-2].to_f end |
#time_left=(time_left) ⇒ Object
74 75 76 |
# File 'lib/node.rb', line 74 def time_left=(time_left) @node_text.gsub!(/[#{color}]L\[\d{0,6}.\d{3}\]/, "#{color}L[%.3f]" % [time_left]) end |
#to_move_list ⇒ Object
18 19 20 |
# File 'lib/node.rb', line 18 def to_move_list children_to_s end |
#to_s ⇒ Object
35 36 37 |
# File 'lib/node.rb', line 35 def to_s children_to_s(true) end |
#validate_node_format(node) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/node.rb', line 63 def validate_node_format(node) valid = node.match(/;[BW]\[(|[a-z][a-z])\]/) if node.include?("BL") || node.include?("WL") valid = valid && node.match(/[BW]L\[\d{0,6}.\d{3}\]/) end raise "#{node} is invalid node format" unless valid end |
#x ⇒ Object
50 51 52 |
# File 'lib/node.rb', line 50 def x @node_text[3] unless pass_node? end |
#y ⇒ Object
53 54 55 |
# File 'lib/node.rb', line 53 def y @node_text[4] unless pass_node? end |