niklasf / python-chess

A chess library for Python, with move generation and validation, PGN parsing and writing, Polyglot opening book reading, Gaviota tablebase probing, Syzygy tablebase probing, and UCI/XBoard engine communication
https://python-chess.readthedocs.io/en/latest/
GNU General Public License v3.0
2.44k stars 531 forks source link

set_fen does not yet check for chess position legality #16

Closed sshivaji closed 9 years ago

sshivaji commented 9 years ago

I tried set_fen with only one king on the board and it passed validation. Looking at the code, this is not yet done. It would be useful to check if a fen is valid.

Use case: Setup position on a board and using python chess to verify that the position is valid.

sshivaji commented 9 years ago

Never mind, looks like calling status() after setting fen does the trick, might be worth making this happen by default

niklasf commented 9 years ago

Yeah, error handling can sometimes be a difficult thing to get right.

It was easier not to automatically check the status after set_fen, because then it is very simple to keep the guarantee, that set_fen does not leave the board in a half updated state.

For example:

board = chess.Bitboard()
try:
    board.set_fen("syntactically-invalid-fen")
except:
    # Now the board is guaranteed to not have changed
    pass

I'll keep this issue to look into this again sometime.

sshivaji commented 9 years ago

I also like that set_fen allows you to setup a position partially (useful for doing setup board). Maybe a separate process_fen(fen) method might be useful. This method can throw exceptions if there is something wrong.

This method might be eaiser than set_fen and then checking status() for any problems.

On Mon, Nov 24, 2014 at 8:43 AM, Niklas Fiekas notifications@github.com wrote:

Yeah, error handling can sometimes be a difficult thing to get right.

It was easier not to automatically check the status after set_fen, because then it is very simple to keep the guarantee, that set_fen does not leave the board in a half updated state.

For example:

board = chess.Bitboard() try: board.set_fen("invalid-fen") except:

Now the board is guaranteed to not have changed

pass

I'll keep this issue to look into this again sometime.

— Reply to this email directly or view it on GitHub https://github.com/niklasf/python-chess/issues/16#issuecomment-64222793.

sshivaji commented 9 years ago

Will close issue as there are more important things to address