gunyarakun / python-shogi

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

Board.legal_moves contains illegal moves using bishop #50

Closed rmilville closed 2 years ago

rmilville commented 2 years ago

When programming a shogi AI, I found that board.legal_moves sometimes contains illegal moves.

This case may be because I create a board from a sfen.

The piece played in illegal moves seems to always be a bishop from my experience.

Here is an example :

import shogi
board = shogi.Board('3k4p/lg2sB1rl/npp1p1s2/p2pNpppg/P1P6/1PGPP1PRL/L8/3SG2S1/1NB1K2N1 b 3p 80')
for m in board.legal_moves :
    print(m)
print(board)

The silver on 3c is blocking the bishop's diagonal but B(4b)2d+ and B(4b)2d are listed as legal moves.

result :

9e9d 7e7d 8f8e 6f6e 5f5e 3f3e 1f1d 1f1e 9g9f 5d6b+ 8i7g 2i3g 2i1g 6h7g 6h6g 6h5g 2h3g 2h2g 2h1g 2h3i 2h1i 7f8e 7f6e 7f7g 5h6g 5h5g 5h4g 5h4h 4b5a 4b5a+ 4b3a 4b3a+ 4b5c 4b5c+ 4b3c 4b3c+ 4b2d 4b2d+ 4b1e 4b1e+ 7i8h 2f2d 2f2e 2f2g 5i4h 5i6i 5i4i . . . k . . . . p l g . . s B . r l n p p . p . s . . p . . p N p p p g P . P . . . . . . . P G P P . P R L L . . . . . . . . . . . S G . . S . . N B . K . . N .

p*3

edit : formatting + detailling

rmilville commented 2 years ago

I found what causes this and it is not within this code. My apologies for this

gunyarakun commented 2 years ago

Thank you for your report!