kantan-sgf 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README +17 -13
- data/lib/kantan-sgf/sgf.rb +41 -8
- data/lib/kantan-sgf/version.rb +1 -1
- metadata +1 -1
data/README
CHANGED
@@ -1,19 +1,15 @@
|
|
1
1
|
~~ ABOUT ~~~~~*
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
Author: Brian "bojo" Jones
|
4
|
+
Email: mojobojo@gmail.com
|
5
5
|
|
6
|
-
|
7
|
-
thing as a dedicated Ruby SGF project.
|
8
|
-
I know because I've been looking for
|
9
|
-
a few years.
|
6
|
+
KANTAN means "simple" in Japanese.
|
10
7
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
and wrote this.
|
8
|
+
Due to the lack of standalone Ruby SGF parsers,
|
9
|
+
I ended up writing this project to help with a
|
10
|
+
few other side projects. Hopefully it benefits
|
11
|
+
other people interested in using Ruby for their
|
12
|
+
SGF parsing related projects.
|
17
13
|
|
18
14
|
It doesn't do node parsing (yet).
|
19
15
|
I just upgraded it using Treetop grammar
|
@@ -48,7 +44,15 @@ You pretty much run it like so:
|
|
48
44
|
end
|
49
45
|
|
50
46
|
Note that the move data is stored as:
|
47
|
+
* move: The current move number
|
51
48
|
* color: 'B' or 'W'
|
52
49
|
* x, y: Integer value from 0..board_size - 1
|
53
50
|
* time: Clock time left
|
54
|
-
* ot_stones: Overtime stones
|
51
|
+
* ot_stones: Overtime stones
|
52
|
+
* pass: true
|
53
|
+
|
54
|
+
Properties can be accessed directly via
|
55
|
+
sgf.properties["prop_name"].
|
56
|
+
|
57
|
+
Comments can be access via
|
58
|
+
sgf.comments # {:move, :data}
|
data/lib/kantan-sgf/sgf.rb
CHANGED
@@ -8,7 +8,7 @@ require "#{dir}/../grammar/sgf-grammar"
|
|
8
8
|
module KantanSgf
|
9
9
|
class Sgf
|
10
10
|
|
11
|
-
attr_accessor :move_list, :properties
|
11
|
+
attr_accessor :move_list, :properties, :comments
|
12
12
|
|
13
13
|
def initialize(file)
|
14
14
|
@file = file
|
@@ -23,6 +23,7 @@ module KantanSgf
|
|
23
23
|
end
|
24
24
|
|
25
25
|
@move_list = []
|
26
|
+
@comments = {}
|
26
27
|
end
|
27
28
|
|
28
29
|
def parse
|
@@ -37,31 +38,39 @@ module KantanSgf
|
|
37
38
|
# Pull the data out of the Treetop grammar parser
|
38
39
|
data = results.value
|
39
40
|
|
40
|
-
# Header info
|
41
|
+
# Header info - Typically the first chunk is game data
|
41
42
|
header = data.shift
|
42
43
|
header.each do |chunk|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
@properties.store(chunk[:property], chunk[:data])
|
44
|
+
if chunk[:property] == "C"
|
45
|
+
@comments.store("0", chunk[:data])
|
46
|
+
else
|
47
|
+
@properties.store(chunk[:property], chunk[:data])
|
48
|
+
end
|
49
49
|
end
|
50
50
|
# Moves
|
51
|
+
move_count = 0
|
51
52
|
data.each do |chunk|
|
53
|
+
move_count += 1
|
52
54
|
move = {}
|
53
55
|
chunk.each do |info|
|
56
|
+
move[:move] = move_count
|
54
57
|
case info[:property]
|
55
58
|
when 'B', 'W'
|
56
59
|
move[:color] = info[:property]
|
57
60
|
if !info[:data].empty?
|
58
61
|
move[:x] = @symbol_table[info[:data][0].chr]
|
59
62
|
move[:y] = @symbol_table[info[:data][1].chr]
|
63
|
+
else
|
64
|
+
move[:pass] = true
|
60
65
|
end
|
61
66
|
when 'BL', 'WL'
|
62
67
|
move[:time] = info[:data]
|
63
68
|
when 'OB', 'OW'
|
64
69
|
move[:ot_stones] = info[:data]
|
70
|
+
when 'C'
|
71
|
+
@comments.store(move_count.to_s, info[:data])
|
72
|
+
else
|
73
|
+
@properties.store(info[:property], info[:data])
|
65
74
|
end
|
66
75
|
end
|
67
76
|
@move_list << move
|
@@ -127,6 +136,30 @@ module KantanSgf
|
|
127
136
|
def game_name
|
128
137
|
return @properties["GN"]
|
129
138
|
end
|
139
|
+
|
140
|
+
def game_time
|
141
|
+
return @properties["TM"]
|
142
|
+
end
|
143
|
+
|
144
|
+
def game_overtime
|
145
|
+
return @properties["OT"]
|
146
|
+
end
|
147
|
+
|
148
|
+
def territory_black
|
149
|
+
return @properties["TB"]
|
150
|
+
end
|
151
|
+
|
152
|
+
def territory_white
|
153
|
+
return @properties["TW"]
|
154
|
+
end
|
155
|
+
|
156
|
+
def sgf_version
|
157
|
+
return @properties["FF"]
|
158
|
+
end
|
159
|
+
|
160
|
+
def sgf_application
|
161
|
+
return @properties["AP"]
|
162
|
+
end
|
130
163
|
|
131
164
|
end
|
132
165
|
end
|
data/lib/kantan-sgf/version.rb
CHANGED