python-chess icon indicating copy to clipboard operation
python-chess copied to clipboard

[bug] En passant capture is missing from `generate_legal_moves()`

Open axtrace opened this issue 1 year ago • 2 comments

En passant capture is missing from generate_legal_moves()

It seems that en passant is not recognized as a legal move in the output of generate_legal_moves(), although it should be according to the rules of chess.

Example:
In the following position the en passant capture (e5xd6) by White should be legal:

rnbqkbnr/ppp2ppp/8/3pP3/3p4/8/PPP2PPP/RNBQKBNR w KQkq d6 0 4

According to the FEN, it is White's move and the en passant square is set to d6, so e5xd6 is legal. However, this move does not appear among the legal moves returned by generate_legal_moves().

Could you please check this behavior? Thanks!

axtrace avatar May 10 '25 18:05 axtrace

Works for me. Can you please share the exact code that produces the issue?

See the last move in:

>>> import chess
>>> board = chess.Board("rnbqkbnr/ppp2ppp/8/3pP3/3p4/8/PPP2PPP/RNBQKBNR w KQkq d6 0 4")
>>> list(board.generate_legal_moves())
[Move.from_uci('g1h3'), Move.from_uci('g1f3'), Move.from_uci('g1e2'), Move.from_uci('f1a6'), Move.from_uci('f1b5'), Move.from_uci('f1c4'), Move.from_uci('f1d3'), Move.from_uci('f1e2'), Move.from_uci('e1e2'), Move.from_uci('e1d2'), Move.from_uci('d1h5'), Move.from_uci('d1g4'), Move.from_uci('d1d4'), Move.from_uci('d1f3'), Move.from_uci('d1d3'), Move.from_uci('d1e2'), Move.from_uci('d1d2'), Move.from_uci('c1h6'), Move.from_uci('c1g5'), Move.from_uci('c1f4'), Move.from_uci('c1e3'), Move.from_uci('c1d2'), Move.from_uci('b1c3'), Move.from_uci('b1a3'), Move.from_uci('b1d2'), Move.from_uci('e5e6'), Move.from_uci('h2h3'), Move.from_uci('g2g3'), Move.from_uci('f2f3'), Move.from_uci('c2c3'), Move.from_uci('b2b3'), Move.from_uci('a2a3'), Move.from_uci('h2h4'), Move.from_uci('g2g4'), Move.from_uci('f2f4'), Move.from_uci('c2c4'), Move.from_uci('b2b4'), Move.from_uci('a2a4'), Move.from_uci('e5d6')]

Note that UCI notation does not use x, but in Standard Algebraic Notation:

>>> board.san(chess.Move.from_uci('e5d6'))
'exd6'

niklasf avatar May 10 '25 20:05 niklasf

Yes, sorry, this actually works and is a valid move. I was trying to apply the move in d6 format, but I needed to check it in exd6 format.

Thank you! I'm closing the issue. Sorry for the inconvenience!

axtrace avatar May 11 '25 06:05 axtrace