Class: SGF::Gametree

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sgf/properties.rb,
lib/sgf/gametree.rb

Overview

Constant Summary collapse

PROPERTIES =
{
 annotator:         "AN",
 black_octisquares: "BO", # Octi
 black_rank:        "BR",
 black_team:        "BT",
 copyright:         "CP",
 date:              "DT",
 event:             "EV",
 game_content:      "GC",
 handicap:          "HA", # Go
 initial_position:  "IP", # Lines of Action
 invert_y_axis:     "IY", # Lines of Action
 komi:              "KM", # Go
 match_information: "MI", # Backgammon
 name:              "GN",
 prongs:            "NP", # Octi
 reserve:           "NR", # Octi
 superprongs:       "NS", # Octi
 opening:           "ON",
 overtime:          "OT",
 black_player:      "PB",
 place:             "PC",
 puzzle:            "PZ",
 white_player:      "PW",
 result:            "RE",
 round:             "RO",
 rules:             "RU",
 setup_type:        "SU", # Lines of Action
 source:            "SO",
 time:              "TM",
 data_entry:        "US",
 white_octisquares: "WO", # Octi
 white_rank:        "WR",
 white_team:        "WT"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Gametree

Takes a SGF::Node as an argument. It will be a problem if that node isn’t really the first node of of a game (ie: no FF property)

Raises:

  • (ArgumentError)


18
19
20
21
22
# File 'lib/sgf/gametree.rb', line 18

def initialize node
  raise ArgumentError, "Expected SGF::Node argument but received #{node.class}" unless node.instance_of? SGF::Node
  @root = node
  @current_node = node
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object (private)



59
60
61
62
63
# File 'lib/sgf/gametree.rb', line 59

def method_missing method_name, *args
  human_readable_method = method_name.to_s.downcase
  sgf_identity = SGF::Gametree::PROPERTIES[human_readable_method]
  return @root[sgf_identity] || raise(SGF::NoIdentityError, "This gametree does not have '#{human_readable_method}' available")
end

Instance Attribute Details

#current_nodeObject

Returns the value of attribute current_node.



14
15
16
# File 'lib/sgf/gametree.rb', line 14

def current_node
  @current_node
end

#rootObject (readonly)

Returns the value of attribute root.



12
13
14
# File 'lib/sgf/gametree.rb', line 12

def root
  @root
end

Instance Method Details

#each(&block) ⇒ Object

Iterate through all the nodes in preorder fashion



30
31
32
# File 'lib/sgf/gametree.rb', line 30

def each &block
  @root.each(&block)
end

#inspectObject



34
35
36
# File 'lib/sgf/gametree.rb', line 34

def inspect
  "<SGF::Gametree:#{object_id}>"
end

#next_nodeObject

A simple way to go to the next node in the same branch of the tree



25
26
27
# File 'lib/sgf/gametree.rb', line 25

def next_node
  @current_node = @current_node.children[0]
end

#slice(range) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sgf/gametree.rb', line 42

def slice range
  new_root = nil
  each do |node|
    if node.depth == range.begin
      new_root = node.dup
      break
    end
  end

  new_root ||= @root.dup
  new_root.depth = 0
  new_root.parent = nil
  SGF::Gametree.new new_root
end

#to_sObject



38
39
40
# File 'lib/sgf/gametree.rb', line 38

def to_s
  SGF::Writer.new.stringify_tree_from @root
end