Class: KantanSgf::Sgf

Inherits:
Object
  • Object
show all
Defined in:
lib/kantan-sgf/sgf.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Sgf

Returns a new instance of Sgf.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kantan-sgf/sgf.rb', line 13

def initialize(file)
  @file = file
  @data = nil
  @properties = {}

  @symbol_table = {}
  a = 'a'
  for i in 0..18
    @symbol_table.store(a, i)
    a = a.succ
  end

  @move_list = []
end

Instance Attribute Details

#move_listObject

Returns the value of attribute move_list.



11
12
13
# File 'lib/kantan-sgf/sgf.rb', line 11

def move_list
  @move_list
end

#propertiesObject

Returns the value of attribute properties.



11
12
13
# File 'lib/kantan-sgf/sgf.rb', line 11

def properties
  @properties
end

Instance Method Details

#board_sizeObject



87
88
89
# File 'lib/kantan-sgf/sgf.rb', line 87

def board_size
  return @properties.include?("SZ") ? @properties["SZ"].to_i : 19
end

#game_dateObject



107
108
109
# File 'lib/kantan-sgf/sgf.rb', line 107

def game_date
  return @properties["DT"]
end

#game_eventObject



111
112
113
# File 'lib/kantan-sgf/sgf.rb', line 111

def game_event
  return @properties["EV"]
end

#game_nameObject



127
128
129
# File 'lib/kantan-sgf/sgf.rb', line 127

def game_name
  return @properties["GN"]
end

#game_placeObject



119
120
121
# File 'lib/kantan-sgf/sgf.rb', line 119

def game_place
  return @properties["PC"]
end

#game_roundObject



115
116
117
# File 'lib/kantan-sgf/sgf.rb', line 115

def game_round
  return @properties["RO"]
end

#game_rulesObject



123
124
125
# File 'lib/kantan-sgf/sgf.rb', line 123

def game_rules
  return @properties["RU"]
end

#handicapObject



99
100
101
# File 'lib/kantan-sgf/sgf.rb', line 99

def handicap
  return @properties["HA"]
end

#komiObject



91
92
93
# File 'lib/kantan-sgf/sgf.rb', line 91

def komi
  return @properties["KM"]
end

#parseObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/kantan-sgf/sgf.rb', line 28

def parse
  f = File.open(@file)
  @data = f.read
  f.close
  
  @sgf = SgfGrammarParser.new
  results = @sgf.parse(@data)
  raise "Parsing failed due to [#{@sgf.failure_reason}]." if results.nil?
  
  # Pull the data out of the Treetop grammar parser
  data = results.value
  
  # Header info
  header = data.shift
  header.each do |chunk|
    @properties.store(chunk[:property], chunk[:data])
  end
  # Footer info
  footer = data.pop
  footer.each do |chunk|
    @properties.store(chunk[:property], chunk[:data])
  end
  # Moves
  data.each do |chunk|
    move = {}
    chunk.each do |info|
      case info[:property]
        when 'B', 'W'
          move[:color] = info[:property]
          if !info[:data].empty?
            move[:x] = @symbol_table[info[:data][0].chr]
            move[:y] = @symbol_table[info[:data][1].chr]
          end
        when 'BL', 'WL'
          move[:time] = info[:data]
        when 'OB', 'OW'
          move[:ot_stones] = info[:data]
      end
    end
    @move_list << move
  end
end

#player_blackObject



71
72
73
# File 'lib/kantan-sgf/sgf.rb', line 71

def player_black
  return @properties["PB"]
end

#player_timeObject



103
104
105
# File 'lib/kantan-sgf/sgf.rb', line 103

def player_time
  return @properties["TM"]
end

#player_whiteObject



75
76
77
# File 'lib/kantan-sgf/sgf.rb', line 75

def player_white
  return @properties["PW"]
end

#rank_blackObject



79
80
81
# File 'lib/kantan-sgf/sgf.rb', line 79

def rank_black
  return @properties["BR"]
end

#rank_whiteObject



83
84
85
# File 'lib/kantan-sgf/sgf.rb', line 83

def rank_white
  return @properties["WR"]
end

#resultObject



95
96
97
# File 'lib/kantan-sgf/sgf.rb', line 95

def result
  return @properties["RE"]
end