lib/sgf/tree.rb in SgfParser-1.0.0 vs lib/sgf/tree.rb in SgfParser-2.0.0
- old
+ new
@@ -1,24 +1,22 @@
module SGF
+ #Tree holds most of the logic, for now. It has all the nodes, can iterate over them, and can even save to a file!
+ #Somehow this feels like it should be split into another class or two...
class Tree
include Enumerable
- attr_accessor :root
+ attr_accessor :root, :current_node, :errors
- def initialize sgf
+ def initialize
@root = Node.new
- @sgf = sgf
+ @current_node = @root
+ @errors = []
end
- def each order=:preorder, &block
- # I know, I know. SGF is only preorder. Well, it's implemented, ain't it?
- # Stop complaining.
- case order
- when :preorder
- preorder @root, &block
- end
+ def each
+ games.each { |game| game.each { |node| yield node } }
end
# Compares a tree to another tree, node by node.
# Nodes must be the same (same properties, parents and children).
def == other_tree
@@ -27,65 +25,53 @@
each { |node| one << node }
other_tree.each { |node| two << node }
one == two
end
- # Saves the tree as an SGF file. raises an error if a filename is not given.
- # tree.save :filename => file_name
- def save args={}
- raise ArgumentError, "No file name provided" if args[:filename].nil?
- @savable_sgf = "("
- @root.children.each { |child| write_node child }
- @savable_sgf << ")"
+ #Returns an array of the Game objects in this tree.
+ def games
+ populate_game_array
+ end
- File.open(args[:filename], 'w') { |f| f << @savable_sgf }
+ def to_s
+ out = "#<SGF::Tree:#{self.object_id}, "
+ out << "#{games.count} Games, "
+ out << "#{node_count} Nodes"
+ out << ">"
end
- private
+ alias :inspect :to_s
- # Adds a stringified node to the variable @savable_sgf.
- def write_node node=@root
- @savable_sgf << ";"
- unless node.properties.empty?
- properties = ""
- node.properties.each do |identity, property|
- new_property = property[1...-1]
- new_property.gsub!("]", "\\]") if identity == "C"
- properties += "#{identity.to_s}[#{new_property}]"
- end
- @savable_sgf << properties
- end
+ def to_str
+ SGF::Writer.new.stringify_tree_from @root
+ end
- case node.children.size
- when 0
- @savable_sgf << ")"
- when 1
- write_node node.children[0]
- else
- node.each_child do |child|
- @savable_sgf << "("
- write_node child
- end
- end
+ # Saves the Tree as an SGF file. Takes a filename as argument.
+ def save filename
+ SGF::Writer.new.save(@root, filename)
end
- # Traverse the tree in preorder fashion, starting with the @root node if
- # no node is given, and activating the passed block on each.
- def preorder node=@root, &block
- # stop processing if the block returns false
- if yield node then
- node.each_child do |child|
- preorder(child, &block)
- end
+ private
+
+ def node_count
+ count = 0
+ games.each { |game| count += game.node_count }
+ count
+ end
+
+ def populate_game_array
+ games = []
+ @root.children.each do |first_node_of_gametree|
+ games << Game.new(first_node_of_gametree)
end
+ games
end
def method_missing method_name, *args
output = @root.children[0].properties[method_name]
super(method_name, args) if output.nil?
output
end
- end
-
-end
+ end # Tree
+end # SGF