Class: KantanSgf::Sgf
- Inherits:
-
Object
- Object
- KantanSgf::Sgf
- Defined in:
- lib/kantan-sgf/sgf.rb
Instance Attribute Summary collapse
-
#move_list ⇒ Object
Returns the value of attribute move_list.
-
#properties ⇒ Object
Returns the value of attribute properties.
Instance Method Summary collapse
- #board_size ⇒ Object
- #game_date ⇒ Object
- #game_event ⇒ Object
- #game_name ⇒ Object
- #game_place ⇒ Object
- #game_round ⇒ Object
- #game_rules ⇒ Object
- #handicap ⇒ Object
-
#initialize(file) ⇒ Sgf
constructor
A new instance of Sgf.
- #komi ⇒ Object
- #parse ⇒ Object
- #player_black ⇒ Object
- #player_time ⇒ Object
- #player_white ⇒ Object
- #rank_black ⇒ Object
- #rank_white ⇒ Object
- #result ⇒ Object
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_list ⇒ Object
Returns the value of attribute move_list.
11 12 13 |
# File 'lib/kantan-sgf/sgf.rb', line 11 def move_list @move_list end |
#properties ⇒ Object
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_size ⇒ Object
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_date ⇒ Object
107 108 109 |
# File 'lib/kantan-sgf/sgf.rb', line 107 def game_date return @properties["DT"] end |
#game_event ⇒ Object
111 112 113 |
# File 'lib/kantan-sgf/sgf.rb', line 111 def game_event return @properties["EV"] end |
#game_name ⇒ Object
127 128 129 |
# File 'lib/kantan-sgf/sgf.rb', line 127 def game_name return @properties["GN"] end |
#game_place ⇒ Object
119 120 121 |
# File 'lib/kantan-sgf/sgf.rb', line 119 def game_place return @properties["PC"] end |
#game_round ⇒ Object
115 116 117 |
# File 'lib/kantan-sgf/sgf.rb', line 115 def game_round return @properties["RO"] end |
#game_rules ⇒ Object
123 124 125 |
# File 'lib/kantan-sgf/sgf.rb', line 123 def game_rules return @properties["RU"] end |
#handicap ⇒ Object
99 100 101 |
# File 'lib/kantan-sgf/sgf.rb', line 99 def handicap return @properties["HA"] end |
#komi ⇒ Object
91 92 93 |
# File 'lib/kantan-sgf/sgf.rb', line 91 def komi return @properties["KM"] end |
#parse ⇒ Object
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 = data.pop .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_black ⇒ Object
71 72 73 |
# File 'lib/kantan-sgf/sgf.rb', line 71 def player_black return @properties["PB"] end |
#player_time ⇒ Object
103 104 105 |
# File 'lib/kantan-sgf/sgf.rb', line 103 def player_time return @properties["TM"] end |
#player_white ⇒ Object
75 76 77 |
# File 'lib/kantan-sgf/sgf.rb', line 75 def player_white return @properties["PW"] end |
#rank_black ⇒ Object
79 80 81 |
# File 'lib/kantan-sgf/sgf.rb', line 79 def rank_black return @properties["BR"] end |
#rank_white ⇒ Object
83 84 85 |
# File 'lib/kantan-sgf/sgf.rb', line 83 def rank_white return @properties["WR"] end |
#result ⇒ Object
95 96 97 |
# File 'lib/kantan-sgf/sgf.rb', line 95 def result return @properties["RE"] end |