jkf 0.5.0 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.envrc +1 -0
- data/.rubocop.yml +67 -0
- data/.rubocop_todo.yml +1 -454
- data/.yardopts +4 -0
- data/CHANGELOG.md +27 -0
- data/Gemfile +12 -11
- data/Guardfile +3 -3
- data/README.en.md +27 -23
- data/README.md +32 -21
- data/Rakefile +4 -4
- data/bench.rb +12 -0
- data/bin/console +4 -4
- data/jkf.gemspec +16 -15
- data/lib/jkf/converter/base.rb +12 -10
- data/lib/jkf/converter/csa.rb +141 -150
- data/lib/jkf/converter/ki2.rb +94 -92
- data/lib/jkf/converter/kif.rb +105 -99
- data/lib/jkf/converter/kifuable.rb +160 -160
- data/lib/jkf/converter.rb +6 -8
- data/lib/jkf/parser/base.rb +81 -95
- data/lib/jkf/parser/csa.rb +651 -654
- data/lib/jkf/parser/ki2.rb +331 -333
- data/lib/jkf/parser/kif.rb +467 -468
- data/lib/jkf/parser/kifuable.rb +511 -517
- data/lib/jkf/parser.rb +5 -7
- data/lib/jkf/version.rb +1 -2
- data/lib/jkf.rb +6 -6
- data/manifest.scm +3 -30
- data/po/all.pot +64 -48
- data/po/en.po +83 -82
- data/po/en.rdoc.po +580 -0
- metadata +18 -10
data/lib/jkf/converter/ki2.rb
CHANGED
@@ -1,113 +1,115 @@
|
|
1
|
-
module Jkf
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
module Jkf
|
2
|
+
module Converter
|
3
|
+
# KI2 Converter
|
4
|
+
class Ki2 < Base
|
5
|
+
include Kifuable
|
5
6
|
|
6
|
-
|
7
|
+
protected
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
def convert_root(jkf)
|
10
|
+
reset!
|
11
|
+
setup_players!(jkf)
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
13
|
+
result = ''
|
14
|
+
result += convert_header(jkf['header']) if jkf['header']
|
15
|
+
result += convert_initial(jkf['initial']) if jkf['initial']
|
16
|
+
result += @header2.join + "\n"
|
17
|
+
result += convert_moves(jkf['moves']) if jkf['moves']
|
18
|
+
unless @forks.empty?
|
19
|
+
result += "\n"
|
20
|
+
result += @forks.join("\n")
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
-
|
23
|
+
result
|
24
|
+
end
|
24
25
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
26
|
+
def convert_header(header)
|
27
|
+
header.filter_map do |(key, value)|
|
28
|
+
result = add_header(key, value)
|
29
|
+
if /\A[先後上下]手\Z/.match?(key)
|
30
|
+
nil
|
31
|
+
else
|
32
|
+
result
|
33
|
+
end
|
34
|
+
end.join
|
35
|
+
end
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
37
|
+
def add_header(key, value)
|
38
|
+
result = "#{key}:#{value}\n"
|
39
|
+
if /\A[先後上下]手\Z/.match?(key)
|
40
|
+
if /[先下]/.match?(key)
|
41
|
+
@header2.unshift result
|
42
|
+
else
|
43
|
+
@header2 << result
|
44
|
+
end
|
43
45
|
end
|
46
|
+
result
|
44
47
|
end
|
45
|
-
result
|
46
|
-
end
|
47
48
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
49
|
+
def convert_moves(moves, idx = 0)
|
50
|
+
result = ''
|
51
|
+
j = 0
|
52
|
+
before_split = ''
|
53
|
+
moves.each_with_index do |move, i|
|
54
|
+
if move['special']
|
55
|
+
# first_board+speical分を引く(-2)
|
56
|
+
result += convert_special_and_split(move, i + idx - 2)
|
57
|
+
else
|
58
|
+
result += before_split
|
59
|
+
if move['move']
|
60
|
+
j += 1
|
61
|
+
result_move, before_split = convert_move_and_split(move, j)
|
62
|
+
result += result_move
|
63
|
+
end
|
63
64
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
65
|
+
if move['comments']
|
66
|
+
unless result.end_with?("\n") || result.empty?
|
67
|
+
result += "\n"
|
68
|
+
before_split = ''
|
69
|
+
j = 0
|
70
|
+
end
|
71
|
+
result += convert_comments(move['comments'])
|
69
72
|
end
|
70
|
-
result += convert_comments(move["comments"])
|
71
|
-
end
|
72
73
|
|
73
|
-
|
74
|
+
@forks.unshift convert_forks(move['forks'], i + idx) if move['forks']
|
75
|
+
end
|
74
76
|
end
|
77
|
+
result
|
75
78
|
end
|
76
|
-
result
|
77
|
-
end
|
78
79
|
|
79
|
-
|
80
|
-
|
81
|
-
|
80
|
+
def convert_special_and_split(hash, index)
|
81
|
+
"\n" + convert_special(hash['special'], index)
|
82
|
+
end
|
82
83
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
84
|
+
def convert_move_and_split(move, num)
|
85
|
+
result = convert_move(move['move'])
|
86
|
+
split = if num % 6 == 0
|
87
|
+
"\n"
|
88
|
+
else
|
89
|
+
result.size == 4 ? ' ' * 4 : ' ' * 2
|
90
|
+
end
|
91
|
+
[result, split]
|
92
|
+
end
|
92
93
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
94
|
+
def convert_move(move)
|
95
|
+
result = move['color'] == 0 ? '▲' : '△'
|
96
|
+
result += convert_piece_with_pos(move)
|
97
|
+
result += csa2relative(move['relative']) if move['relative']
|
98
|
+
result
|
99
|
+
end
|
99
100
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
101
|
+
def csa2relative(relative)
|
102
|
+
case relative
|
103
|
+
when 'L' then '左'
|
104
|
+
when 'C' then '直'
|
105
|
+
when 'R' then '右'
|
106
|
+
when 'U' then '上'
|
107
|
+
when 'M' then '寄'
|
108
|
+
when 'D' then '引'
|
109
|
+
when 'H' then '打'
|
110
|
+
else
|
111
|
+
''
|
112
|
+
end
|
111
113
|
end
|
112
114
|
end
|
113
115
|
end
|
data/lib/jkf/converter/kif.rb
CHANGED
@@ -1,120 +1,126 @@
|
|
1
|
-
module Jkf
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
module Jkf
|
2
|
+
module Converter
|
3
|
+
# KIF Converter
|
4
|
+
class Kif < Base
|
5
|
+
protected
|
5
6
|
|
6
|
-
|
7
|
+
include Kifuable
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
def convert_root(jkf)
|
10
|
+
reset!
|
11
|
+
setup_players!(jkf)
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
13
|
+
result = ''
|
14
|
+
result += convert_header(jkf['header'], jkf) if jkf['header']
|
15
|
+
result += convert_initial(jkf['initial']) if jkf['initial']
|
16
|
+
result += @header2.join
|
17
|
+
result += "手数----指手---------消費時間--\n"
|
18
|
+
result += convert_moves(jkf['moves'])
|
19
|
+
unless @forks.empty?
|
20
|
+
result += "\n"
|
21
|
+
result += @forks.join("\n")
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
24
|
+
result
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
27
|
+
def convert_header(header, jkf)
|
28
|
+
header.filter_map do |(key, value)|
|
29
|
+
result = "#{key}:#{value}\n"
|
30
|
+
if /\A[先後上下]手\Z/.match?(key)
|
31
|
+
if /[先下]/.match?(key)
|
32
|
+
@header2.unshift result
|
33
|
+
else
|
34
|
+
@header2 << result
|
35
|
+
end
|
36
|
+
nil
|
37
|
+
elsif key == '手合割' && jkf['initial'] && jkf['initial']['preset'] && value == preset2str(jkf['initial']['preset'])
|
38
|
+
nil
|
32
39
|
else
|
33
|
-
|
40
|
+
result
|
34
41
|
end
|
35
|
-
|
36
|
-
|
37
|
-
nil
|
38
|
-
else
|
39
|
-
result
|
40
|
-
end
|
41
|
-
end.compact.join
|
42
|
-
end
|
42
|
+
end.join
|
43
|
+
end
|
43
44
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
45
|
+
def convert_moves(moves, idx = 0)
|
46
|
+
result = ''
|
47
|
+
moves.each_with_index do |move, i|
|
48
|
+
if move['special']
|
49
|
+
result += convert_special_line(move, i + idx)
|
50
|
+
else
|
51
|
+
result += convert_move_line(move, i + idx) if move['move']
|
52
|
+
result += convert_comments(move['comments']) if move['comments']
|
53
|
+
@forks.unshift convert_forks(move['forks'], i + idx) if move['forks']
|
54
|
+
end
|
53
55
|
end
|
56
|
+
result
|
54
57
|
end
|
55
|
-
result
|
56
|
-
end
|
57
58
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
59
|
+
def convert_move_line(move, index)
|
60
|
+
result = '%4d ' % [index]
|
61
|
+
result += convert_move(move['move'])
|
62
|
+
result += convert_time(move['time']) if move['time']
|
63
|
+
result += '+' if move['forks']
|
64
|
+
result + "\n"
|
65
|
+
end
|
65
66
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
67
|
+
def convert_special_line(move, index)
|
68
|
+
result = '%4d ' % [index]
|
69
|
+
result += ljust(special2kan(move['special']), 13)
|
70
|
+
result += convert_time(move['time']) if move['time']
|
71
|
+
result += '+' if move['forks']
|
72
|
+
result += "\n"
|
73
|
+
# first_board+speical分を引く(-2)
|
74
|
+
result + convert_special(move['special'], index - 2)
|
75
|
+
end
|
75
76
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
77
|
+
def convert_move(move)
|
78
|
+
result = convert_piece_with_pos(move)
|
79
|
+
result += if move['from']
|
80
|
+
"(#{pos2str(move['from'])})"
|
81
|
+
else
|
82
|
+
'打'
|
83
|
+
end
|
84
|
+
ljust(result, 13)
|
85
|
+
end
|
85
86
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
87
|
+
def convert_time(time)
|
88
|
+
'(%2d:%02d/%02d:%02d:%02d)' % [
|
89
|
+
time['now']['m'],
|
90
|
+
time['now']['s'],
|
91
|
+
time['total']['h'],
|
92
|
+
time['total']['m'],
|
93
|
+
time['total']['s']
|
94
|
+
]
|
95
|
+
end
|
95
96
|
|
96
|
-
|
97
|
-
|
98
|
-
when "CHUDAN" then "中断"
|
99
|
-
when "TORYO" then "投了"
|
100
|
-
when "JISHOGI" then "持将棋"
|
101
|
-
when "SENNICHITE" then "千日手"
|
102
|
-
when "TSUMI" then "詰み"
|
103
|
-
when "FUZUMI" then "不詰"
|
104
|
-
when "TIME_UP" then "切れ負け"
|
105
|
-
when "ILLEGAL_ACTION" then "反則勝ち"
|
106
|
-
when "ILLEGAL_MOVE" then "反則負け"
|
97
|
+
def special2kan(special)
|
98
|
+
SPECIAL_NAME_TO_KIF_MAPPING[special]
|
107
99
|
end
|
108
|
-
end
|
109
100
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
101
|
+
SPECIAL_NAME_TO_KIF_MAPPING = {
|
102
|
+
'CHUDAN' => '中断',
|
103
|
+
'TORYO' => '投了',
|
104
|
+
'JISHOGI' => '持将棋',
|
105
|
+
'SENNICHITE' => '千日手',
|
106
|
+
'TSUMI' => '詰み',
|
107
|
+
'FUZUMI' => '不詰',
|
108
|
+
'TIME_UP' => '切れ負け',
|
109
|
+
'ILLEGAL_ACTION' => '反則勝ち',
|
110
|
+
'ILLEGAL_MOVE' => '反則負け'
|
111
|
+
}.freeze
|
112
|
+
|
113
|
+
private_constant :SPECIAL_NAME_TO_KIF_MAPPING
|
115
114
|
|
116
|
-
|
117
|
-
|
115
|
+
def ljust(str, n)
|
116
|
+
len = 0
|
117
|
+
str.each_codepoint { |codepoint| len += codepoint > 255 ? 2 : 1 }
|
118
|
+
str + (' ' * (n - len))
|
119
|
+
end
|
120
|
+
|
121
|
+
def pos2str(pos)
|
122
|
+
'%d%d' % [pos['x'], pos['y']]
|
123
|
+
end
|
118
124
|
end
|
119
125
|
end
|
120
126
|
end
|