Permalink
Browse files

Rename Tree to Collection and Game to Gametree. Preparing for 3.0.0

  • Loading branch information...
1 parent 86ade54 commit f8f282e4ba480142b52fe13a5a36dcfe40cc3a56 @Trevoke committed Mar 26, 2012
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- SgfParser (2.0.0)
+ SgfParser (2.1.0)
GEM
remote: http://rubygems.org/
View
@@ -11,14 +11,11 @@ Gem::Specification.new do |s|
s.date = %q{2011-08-01}
s.summary = %q{A library that parses and saves SGF (Smart Game Format) files.}
s.description = %q{SGF::Parser does standard stream parsing of the SGF file, instead of using an AG or some other auto-generated newfangled parser stuff. It is therefore faster to use, and hopefully will also be easier to use. Feedback helps :)}
- s.extra_rdoc_files = [
- "LICENSE",
- "README.rdoc"
- ]
+ s.extra_rdoc_files = %w(LICENSE README.rdoc)
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
- s.require_paths = ["lib"]
+ s.require_paths = %w(lib)
s.add_development_dependency 'rspec'
s.add_development_dependency 'rcov'
View
@@ -9,5 +9,5 @@
end
parser = SGF::Parser.new
-tree = parser.parse ARGV[0]
-tree.save ARGV[1]
+collection = parser.parse ARGV[0]
+collection.save ARGV[1]
@@ -3,9 +3,9 @@
parser = SGF::Parser.new
Dir['*.sgf'].each do |file|
- tree = parser.parse File.read(file)
- tree.games.each do |game|
+ collection = parser.parse File.read(file)
+ collection.gametrees.each do |game|
puts "White player: #{game.white_player} and Black player: #{game.black_player}"
end
- tree.save file #Because may as well indent the files while I'm here, right?
+ collection.save file #Because may as well indent the files while I'm here, right?
end
View
@@ -5,7 +5,7 @@
require 'sgf/version'
require 'sgf/properties'
require 'sgf/node'
-require 'sgf/tree'
+require 'sgf/collection'
require 'sgf/parser'
-require 'sgf/game'
+require 'sgf/gametree'
require 'sgf/writer'
@@ -1,8 +1,8 @@
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!
+ #Collection 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
+ class Collection
include Enumerable
attr_accessor :root, :current_node, :errors
@@ -14,27 +14,27 @@ def initialize
end
def each
- games.each { |game| game.each { |node| yield node } }
+ gametrees.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
+ def == other_collection
one = []
two = []
each { |node| one << node }
- other_tree.each { |node| two << node }
+ other_collection.each { |node| two << node }
one == two
end
#Returns an array of the Game objects in this tree.
- def games
+ def gametrees
populate_game_array
end
def to_s
- out = "#<SGF::Tree:#{self.object_id}, "
- out << "#{games.count} Games, "
+ out = "#<SGF::Collection:#{self.object_id}, "
+ out << "#{gametrees.count} Games, "
out << "#{node_count} Nodes"
out << ">"
end
@@ -45,7 +45,7 @@ def to_str
SGF::Writer.new.stringify_tree_from @root
end
- # Saves the Tree as an SGF file. Takes a filename as argument.
+ # Saves the Collection as an SGF file. Takes a filename as argument.
def save filename
SGF::Writer.new.save(@root, filename)
end
@@ -54,14 +54,14 @@ def save filename
def node_count
count = 0
- games.each { |game| count += game.node_count }
+ gametrees.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)
+ @root.children.each do |first_node_of_tree|
+ games << Gametree.new(first_node_of_tree)
end
games
end
@@ -72,6 +72,6 @@ def method_missing method_name, *args
output
end
- end # Tree
+ end # Collection
end # SGF
@@ -1,9 +1,9 @@
module SGF
- class Game
+ class Gametree
include Enumerable
- SGF::Game::PROPERTIES.each do |human_readable_method, sgf_identity|
+ SGF::Gametree::PROPERTIES.each do |human_readable_method, sgf_identity|
define_method(human_readable_method.to_sym) do
@root[sgf_identity] ? @root[sgf_identity] : raise(SGF::NoIdentityError)
end
@@ -49,7 +49,7 @@ def to_str
def method_missing method_name, *args
human_readable_identity = method_name.to_s.downcase
- identity = SGF::Game::PROPERTIES[human_readable_identity]
+ identity = SGF::Gametree::PROPERTIES[human_readable_identity]
return @root[identity] if identity
super(method_name, args)
end
View
@@ -24,16 +24,16 @@ def parent
@parent
end
+ #Set the given node as a parent and self as one of that node's children
def parent= parent
- if @parent
- @parent.children.delete self
- end
+ @parent.children.delete self if @parent
case @parent = parent
- when nil then @depth = 0
+ when nil
+ @depth = 0
else
@parent.children << self
- @depth = parent.depth + 1
+ @depth = @parent.depth + 1
end
update_depth_of_children
end
View
@@ -2,9 +2,9 @@
module SGF
- #The parser returns a SGF::Tree representation of the SGF file
+ #The parser returns a SGF::Collection representation of the SGF file
#parser = SGF::Parser.new
- #tree = parser.parse sgf_in_string_form
+ #collection = parser.parse sgf_in_string_form
class Parser
NEW_NODE = ";"
@@ -13,16 +13,16 @@ class Parser
NODE_DELIMITERS = [NEW_NODE].concat BRANCHING
LIST_IDENTITIES = %w(AW AB AE AR CR DD LB LN MA SL SQ TR VW TB TW)
- # This takes as argument an SGF and returns an SGF::Tree object
+ # This takes as argument an SGF and returns an SGF::Collection object
# It accepts a local path (String), a stringified SGF (String),
# or a file handler (File).
# The second argument is optional, in case you don't want this to raise errors.
# You probably shouldn't use it, but who's gonna stop you?
def parse sgf, strict_parsing = true
@strict_parsing = strict_parsing
@stream = streamably_stringify sgf
- @tree = Tree.new
- @root = @tree.root
+ @collection = Collection.new
+ @root = @collection.root
@current_node = @root
@branches = []
until @stream.eof?
@@ -36,7 +36,7 @@ def parse sgf, strict_parsing = true
else next
end
end
- @tree
+ @collection
end
private
@@ -2,7 +2,7 @@ module SGF
# http://www.red-bean.com/sgf/proplist.html
- class Game
+ class Gametree
PROPERTIES = {"annotator"=>"AN",
"black_octisquares"=>"BO", #Octi
View
@@ -1,3 +1,3 @@
module SGF
- VERSION = "2.1.0"
+ VERSION = "3.0.0alpha"
end
View
@@ -9,6 +9,7 @@ def stringify_tree_from root_node
# Takes a node and a filename as arguments
def save(root_node, filename)
+ #TODO - accept any I/O object
stringify_tree_from root_node
File.open(filename, 'w') { |f| f << @sgf }
@@ -0,0 +1,36 @@
+require 'spec_helper'
+
+describe SGF::Collection do
+
+ before :each do
+ @collection = get_collection_from 'spec/data/ff4_ex.sgf'
+ end
+
+ it "should have two trees" do
+ @collection.gametrees.size.should eq 2
+ end
+
+ it "should have a decent inspect" do
+ inspect = @collection.inspect
+ inspect.should match /SGF::Collection/
+ inspect.should match /#{@collection.object_id}/
+ inspect.should match /2 Games/
+ inspect.should match /62 Nodes/
+ end
+
+ it "should use preorder traversal for each" do
+ @collection = get_collection_from 'spec/data/example1.sgf'
+ array = []
+ @collection.each { |node| array << node }
+ array[0].c.should eq "root"
+ array[1].c.should eq "a"
+ array[2].c.should eq "b"
+ end
+
+ it "should load a file properly" do
+ @collection.should be_an_instance_of SGF::Collection
+ @collection.root.children.size.should eq 2
+ @collection.root.children[0].children.size.should eq 5
+ end
+
+end
@@ -1,15 +1,15 @@
require 'spec_helper'
-describe "SGF::Game" do
+describe SGF::Gametree do
it "should hold the first node of the game" do
game = get_first_game_from 'spec/data/ff4_ex.sgf'
game.current_node["FF"].should eq "4"
end
it "should throw up if initialized with a non-Node argument" do
- expect { SGF::Game.new("I am a string") }.to raise_error(ArgumentError)
- expect { SGF::Game.new(SGF::Node.new) }.to_not raise_error(ArgumentError)
+ expect { SGF::Gametree.new("I am a string") }.to raise_error(ArgumentError)
+ expect { SGF::Gametree.new(SGF::Node.new) }.to_not raise_error(ArgumentError)
end
it "should have the expected game-level information" do
@@ -60,7 +60,7 @@
it "should go through all nodes, even if block returns 'nil' (puts, anyone?)" do
root = SGF::Node.new FF: "4", PB: "Me", PW: "You"
- game = SGF::Game.new root
+ game = SGF::Gametree.new root
root.add_children SGF::Node.new(B: "dd")
nodes = []
game.each { |node| nodes << node; nil }
Oops, something went wrong.

0 comments on commit f8f282e

Please sign in to comment.