jkf 0.5.0 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,113 +1,115 @@
1
- module Jkf::Converter
2
- # KI2 Converter
3
- class Ki2 < Base
4
- include Kifuable
1
+ module Jkf
2
+ module Converter
3
+ # KI2 Converter
4
+ class Ki2 < Base
5
+ include Kifuable
5
6
 
6
- protected
7
+ protected
7
8
 
8
- def convert_root(jkf)
9
- reset!
10
- setup_players!(jkf)
9
+ def convert_root(jkf)
10
+ reset!
11
+ setup_players!(jkf)
11
12
 
12
- result = ""
13
- result += convert_header(jkf["header"]) if jkf["header"]
14
- result += convert_initial(jkf["initial"]) if jkf["initial"]
15
- result += @header2.join + "\n"
16
- result += convert_moves(jkf["moves"]) if jkf["moves"]
17
- if !@forks.empty?
18
- result += "\n"
19
- result += @forks.join("\n")
20
- end
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
- result
23
- end
23
+ result
24
+ end
24
25
 
25
- def convert_header(header)
26
- header.map do |(key, value)|
27
- result = add_header(key, value)
28
- if key =~ /\A[先後上下]手\Z/
29
- nil
30
- else
31
- result
32
- end
33
- end.compact.join
34
- end
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
- def add_header(key, value)
37
- result = "#{key}:#{value}\n"
38
- if key =~ /\A[先後上下]手\Z/
39
- if key =~ /[先下]/
40
- @header2.unshift result
41
- else
42
- @header2 << result
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
- def convert_moves(moves, idx = 0)
49
- result = ""
50
- j = 0
51
- before_split = ""
52
- moves.each_with_index do |move, i|
53
- if move["special"]
54
- # first_board+speical分を引く(-2)
55
- result += convert_special_and_split(move, i + idx - 2)
56
- else
57
- result += before_split
58
- if move["move"]
59
- j += 1
60
- result_move, before_split = convert_move_and_split(move, j)
61
- result += result_move
62
- end
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
- if move["comments"]
65
- if !(result.end_with?("\n") || result.empty?)
66
- result += "\n"
67
- before_split = ""
68
- j = 0
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
- @forks.unshift convert_forks(move["forks"], i + idx) if move["forks"]
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
- def convert_special_and_split(hash, index)
80
- "\n" + convert_special(hash["special"], index)
81
- end
80
+ def convert_special_and_split(hash, index)
81
+ "\n" + convert_special(hash['special'], index)
82
+ end
82
83
 
83
- def convert_move_and_split(move, num)
84
- result = convert_move(move["move"])
85
- split = if num % 6 == 0
86
- "\n"
87
- else
88
- result.size == 4 ? " " * 4 : " " * 2
89
- end
90
- [result, split]
91
- end
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
- def convert_move(move)
94
- result = move["color"] == 0 ? "" : ""
95
- result += convert_piece_with_pos(move)
96
- result += csa2relative(move["relative"]) if move["relative"]
97
- result
98
- end
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
- def csa2relative(relative)
101
- case relative
102
- when "L" then ""
103
- when "C" then ""
104
- when "R" then ""
105
- when "U" then ""
106
- when "M" then ""
107
- when "D" then ""
108
- when "H" then ""
109
- else
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
@@ -1,120 +1,126 @@
1
- module Jkf::Converter
2
- # KIF Converter
3
- class Kif < Base
4
- protected
1
+ module Jkf
2
+ module Converter
3
+ # KIF Converter
4
+ class Kif < Base
5
+ protected
5
6
 
6
- include Kifuable
7
+ include Kifuable
7
8
 
8
- def convert_root(jkf)
9
- reset!
10
- setup_players!(jkf)
9
+ def convert_root(jkf)
10
+ reset!
11
+ setup_players!(jkf)
11
12
 
12
- result = ""
13
- result += convert_header(jkf["header"], jkf) if jkf["header"]
14
- result += convert_initial(jkf["initial"]) if jkf["initial"]
15
- result += @header2.join
16
- result += "手数----指手---------消費時間--\n"
17
- result += convert_moves(jkf["moves"])
18
- if !@forks.empty?
19
- result += "\n"
20
- result += @forks.join("\n")
21
- end
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
- result
24
- end
24
+ result
25
+ end
25
26
 
26
- def convert_header(header, jkf)
27
- header.map do |(key, value)|
28
- result = "#{key}:#{value}\n"
29
- if key =~ /\A[先後上下]手\Z/
30
- if key =~ /[先下]/
31
- @header2.unshift result
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
- @header2 << result
40
+ result
34
41
  end
35
- nil
36
- elsif key == "手合割" && jkf["initial"] && jkf["initial"]["preset"] && value == preset2str(jkf["initial"]["preset"])
37
- nil
38
- else
39
- result
40
- end
41
- end.compact.join
42
- end
42
+ end.join
43
+ end
43
44
 
44
- def convert_moves(moves, idx = 0)
45
- result = ""
46
- moves.each_with_index do |move, i|
47
- if move["special"]
48
- result += convert_special_line(move, i + idx)
49
- else
50
- result += convert_move_line(move, i + idx) if move["move"]
51
- result += convert_comments(move["comments"]) if move["comments"]
52
- @forks.unshift convert_forks(move["forks"], i + idx) if move["forks"]
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
- def convert_move_line(move, index)
59
- result = "%4d " % [index]
60
- result += convert_move(move["move"])
61
- result += convert_time(move["time"]) if move["time"]
62
- result += "+" if move["forks"]
63
- result + "\n"
64
- end
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
- def convert_special_line(move, index)
67
- result = "%4d " % [index]
68
- result += ljust(special2kan(move["special"]), 13)
69
- result += convert_time(move["time"]) if move["time"]
70
- result += "+" if move["forks"]
71
- result += "\n"
72
- # first_board+speical分を引く(-2)
73
- result + convert_special(move["special"], index - 2)
74
- end
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
- def convert_move(move)
77
- result = convert_piece_with_pos(move)
78
- result += if move["from"]
79
- "(#{pos2str(move['from'])})"
80
- else
81
- ""
82
- end
83
- ljust(result, 13)
84
- end
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
- def convert_time(time)
87
- "(%2d:%02d/%02d:%02d:%02d)" % [
88
- time["now"]["m"],
89
- time["now"]["s"],
90
- time["total"]["h"],
91
- time["total"]["m"],
92
- time["total"]["s"]
93
- ]
94
- end
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
- def special2kan(special)
97
- case special
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
- def ljust(str, n)
111
- len = 0
112
- str.each_codepoint { |codepoint| len += codepoint > 255 ? 2 : 1 }
113
- str + (" " * (n - len))
114
- end
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
- def pos2str(pos)
117
- "%d%d" % [pos["x"], pos["y"]]
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