Geras1mleo / Chess

C# .NET 6.0 Chess Library
MIT License
49 stars 15 forks source link

Cannot load certain En Passant positions from PGN (PGN and demo code included) #19

Closed ernop closed 8 months ago

ernop commented 9 months ago

Describe the bug Cannot load certain En Passant positions from PGN

Try to load this PGN (a game from Ding Liren) using ChessBoard.LoadFromPgn:

1.e4 e6 2.d4 d5 3.Nc3 Nf6 4.Bg5 dxe4 5.Nxe4 Be7 6.Bxf6 gxf6 7.Nf3 f5 8.Nc3 a6 9.Qd2 b5 10.O-O-O b4 11.Na4 Bb7 12.Bc4 Bd5 13.Qe2 Nc6 14.Rhe1 Na5 15.Bxd5 Qxd5 16.b3 Nc6 17.c4 bxc3 18.Nxc3 Qa5 19.Qc4 Nb4 20.Kb1 O-O 21.Ne5 Rad8 22.g4 f4 23.Nd3 Nxd3 24.Rxd3 Bf6 25.Red1 Bg7 26.Qc5 Qxc5 27.dxc5 Rxd3 28.Rxd3 f5 29.gxf5 Bxc3 30.Rxc3 Rxf5 31.Kc2 Rg5 32.Kd3 Rg2 33.Ke2 Rxh2 34.Rd3 e5 35.Rd5 e4 36.Rg5+ Kh8 37.Rg4 e3 38.Rxf4 exf2 39.Rf7 c6 40.a4 Kg8 41.Rc7 Rh3 42.Rxc6 Rxb3 43.Rxa6 Rb2+ 44.Kf1 Kf7 45.a5 Ke8 46.Ra8+ Kd7 47.a6 Ra2 48.a7 Kc7 1/2-1/2

If fails with "'Given SAN move: Qc4 has been not found with current board positions.'"

This happens because in move 17 when w takes En Passant with bxc3, if you draw the board at that moment, you can see that the board has not had the pawn on c4 removed.

lichess analysis board does handle this PGN correctly.

To Reproduce

var pgn = "1.e4 e6 2.d4 d5 3.Nc3 Nf6 4.Bg5 dxe4 5.Nxe4 Be7 6.Bxf6 gxf6 7.Nf3 f5 8.Nc3 a6\r\n9.Qd2 b5 10.O-O-O b4 11.Na4 Bb7 12.Bc4 Bd5 13.Qe2 Nc6 14.Rhe1 Na5 15.Bxd5 Qxd5\r\n16.b3 Nc6 17.c4 bxc3 18.Nxc3 Qa5 19.Qc4 Nb4 20.Kb1 O-O 21.Ne5 Rad8 22.g4 f4\r\n23.Nd3 Nxd3 24.Rxd3 Bf6 25.Red1 Bg7 26.Qc5 Qxc5 27.dxc5 Rxd3 28.Rxd3 f5 29.gxf5 Bxc3\r\n30.Rxc3 Rxf5 31.Kc2 Rg5 32.Kd3 Rg2 33.Ke2 Rxh2 34.Rd3 e5 35.Rd5 e4 36.Rg5+ Kh8\r\n37.Rg4 e3 38.Rxf4 exf2 39.Rf7 c6 40.a4 Kg8 41.Rc7 Rh3 42.Rxc6 Rxb3 43.Rxa6 Rb2+\r\n44.Kf1 Kf7 45.a5 Ke8 46.Ra8+ Kd7 47.a6 Ra2 48.a7 Kc7  1/2-1/2";
var cb = ChessBoard.LoadFromPgn(pgn);

Expected behaviour Loading games with En Passant would work.

Screenshots image

Additional context I'm working around this by manually reconstructing the board and hacking around this, but not ideal.

Otherwise, I really like the library =)

Here is a demo showing

  1. loading the game up to before the e.p. capture - all good
  2. it DOES the e.p. but does NOT remove the pawn
  3. later on this causes problems.

image

Geras1mleo commented 9 months ago

Thank you for the descriptive issue!

I will take a look at this...

Greetings Geras1mleo