jkf 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -27,7 +27,7 @@ module Jkf::Converter
27
27
 
28
28
  def convert_initial(initial)
29
29
  result = ""
30
- data = initial["data"]
30
+ data = initial["data"] || {}
31
31
  result += if initial["preset"] == "OTHER"
32
32
  convert_board(data["board"])
33
33
  else
@@ -56,9 +56,10 @@ module Jkf::Converter
56
56
 
57
57
  def convert_moves(moves)
58
58
  result = ""
59
+ before_pos = nil
59
60
  moves.each do |move|
60
61
  next if move == {}
61
- result += convert_move(move["move"]) if move["move"]
62
+ result += convert_move(move["move"], before_pos) if move["move"]
62
63
  result += convert_special(move["special"], move["color"]) if move["special"]
63
64
  if move["time"]
64
65
  result += "," + convert_time(move["time"])
@@ -66,14 +67,20 @@ module Jkf::Converter
66
67
  result += "\n"
67
68
  end
68
69
  result += convert_comments(move["comments"]) if move["comments"]
70
+ before_pos = move["move"]["to"] if move["move"] && move["move"]["to"]
69
71
  end
70
72
  result
71
73
  end
72
74
 
73
- def convert_move(move)
75
+ def convert_move(move, before_pos)
74
76
  result = csa_color(move["color"])
75
77
  result += move["from"] ? pos2str(move["from"]) : "00"
76
- result + pos2str(move["to"]) + move["piece"]
78
+ result += if move["to"]
79
+ pos2str(move["to"]) + move["piece"]
80
+ else
81
+ pos2str(before_pos) + move["piece"]
82
+ end
83
+ result
77
84
  end
78
85
 
79
86
  def convert_special(special, color = nil)
@@ -83,7 +90,7 @@ module Jkf::Converter
83
90
  end
84
91
 
85
92
  def convert_time(time)
86
- sec = time["now"]["m"] * 60 + time["now"]["s"]
93
+ sec = (time["now"]["m"] * 60) + time["now"]["s"]
87
94
  "T#{sec}\n"
88
95
  end
89
96
 
@@ -111,7 +118,8 @@ module Jkf::Converter
111
118
  def convert_preset(preset)
112
119
  "PI" +
113
120
  case preset
114
- when "HIRATE"
121
+ when "HIRATE" # 平手
122
+ ""
115
123
  when "KY" # 香落ち
116
124
  "11KY"
117
125
  when "KY_R" # 右香落ち
@@ -10,7 +10,7 @@ module Jkf::Converter
10
10
  setup_players!(jkf)
11
11
 
12
12
  result = ""
13
- result += convert_header(jkf["header"]) if jkf["header"]
13
+ result += convert_header(jkf["header"], jkf) if jkf["header"]
14
14
  result += convert_initial(jkf["initial"]) if jkf["initial"]
15
15
  result += @header2.join
16
16
  result += "手数----指手---------消費時間--\n"
@@ -23,7 +23,7 @@ module Jkf::Converter
23
23
  result
24
24
  end
25
25
 
26
- def convert_header(header)
26
+ def convert_header(header, jkf)
27
27
  header.map do |(key, value)|
28
28
  result = "#{key}:#{value}\n"
29
29
  if key =~ /\A[先後上下]手\Z/
@@ -33,6 +33,8 @@ module Jkf::Converter
33
33
  @header2 << result
34
34
  end
35
35
  nil
36
+ elsif key == "手合割" && jkf["initial"] && jkf["initial"]["preset"] && value == preset2str(jkf["initial"]["preset"])
37
+ nil
36
38
  else
37
39
  result
38
40
  end
@@ -108,7 +110,7 @@ module Jkf::Converter
108
110
  def ljust(str, n)
109
111
  len = 0
110
112
  str.each_codepoint { |codepoint| len += codepoint > 255 ? 2 : 1 }
111
- str + " " * (n - len)
113
+ str + (" " * (n - len))
112
114
  end
113
115
 
114
116
  def pos2str(pos)
@@ -22,7 +22,7 @@ module Jkf::Converter
22
22
  end
23
23
 
24
24
  def convert_handicap(preset)
25
- preset != "OTHER" ? "手合割:#{preset2str(preset)}\n" : ""
25
+ preset == "OTHER" ? "" : "手合割:#{preset2str(preset)}\n"
26
26
  end
27
27
 
28
28
  def convert_teban(data, color)
@@ -108,8 +108,6 @@ module Jkf::Converter
108
108
  n2zen(move["to"]["x"]) + n2kan(move["to"]["y"])
109
109
  elsif move["same"]
110
110
  "同 "
111
- else
112
- raise "error??"
113
111
  end
114
112
  result += csa2kind(move["piece"])
115
113
  result += "成" if move["promote"]