Skip to content

Commit

Permalink
fix: allow KIF to split pieces_in_hands with " "
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryohei Tokuda authored and gunyarakun committed Feb 9, 2024
1 parent 55edaa2 commit 5631d84
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion shogi/KIF.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def parse_pieces_in_hand(target):
return ordered_dict()

result = ordered_dict()
for item in target.split(" "):
for item in target.replace(" ", " ").split(" "):
if len(item) == 1:
result[shogi.PIECE_JAPANESE_SYMBOLS.index(item)] = 1
elif len(item) == 2 or len(item) == 3:
Expand Down
36 changes: 31 additions & 5 deletions tests/kif_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,31 @@
まで1手で中断
"""

TEST_KIF_CUSTOM_BOARD_SHOGI_GUI = """#KIF version=2.0 encoding=UTF-8
開始日時:2024/02/06 17:26:03
後手の持駒:飛 角 金四 銀三 桂四 香三 歩十七
9 8 7 6 5 4 3 2 1
+---------------------------+
| ・ ・ ・ ・ ・ ・ ・ ・v香|一
| ・ ・ ・ ・ 飛 馬 ・ ・v玉|二
| ・ ・ ・ ・ ・ ・ ・v歩 ・|三
| ・ ・ ・ ・ ・ ・v銀 ・ ・|四
| ・ ・ ・ ・ ・ ・ ・ ・ ・|五
| ・ ・ ・ ・ ・ ・ ・ ・ ・|六
| ・ ・ ・ ・ ・ ・ ・ ・ ・|七
| ・ ・ ・ ・ ・ ・ ・ ・ ・|八
| ・ ・ ・ ・ ・ ・ ・ ・ ・|九
+---------------------------+
先手の持駒:なし
先手:大内延介
後手:最新詰将棋200選
手数----指手---------消費時間--
1 3一馬(42) ( 0:00/00:00:00)
2 中断 ( 0:00/00:00:00)
まで1手で中断
"""


TEST_KIF_EXPORTED_TO_KIF = """開始日時: \r
終了日時: \r
手合割:平手\r
Expand Down Expand Up @@ -904,11 +929,12 @@ def test_parse_file(self):
self.assertEqual(result[0], TEST_KIF_RESULT)

# .kif with custom starting position
path = os.path.join(tempdir, "test_tsume.kif")
with codecs.open(path, "w", "cp932") as f:
f.write(TEST_KIF_CUSTOM_BOARD)
result = KIF.Parser.parse_file(path)
self.assertEqual(result[0], TEST_KIF_CUSTOM_BOARD_RESULT)
for test_str in [TEST_KIF_CUSTOM_BOARD, TEST_KIF_CUSTOM_BOARD_SHOGI_GUI]:
path = os.path.join(tempdir, "test_tsume.kif")
with codecs.open(path, "w", "cp932") as f:
f.write(test_str)
result = KIF.Parser.parse_file(path)
self.assertEqual(result[0], TEST_KIF_CUSTOM_BOARD_RESULT)

finally:
shutil.rmtree(tempdir)
Expand Down

0 comments on commit 5631d84

Please sign in to comment.