Created
December 29, 2011 11:10
-
-
Save ainame/1533573 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| # $KCODE = "SJIS" | |
| module Kif | |
| Piece = { | |
| "FU" => "歩", "KE" => "桂", "KY" =>"香", | |
| "GI" => "銀", "KI" => "金", "KA" => "角", | |
| "HI" => "飛", "OU" => "玉", "TO" => "と", | |
| "NG" => "成銀", "NK" => "成桂", "NY" => "成香", | |
| "UM" => "馬", "RY" => "竜" | |
| }; | |
| NotProPiece = { | |
| "NG" => "銀", "NK" => "桂", "NY" => "香", | |
| "UM" => "角", "RY" => "飛" , "TO" =>"歩" | |
| }; | |
| ProFlag = { | |
| "FU" => false, "KE" => false, "KY" => false, | |
| "GI" => false, "KI" => false, "KA" => false, | |
| "HI" => false, "OU" => false, | |
| "TO" => true, "NG" => true, "NK" => true, | |
| "NY" => true, "UM" => true, "RY" => true | |
| }; | |
| Rank = { | |
| "1" => "一", "2" => "二", "3" => "三", | |
| "4" => "四", "5" => "五", "6" => "六", | |
| "7" => "七", "8" => "八", "9" => "九" | |
| }; | |
| File = { | |
| "1" => "1", "2" => "2", "3" => "3", | |
| "4" => "4", "5" => "5", "6" => "6", | |
| "7" => "7", "8" => "8", "9" => "9" | |
| }; | |
| Turn = ["先手", "後手"] | |
| Pro = "成" | |
| Drop = "打" | |
| B = "▲" | |
| W = "△" | |
| end | |
| #着手の変換 | |
| def conv(line, ban, comment) | |
| token = line.unpack("a1a2a1a1a2") | |
| move = "" | |
| move << ((/\+/ =~ token[0]) ? Kif::B : Kif::W) if comment | |
| move << sprintf("%s%s", Kif::File[token[2]], Kif::Rank[token[3]]) | |
| if token[1].to_i != 0 | |
| if ban[token[1]] == 0 && Kif::ProFlag[token[4]] | |
| move << sprintf("%s%s", Kif::NotProPiece[token[4]] ,Kif::Pro) | |
| else | |
| move << sprintf("%s", Kif::Piece[token[4]]) | |
| end | |
| move << sprintf("(%s)", token[1]) | |
| ban[token[2]+token[3]] = Kif::ProFlag[token[4]] if !comment | |
| elsif | |
| move << sprintf("%s%s", Kif::Piece[token[4]], Kif::Drop) | |
| ban[token[2]+token[3]] = 0 if !comment | |
| end | |
| return move | |
| end | |
| sente = "" | |
| gote = "" | |
| while line = gets | |
| if /^N\+.+/ =~ line | |
| sente = line[2..-1] | |
| elsif /^N-.+/ =~ line | |
| gote = line[2..-1] | |
| elsif /^\+$/ =~ line | |
| break | |
| elsif /^PI/ =~ line | |
| break | |
| end | |
| end | |
| header =<<HEAD | |
| \# ---- Kifu for Windows V6.24 棋譜ファイル ---- | |
| 開始日時: | |
| 手合割:平手 | |
| HEAD | |
| print header | |
| print "先手:"+sente | |
| print "後手:"+gote | |
| print "手数----指手---------消費時間--\n" | |
| tesuu = 1 | |
| timeB = timeW = 0 | |
| #手番:先手はtrue,後手はfalse、着手を取得した時に反転=>持ち時間の時に使用 | |
| turn = false | |
| ban = Hash.new | |
| 10.times{ |i| | |
| ban[i.to_s+"1"] = ban[i.to_s+"3"] = ban[i.to_s+"7"] = ban[i.to_s+"9"] = 0 | |
| } | |
| ban["22"] = ban["82"] = ban["28"] = ban["88"] = 0 | |
| # 上の4行をコメントアウトしてこちらを使うと5五将棋にも対応できます。 | |
| #6.times{ |i| | |
| # ban[i.to_s+"1"] = ban[i.to_s+"5"] = 0 | |
| #} | |
| #ban["15"] = ban["25"] = ban["41"] = ban["51"] = 0 | |
| flag = 0 | |
| pline = nil | |
| while line = gets | |
| #指し手がplineに入ったら1 | |
| #印字する際のフラグ 2 => 時間を取得したら 2 | |
| #flag = 1で思考時間が使われておらず、指し手変換に入ったら印字 | |
| flag = 0 if flag == 2 | |
| if /^[\+-]\d\d\d\d[A-Z][A-Z]/ =~ line | |
| puts pline if flag == 1 | |
| pline = sprintf("%4d ", tesuu) | |
| pline << conv(line, ban, false) | |
| tesuu = tesuu+1 | |
| turn = !turn | |
| flag = 1 | |
| elsif /^T.+/ =~ line | |
| #持ち時間の印字 | |
| line.scan(/\d+/){ |time| | |
| turn ? timeB = timeB + time.to_i: timeW = timeW + time.to_i | |
| min = time.to_i/60 | |
| sec = time.to_i%60 | |
| hou = turn ? timeB/3600 : timeW/3600 | |
| minsec = (turn ? timeB: timeW - hou*3600) | |
| pline << sprintf(" ( %02d:%02d/%02d:%02d:%02d)\n", min, sec, hou, minsec/60, minsec%60) | |
| break | |
| } | |
| flag = 2 | |
| elsif /^'.*/ =~ line | |
| #kif形式のコメントは行の先頭を*とする | |
| pline = line.sub(/^'\*\*\s/, "*") | |
| pline = line.sub(/^'\*\s/, "*") | |
| pline = pline.gsub(/\+\d\d\d\d[A-Z][A-Z]\s/){|match| | |
| "\n*"+match | |
| } | |
| pline = pline.gsub(/-\d\d\d\d[A-Z][A-Z]\s/){|match| | |
| "\n*"+match | |
| } | |
| pline = pline.gsub(/[\+-]\d\d\d\d[A-Z][A-Z]/){|move| | |
| conv(move,ban, true) | |
| } | |
| flag = 2 | |
| elsif /^%.+/ =~ line | |
| # 行頭が%ならば何らかの条件で終了 | |
| # とりあえず投了させる | |
| pline = sprintf("%4d 投了\n", tesuu) | |
| teban = turn ? Kif::Turn[0] : Kif::Turn[1] | |
| pline << "まで#{tesuu-1}手で#{teban}の勝ち\n" | |
| print pline | |
| break | |
| end | |
| #持ち時間が入力されたら印字 | |
| print pline if flag == 2 | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment