Class: SGF
- Inherits:
-
Object
- Object
- SGF
- Defined in:
- lib/sgf.rb
Constant Summary collapse
- BLACK =
"B"
- WHITE =
"W"
Instance Attribute Summary collapse
-
#comment_buffer ⇒ Object
Returns the value of attribute comment_buffer.
-
#focus ⇒ Object
Returns the value of attribute focus.
-
#move_list ⇒ Object
Returns the value of attribute move_list.
-
#property(symbol) ⇒ Object
Returns the value of attribute property.
Class Method Summary collapse
Instance Method Summary collapse
- #add_comment(comment) ⇒ Object
-
#add_move(node) ⇒ Object
TODO objetify node.
- #add_time(player, time) ⇒ Object
- #hash_to_comment(hash) ⇒ Object
-
#initialize(moves = "", properties = {}) ⇒ SGF
constructor
A new instance of SGF.
- #last_node_by_player(player) ⇒ Object
- #last_play_color ⇒ Object
- #last_two_moves_are_pass? ⇒ Boolean
- #load_file(filename) ⇒ Object
- #load_from_string(input) ⇒ Object
- #move_by_number(index) ⇒ Object
- #move_list_with_comments ⇒ Object
- #nodify_move_list(moves) ⇒ Object
-
#parse_comments!(comments) ⇒ Object
takes a hash and inputs the contents into the nodes.
- #properties ⇒ Object
- #properties=(arg) ⇒ Object
- #time_left(player) ⇒ Object
- #to_s ⇒ Object
- #undo ⇒ Object
-
#validate_coordinate(x, y) ⇒ Object
light validation to make sure the input is not totally bs.
- #validate_node_format(node) ⇒ Object
- #write_property(symbol, value) ⇒ Object
Constructor Details
#initialize(moves = "", properties = {}) ⇒ SGF
Returns a new instance of SGF.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/sgf.rb', line 9 def initialize(moves="", properties={}) moves ||= "" @config = ConfigNode.new @focus = @config nodify_move_list(moves) unless moves.empty? @comment_buffer = "" @size = properties[:size] properties.keys.each {|k| @config.write_property(k,properties[k]) } @config.write_property(:handicap, properties[:handicap]) end |
Instance Attribute Details
#comment_buffer ⇒ Object
Returns the value of attribute comment_buffer.
7 8 9 |
# File 'lib/sgf.rb', line 7 def comment_buffer @comment_buffer end |
#focus ⇒ Object
Returns the value of attribute focus.
7 8 9 |
# File 'lib/sgf.rb', line 7 def focus @focus end |
#move_list ⇒ Object
Returns the value of attribute move_list.
7 8 9 |
# File 'lib/sgf.rb', line 7 def move_list @move_list end |
#property(symbol) ⇒ Object
Returns the value of attribute property.
7 8 9 |
# File 'lib/sgf.rb', line 7 def property @property end |
Class Method Details
.handi_node(size, handicap) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/sgf.rb', line 163 def self.handi_node(size,handicap) case size.to_i when 19 case handicap when 2 return "HA[2]AB[dd][pp]" when 3 return "HA[3]AB[dd][dp][pd]" when 4 return "HA[4]AB[dd][pd][dp][pp]" when 5 return "HA[5]AB[dd][pd][dp][pp][jj]" when 6 return "HA[6]AB[dd][pd][dp][pp][dj][pj]" when 7 return "HA[7]AB[dd][pd][dp][pp][dj][pj][jj]" when 8 return "HA[8]AB[dd][jd][pd][dj][pj][dp][jp][pp]" when 9 return "HA[9]AB[dd][jd][pd][dj][jj][pj][dp][jp][pp]" else raise "Invalid handicap setting #{handicap}" end when 13 case handicap when 2 return "HA[2]AB[dd][jj]" when 3 return "HA[3]AB[dd][dj][jd]" when 4 return "HA[4]AB[dd][jd][dj][jj]" when 5 return "HA[5]AB[dd][jd][dj][gg][jj]" when 6 return "HA[6]AB[dd][jd][dj][jj][dg][jg]" when 7 return "HA[7]AB[dd][jd][dj][jj][dg][jg][gg]" when 8 return "HA[8]AB[dd][jd][dj][gj][jj][jg][gd][dg]" when 9 return "HA[9]AB[dd][jd][dj][gj][jj][jg][gg][gd][dg]" else raise "Invalid handicap setting #{handicap}" end when 9 case handicap when 2 return "HA[2]AB[cc][gg]" when 3 return "HA[3]AB[cc][cg][gg]" when 4 return "HA[4]AB[cc][gg][cg][gc]" when 5 return "HA[5]AB[cc][gg][cg][gc][ee]" when 6 return "HA[6]AB[cc][gg][cg][gc][ee][ge]" when 7 return "HA[7]AB[cc][gg][cg][gc][ee][ge][ee]" when 8 return "HA[8]AB[cc][gc][cg][gg][ce][ge][ec][eg]" when 9 return "HA[9]AB[cc][gc][cg][gg][ce][ge][ec][ee][eg]" else raise "Invalid handicap setting #{handicap}" end end raise "Invalid handicap setting Size: #{size} and handicap #{handicap}" end |
Instance Method Details
#add_comment(comment) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/sgf.rb', line 42 def add_comment(comment) if @focus @focus.add_comment(comment) else @config.add_comment(comment) end move_list end |
#add_move(node) ⇒ Object
TODO objetify node
33 34 35 36 |
# File 'lib/sgf.rb', line 33 def add_move(node) #TODO objetify node @focus = Node.new(@focus,node) move_list end |
#add_time(player, time) ⇒ Object
143 144 145 146 |
# File 'lib/sgf.rb', line 143 def add_time(player,time) ln = last_node_by_player(player) ln.time_left= (ln.time_left + time) end |
#hash_to_comment(hash) ⇒ Object
61 62 63 64 |
# File 'lib/sgf.rb', line 61 def hash_to_comment(hash) raise "invalid hash" unless hash["user"] && hash["rank"] && hash["message"] "#{hash["user"]}#{hash["rank"]}: #{hash["message"]}" end |
#last_node_by_player(player) ⇒ Object
148 149 150 151 152 153 154 155 |
# File 'lib/sgf.rb', line 148 def last_node_by_player(player) return if @focus == @config if @focus.color == player return @focus elsif @focus.parent @focus.parent end end |
#last_play_color ⇒ Object
38 39 40 |
# File 'lib/sgf.rb', line 38 def last_play_color @focus != @config && @focus.color end |
#last_two_moves_are_pass? ⇒ Boolean
20 21 22 23 24 25 |
# File 'lib/sgf.rb', line 20 def last_two_moves_are_pass? if @focus && @focus.parent return @focus.pass_node? && @focus.parent.pass_node? end false end |
#load_file(filename) ⇒ Object
102 103 104 105 106 107 108 109 110 |
# File 'lib/sgf.rb', line 102 def load_file(filename) sgf ="" File.open(filename, 'r') do |file| while (line = file.gets) sgf += line end load_from_string(sgf) end end |
#load_from_string(input) ⇒ Object
111 112 113 114 115 |
# File 'lib/sgf.rb', line 111 def load_from_string(input) properties= input.split(";")[1] @focus = @config = ConfigNode.new(properties) #will process this later nodify_move_list(input.gsub(properties, "").chomp[2..-2]) end |
#move_by_number(index) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/sgf.rb', line 74 def move_by_number(index) index = index.to_i return if (index < 0) node = @config.children.first while(index >0) node = node.children.first index -= 1 end node end |
#move_list_with_comments ⇒ Object
70 71 72 |
# File 'lib/sgf.rb', line 70 def move_list_with_comments @config.children.first.to_s end |
#nodify_move_list(moves) ⇒ Object
27 28 29 30 31 |
# File 'lib/sgf.rb', line 27 def nodify_move_list(moves) moves.split(";").each do |txt| add_move(";"+txt) unless txt.empty? end end |
#parse_comments!(comments) ⇒ Object
takes a hash and inputs the contents into the nodes
51 52 53 54 55 56 57 58 59 |
# File 'lib/sgf.rb', line 51 def parse_comments!(comments) comments.each do |key, value| if (key.to_i == 0) value.each {|v| @config.add_comment(hash_to_comment(v))} next end value.each{ |v| move_by_number(key.to_i-1) && move_by_number(key.to_i-1).add_comment(hash_to_comment(v))} end end |
#properties ⇒ Object
117 118 119 |
# File 'lib/sgf.rb', line 117 def properties @config.to_s end |
#properties=(arg) ⇒ Object
121 122 123 |
# File 'lib/sgf.rb', line 121 def properties=(arg) @config.node_text = arg end |
#time_left(player) ⇒ Object
137 138 139 140 141 |
# File 'lib/sgf.rb', line 137 def time_left(player) raise "Invalid input #{player}. W or B expected" unless player == "B" || player == "W" ln = last_node_by_player(player) ln && ln.time_left end |
#to_s ⇒ Object
133 134 135 |
# File 'lib/sgf.rb', line 133 def to_s "(#{@config.to_s})" end |
#undo ⇒ Object
157 158 159 160 161 |
# File 'lib/sgf.rb', line 157 def undo to_del = @focus @focus = @focus.parent @focus.children.delete(to_del) end |
#validate_coordinate(x, y) ⇒ Object
light validation to make sure the input is not totally bs. only makes sure the coordinate is in the board
86 87 88 89 90 91 92 |
# File 'lib/sgf.rb', line 86 def validate_coordinate(x, y) lower_boundary = 97 upper_boundary = 97+ @size.to_i valid_y_axis = y.bytes.first <= upper_boundary && y.bytes.first >= lower_boundary valid_x_axis = x.bytes.first <= upper_boundary && x.bytes.first >= lower_boundary throw "Invalid coordinate #{x},#{y}" unless valid_y_axis && valid_x_axis end |
#validate_node_format(node) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/sgf.rb', line 94 def validate_node_format(node) valid = node.match(/;[BW]\[(|[a-z][a-z])\]/) if node.include?("BL") || node.include?("WL") valid = valid && node.match(/[BW]L\[\d{0,6}.\d{3}\]/) end raise "#{node} is invalid node format" unless valid end |
#write_property(symbol, value) ⇒ Object
129 130 131 |
# File 'lib/sgf.rb', line 129 def write_property(symbol, value) @config.write_property(symbol, value) end |