1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
function _kifu_io() { var line_list = []; var line_pos = 0; this.get_line_pos = function() { return line_pos; } this.get_line = function() { return (line_pos < line_list.length) ? line_list[line_pos++] : null; } this.unget_line = function() { line_pos--; } this.set_text = function(text) { line_list = text.split(/\r\n|\n|\r/); line_pos = 0; } this.get_err_info = function() { this.unget_line(); return "(" + line_pos + "): " + this.get_line(); } this.data_from_kifu = function(kifu) { this.set_text(kifu); var kyokumen = _kyokumen.get_hirate_kyokumen(); var data = new Object(); data[_key.PLAYER] = _str.DEFAULT_PLAYERS; data[_key.MOCHIGOMA]= 0; data[_key.KYOKUMEN] = kyokumen; data[_key.SASITE] = new _sasite_tree(); var a; var s; var n; var key, val; while ((s = this.get_line()) != null) { a = s.split(_key.SEPARATOR); if (a.length != 2) continue; key = a[0]; val = a[1]; switch (key) { case _key.PLAYER: a = val.split(/,/g); if (a.length != 4) throw new Error(_err.PLAYER_FORMAT); data[key] = a; break; case _key.MOCHIGOMA: n = parseInt(val); if (isNaN(n) || n < 0 || n > 2) throw new Error(_err.MOCHIGOMA_MODE); data[key] = n; break; case _key.KYOKUMEN: if (val != "") kyokumen = this.kyokumen_from_string(val); this.comment_from_kifu(kyokumen.sasite); data[key] = kyokumen; break; case _key.SASITE: data[key] = new _sasite_tree( this.sasite_node_from_kifu(null, kyokumen)); break; } } return data; } this.sasite_node_from_kifu = function(parent, kyokumen) { var sasite = null; var node = null; var a = null; var s; while ((s = this.get_line()) != null) { if (sasite == null) { s = s.substring(1); } else if (s.charAt(0) == "/") { if (s.length == 1) { node.sasite.push(a); break; } if (a == null) a = new Array(); this.unget_line(); a.push(this.sasite_node_from_kifu(node, kyokumen)); continue; } sasite = this.sasite_from_kifu(s, kyokumen); if (node == null) node = new _sasite_node(parent, sasite); else node.sasite.push(sasite); if (!sasite.is_empty_sasite()) kyokumen = kyokumen.get_next_kyokumen(sasite); this.comment_from_kifu(sasite); if (this.end_of_node(s)) break; } if (s == null) throw new Error(_err.SASITE_END); return node; } this.sasite_from_kifu = function(s, kyokumen) { if (s == "") return _sasite.get_empty_sasite(); var after = this.pos_from_kifu(s, 0); var koma = this.koma_from_kifu(s.charAt(2)); var before; var c = s.charAt(3); if (c == "u") // 打 before = 81; else if (c == "n") // 成 before = this.pos_from_kifu(s, 4); else if (_util.is_digit(c)) before = this.pos_from_kifu(s, 3); else throw new Error(_err.SASITE_OPTION); var sasite = this.check_saiste(after, before, koma, (c == "n"), kyokumen); var end; if ((end = s.match(/[thsj]\/{0,1}$/)) != null) sasite.option |= 0x1 << (12 + "thsj".indexOf(end[0].charAt(0))); return sasite; } this.check_saiste = function(after, before, koma, b_nari, kyokumen) { var sasite = new _sasite(after, before, koma); var option = kyokumen.can_put(after, before, koma); if (option == -1 || !b_nari && ((option & _opt.NARI) != 0) || b_nari && ((option & (_opt.NARI | _opt.NARAZU)) == 0)) throw new Error(_err.SASITE_RULE); if (b_nari) { option &= ~_opt.NARAZU; option |= _opt.NARI; } sasite.option = option; return sasite; } this.pos_from_kifu = function(s, offset) { var x = _util.parse_digit(s.charAt(offset)); var y = _util.parse_digit(s.charAt(offset + 1)); if (x < 1 || x > 9 || y < 1 || y > 9) throw new Error(_err.SASITE_POS); return (x - 1) + (y - 1) * 9; } this.koma_from_kifu = function(c) { var koma = parseInt(c, 26); if (isNaN(koma) || koma < 10 || _koma.MOJI[koma - 10] == "") throw new Error(_err.SASITE_KOMA); koma -= 10; if (c.match(/[A-Z]/) != null) koma += 16; return koma; } this.comment_from_kifu = function(sasite) { var a = null; var s; while ((s = this.get_line()) != null) { if (s.match(/^\*/) == null) break; if (a == null) a = new Array(); a.push(s.substring(1)); } if (a != null) sasite.comment = a.join("\n"); if (s != null) this.unget_line(); } this.kyokumen_from_string = function(s) { var a = s.split(/,/g); if (a.length != 4) throw new Error(_err.KYOKUMEN_FORMAT); var s = a[0]; var kyokumen = _kyokumen.get_empty_kyokumen(); var banmen = kyokumen.banmen; var koma; var pos = 0; var c; var n = 0; // 盤面 for (var i = 0; i < s.length; i++) { c = s.charAt(i); if (_util.is_digit(c)) { n = n * 10 + parseInt(c); } else { pos += n; if (pos >= 81) throw new Error(_err.KYOKUMEN_POS); try { koma = this.koma_from_kifu(c); } catch (e) { throw new Error(_err.KYOKUMEN_KOMA); } banmen[pos] = koma; pos++; n = 0; } } // 持駒 kyokumen.sente = this.mochigoma_from_kifu(a[1]); kyokumen.gote = this.mochigoma_from_kifu(a[2]); kyokumen.sasite = this.kyokumen_sasite_from_text(a[3]); return kyokumen; } this.mochigoma_from_kifu = function(s) { if (s.match(/^[\da-i]{0,7}$/) == null) throw new Error(_err.KYOKUMEN_MOCHIGOMA); var a = [0, 0, 0, 0, 0, 0, 0]; var n = s.length; for (var i = 0; i < n; i++) a[i] = parseInt(s.charAt(n - i - 1), 36); return a; } this.kyokumen_sasite_from_text = function(s) { if (s == "") return _sasite.get_empty_sasite(); if (s.match(/^[1-9]{2}[A-z][\da-f]{0,4}$/) == null) throw new Error(_err.KYOKUMEN_SASITE); var pos = this.pos_from_kifu(s, 0); var koma = this.koma_from_kifu(s.charAt(2)); var option = (s.length == 3) ? 0 : parseInt(s.substring(3), 16); return new _sasite(pos, -1, koma, option); } this.end_of_node = function(s) { return s.match(/\/$/) != null; } // 書き込み用 this.clear_text = function() { line_list.length = 0; line_pos = 0; } this.get_text = function() { return line_list.join("\n"); } this.write_line = function(s) { line_list[line_pos++] = s; } this.data_kifu = function(data) { this.clear_text(); var player = data[_key.PLAYER]; if (player != undefined && !_util.arr_equals(player, _str.DEFAULT_PLAYERS)) { this.write_line(_key.PLAYER + _key.SEPARATOR + player.join(",")); } var mochigoma = data[_key.MOCHIGOMA]; if (mochigoma != undefined && mochigoma != 0) { this.write_line(_key.MOCHIGOMA + _key.SEPARATOR + mochigoma); } var kyokumen = data[_key.KYOKUMEN]; if (kyokumen != undefined && (kyokumen.sasite.comment != "" || !kyokumen.equals(_kyokumen.get_hirate_kyokumen()))) { this.write_line(_key.KYOKUMEN + _key.SEPARATOR + this.kyokumen_string(kyokumen)); if (kyokumen.sasite.comment != "") this.comment_kifu(kyokumen.sasite.comment); } var sasite = data[_key.SASITE]; if (sasite != undefined && !sasite.is_empty()) { this.write_line(_key.SASITE + _key.SEPARATOR); this.sasite_node_kifu(sasite.get_root()); } return this.get_text(); } this.sasite_node_kifu = function(node) { var sasite = node.sasite; var s = "/"; var curr; var i = 0; while (i < sasite.length) { curr = sasite[i++]; if (Array.isArray(curr)) { if (curr.length == 1) { sasite = curr[0].sasite; i = 0; continue; } for (var j = 0; j < curr.length; j++) this.sasite_node_kifu(curr[j]); this.write_line("/"); return; } s += this.sasite_kifu(curr); if (i == sasite.length) s += "/"; this.write_line(s); s = ""; if (curr.comment != "") this.comment_kifu(curr.comment); } } this.comment_kifu = function(comment) { var a = comment.split(/\n/g); for (var i = 0; i < a.length; i++) this.write_line("*" + a[i]); } this.pos_kifu = function (pos) { if (pos == 81) return "u"; return "" + (pos % 9 + 1) + Math.floor(pos / 9 + 1); } this.koma_kifu = function(koma) { var c = (koma < 16) ? "a" : "A"; return String.fromCharCode(c.charCodeAt(0) + koma % 16); } this.option_kifu = function(option) { return ((option & _opt.NARI) != 0) ? "n" : ""; } this.end_kifu = function(option) { if ((option & 0xf000) != 0) { for (i = 0; i < 3; i++) if ((option & (0x1 << (i + 12))) != 0) break; return "thsj".charAt(i); } return ""; } this.sasite_kifu = function(sasite) { if (sasite.is_empty_sasite()) return ""; return this.pos_kifu(sasite.after) + this.koma_kifu(sasite.koma) + this.option_kifu(sasite.option) + this.pos_kifu(sasite.before) + this.end_kifu(sasite.option); } this.kyokumen_string = function(kyokumen) { if (kyokumen.equals(_kyokumen.get_hirate_kyokumen())) return ""; var banmen = kyokumen.banmen; var sente = kyokumen.sente; var gote = kyokumen.gote; var sasite = kyokumen.sasite; var s = ""; var koma; var n = 0; for (var i = 0; i < 81; i++) { koma = banmen[i]; if (koma != -1) { if (n != 0) s += n; s += this.koma_kifu(koma); n = 0; } else { n++; } } s += "," + this.mochigoma_kifu(sente); s += "," + this.mochigoma_kifu(gote); s += "," + this.kyokumen_sasite_kifu(sasite); return s; } this.mochigoma_kifu = function(a) { var n; for (n = _koma.Hi; n >= _koma.Fu && a[n] == 0; n--) // 飛から歩 ; if (n == -1) return ""; var s = ""; for (var i = n; i >= 0; i--) s += a[i].toString(36); return s; } this.kyokumen_sasite_kifu = function(sasite) { if (sasite.is_empty_sasite()) return ""; var option = (sasite.option != 0) ? sasite.option.toString(16) : ""; return this.pos_kifu(sasite.after) + this.koma_kifu(sasite.koma) + option; } }