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.43k stars 532 forks source link

can't parse PGN with piece drops #584

Closed fredhope2000 closed 4 years ago

fredhope2000 commented 4 years ago

A Game object is able to accept a drop move (e.g. game.end().add_variation(chess.Move.from_uci('Q@f3'))) and this can be saved in the PGN. However, the PGN parser is unable to read this move when reading from PGN. It errors with "invalid san" because the SAN_REGEX doesn't include the @ notation as a possibility: https://github.com/niklasf/python-chess/blob/29d1a27bbece06c3df529ee844e5b381fb386269/chess/__init__.py#L382

Are there plans to support this?

niklasf commented 4 years ago

A move like Q@f3 is not legal unless there is also [Variant "Crazyhouse"]. Then it should work fine and is already supported.

fredhope2000 commented 4 years ago

Ah, and I imagine the game needs to follow Crazyhouse rules (i.e., a piece can't be dropped unless the person has captured that piece from their opponent) in order to save properly? I tried adding an arbitrary Q@f3 onto the end of a blank game, but as soon as I set the [Variant "Crazyhouse"] in the headers, I started getting KeyError: 5 (having to do with the queen being placed) when trying to do anything with the game.

e.g.:

game = chess.pgn.Game()
game.end().add_variation(chess.Move.from_uci('Q@f3'))
print(game)  # this works, has 1. Q@f3

game.headers['Variant'] = 'Crazyhouse'
print(game)  # now it blows up

Context is I'm using this library to implement bughouse (and I'm tracking the pieces separately), so it may think my game is impossible as recorded.

I'll close this issue as I didn't realize it already worked when the Variant is set (and my new problem is sort of unrelated and not really a flaw in this package).