gunyarakun / python-shogi

A pure Python shogi library with move generation and validation and handling of common formats.
GNU General Public License v3.0
179 stars 43 forks source link

KIF import - ignores given board state #24

Open aphirst opened 4 years ago

aphirst commented 4 years ago

I'd been considering using python-shogi to automate the task of converting a few thousand tsumeshogi problems (in KIF) format to SFEN for use elsewhere (namely an Anki plugin). Each tsumeshogi problem consists of a kifu with the initial state of the problem, and the main solution line(s), though I'm currently ignoring the (much harder) problem of automatically extracting the solutions.

It seems there are some issues with my idea. After some initial problems with KIF import (renaming them to .kifu seems to have solved that issue), I find that no matter which kifu I import, via something like:

>>> import shogi.KIF
>>> kif=shogi.KIF.Parser.parse_file('010001.kifu')[0]
>>> kif['sfen']
'lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b - 1'

I always get an SFEN corresponding to the initial game state, not of the tsumeshogi problem at all!

I asked around and so far it looks as if the following line (and the logic behind it) are responsible: https://github.com/gunyarakun/python-shogi/blob/60c997a1213475628bb0b53481eb5c860369d2dd/shogi/KIF.py#L128 Essentially, it seems that python-shogi has a hardcoded assumption that a kifu will be a "full game record" and thus always initialises the board in a "game start" state, not the state explicitly given in the kifu.

Is this understanding correct; and either way, is it possible for this to be handled properly in a future release?

Content of my .kifu file:

# ----  Kifu for Windows V4.01β 棋譜ファイル  ----
# ファイル名:D:\b\temp\M2TOK141\KIFU\1t120600-1.kif
棋戦:1手詰
戦型:なし
手合割:平手  
後手の持駒:飛 角 金四 銀三 桂四 香三 歩十七 
  9 8 7 6 5 4 3 2 1
+---------------------------+
| ・ ・ ・ ・ ・ ・ ・ ・v香|一
| ・ ・ ・ ・ 飛 馬 ・ ・v玉|二
| ・ ・ ・ ・ ・ ・ ・v歩 ・|三
| ・ ・ ・ ・ ・ ・v銀 ・ ・|四
| ・ ・ ・ ・ ・ ・ ・ ・ ・|五
| ・ ・ ・ ・ ・ ・ ・ ・ ・|六
| ・ ・ ・ ・ ・ ・ ・ ・ ・|七
| ・ ・ ・ ・ ・ ・ ・ ・ ・|八
| ・ ・ ・ ・ ・ ・ ・ ・ ・|九
+---------------------------+
先手の持駒:なし
先手:大内延介
後手:最新詰将棋200選
手数----指手---------消費時間--
*作者:大内延介
*発表誌:最新詰将棋200選
   1 3一馬(42)   ( 0:00/00:00:00)
   2 中断         ( 0:00/00:00:00)
まで1手で中断
gunyarakun commented 4 years ago

The current implementation has a problem as you pointed, that should be fixed especially to use python-shogi for tsume-shogi. I'm waiting for pull requests to handle the situation properly.