Class: Jkf::Converter::Csa
- Inherits:
-
Object
- Object
- Jkf::Converter::Csa
- Defined in:
- lib/jkf/converter/csa.rb
Constant Summary collapse
- VERSION =
'2.2'
Instance Method Summary collapse
- #convert(jkf) ⇒ Object
- #convert_comments(comments) ⇒ Object
- #convert_information(header) ⇒ Object
- #convert_initial(initial) ⇒ Object
- #convert_move(move) ⇒ Object
- #convert_moves(moves) ⇒ Object
- #convert_special(special, color = nil) ⇒ Object
- #convert_time(time) ⇒ Object
Instance Method Details
#convert(jkf) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/jkf/converter/csa.rb', line 5 def convert(jkf) hash = if jkf.is_a?(Hash) jkf else JSON.parse(jkf) end result = version result += convert_information(hash['header']) if hash['header'] result += convert_initial(hash['initial']) if hash['initial'] result += convert_moves(hash['moves']) if hash['moves'] result end |
#convert_comments(comments) ⇒ Object
136 137 138 |
# File 'lib/jkf/converter/csa.rb', line 136 def convert_comments(comments) comments.map { |comment| "'#{comment}" }.join("\n") + "\n" end |
#convert_information(header) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/jkf/converter/csa.rb', line 19 def convert_information(header) result = '' result += 'N+' + (header.delete('先手') || header.delete('下手') || '') + "\n" if header['先手'] || header['下手'] result += 'N-' + (header.delete('後手') || header.delete('上手') || '') + "\n" if header['後手'] || header['上手'] header.each { |(k,v)| result += "$#{csa_header_key(k)}:#{v}\n" } result end |
#convert_initial(initial) ⇒ Object
27 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/jkf/converter/csa.rb', line 27 def convert_initial(initial) result = '' data = initial['data'] if initial['preset'] == 'OTHER' 9.times { |y| line = "P#{y+1}" 9.times { |x| piece = data['board'][8-x][y] line += if piece == {} " * " else csa_color(piece['color']) + piece['kind'] end } result += line + "\n" } else result += 'PI' case initial['preset'] when 'HIRATE' when 'KY' # 香落ち result += '11KY' when 'KY_R' # 右香落ち result += '91KY' when 'KA' # 角落ち result += '22KA' when 'HI' # 飛車落ち result += '82HI' when 'HIKY' # 飛香落ち result += '22HI11KY91KY' when '2' # 二枚落ち result += '82HI22KA' when '3' # 三枚落ち result += '82HI22KA91KY' when '4' # 四枚落ち result += '82HI22KA11KY91KY' when '5' # 五枚落ち result += '82HI22KA81KE11KY91KY' when '5_L' # 左五枚落ち result += '82HI22KA21KE11KY91KY' when '6' # 六枚落ち result += '82HI22KA21KE81KE11KY91KY' when '8' # 八枚落ち result += '82HI22KA31GI71GI21KE81KE11KY91KY' when '10' # 十枚落ち result += '82HI22KA41KI61KI31GI71GI21KE81KE11KY91KY' end end # 持駒 if data['hands'] sum = 0 data['hands'][0].each_value { |n| sum += n } if sum > 0 result += 'P+' data['hands'][0].to_a.reverse.each { |(k, v)| v.times { result += "00#{k}" } } result += "\n" end sum = 0 data['hands'][1].each_value { |n| sum += n } if sum > 0 result += 'P-' data['hands'][1].to_a.reverse.each { |(k, v)| v.times { result += "00#{k}" } } result += "\n" end end result += csa_color(data['color']) + "\n" if data['color'] result end |
#convert_move(move) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/jkf/converter/csa.rb', line 112 def convert_move(move) result = csa_color(move['color']) result += if move['from'] "#{move['from']['x']}#{move['from']['y']}" else "00" end result += "#{move['to']['x']}#{move['to']['y']}" result += move['piece'] result end |
#convert_moves(moves) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/jkf/converter/csa.rb', line 96 def convert_moves(moves) result = '' moves.each do |move| next if move == {} result += convert_move(move['move']) if move['move'] result += convert_special(move['special'], move['color']) if move['special'] if move['time'] result += "," + convert_time(move['time']) elsif move['move'] || move['special'] result += "\n" end result += convert_comments(move['comments']) if move['comments'] end result end |
#convert_special(special, color = nil) ⇒ Object
124 125 126 127 128 129 |
# File 'lib/jkf/converter/csa.rb', line 124 def convert_special(special, color=nil) result = "%" result += csa_color(color) if color result += special result end |
#convert_time(time) ⇒ Object
131 132 133 134 |
# File 'lib/jkf/converter/csa.rb', line 131 def convert_time(time) sec = time['now']['m'] * 60 + time['now']['s'] "T#{sec}\n" end |