羽生さんが駒を一度も後退させずに勝った対局 - inaniwa3's blog
を追試してみる
棋譜データは元記事と同様に2chkifu.zipをダウンロードして,適当なディレクトリに解凍しておく.更に,path に解凍したディレクトリを指定しておく.
In [1]:
import os
import sys
sys.path.append('./../../')
from pyogi.ki2converter import *
from pyogi.kifu import *
In [2]:
forking_csa_indexes = []
for n in range(0, 50000):
path = '~/data/shogi/2chkifu/{0}000{1}/{2:0>5}.KI2'.format(n // 10000, int(n < 10000), n)
kifile = os.path.expanduser(path)
if not os.path.exists(kifile):
continue
ki2converter = Ki2converter()
ki2converter.from_path(kifile)
try:
csa = ki2converter.to_csa()
except RuntimeError:
print('Parse error:{0}'.format(n))
continue
if not csa:
continue
kifu = Kifu()
kifu.from_csa(csa)
if not kifu.extracted:
continue
if kifu.players[0] == '羽生善治':
teban_habu = 0
direction = 1
elif kifu.players[1] == '羽生善治':
teban_habu = 1
direction = -1
else:
continue
## 羽生が勝っていない場合
if (teban_habu == 1 and kifu.sente_win) or kifu.is_sennichite:
continue
moves = kifu.moves[teban_habu::2]
for move in moves:
## 駒が後退したとき
if (int(move[4]) - int(move[2])) * direction > 0 and int(move[2]) != 0:
break
else:
forking_csa_indexes.append(n)
print(n)
kifu.print_state(mode='mpl')
該当対局は以下の通り
In [3]:
print(forking_csa_indexes)
羽生さんが駒を一度も後退させずに勝った対局 - inaniwa3's blog中の出力結果
00311.csa 00685.csa 01600.csa 06521.csa 07639.csa 35267.csa 45018.csa
と完全に一致する.
In [4]:
%load_ext version_information
%version_information matplotlib
Out[4]: