notnil / chess

chess package for go
MIT License
517 stars 130 forks source link

Parser Error? (N5f6) #82

Closed jojomi closed 2 years ago

jojomi commented 2 years ago

I am trying to parse this World Championship game

[Event "WCh"]
[Site "Elista RUS"]
[Date "2006.10.05"]
[Round "8"]
[White "Kramnik,V"]
[Black "Topalov,V"]
[Result "0-1"]
[WhiteElo "2743"]
[BlackElo "2813"]
[EventDate "2006.09.23"]
[ECO "D47"]

1. d4 d5 2. c4 c6 3. Nf3 Nf6 4. Nc3 e6 5. e3 Nbd7 6. Bd3 dxc4 7. Bxc4 b5 8.
Be2 Bb7 9. O-O b4 10. Na4 c5 11. dxc5 Nxc5 12. Bb5+ Ncd7 13. Ne5 Qc7 14.
Qd4 Rd8 15. Bd2 Qa5 16. Bc6 Be7 17. Rfc1 Bxc6 18. Nxc6 Qxa4 19. Nxd8 Bxd8
20. Qxb4 Qxb4 21. Bxb4 Nd5 22. Bd6 f5 23. Rc8 N5b6 24. Rc6 Be7 25. Rd1 Kf7
26. Rc7 Ra8 27. Rb7 Ke8 28. Bxe7 Kxe7 29. Rc1 a5 30. Rc6 Nd5 31. h4 h6 32.
a4 g5 33. hxg5 hxg5 34. Kf1 g4 35. Ke2 N5f6 36. b3 Ne8 37. f3 g3 38. Rc1
Nef6 39. f4 Kd6 40. Kf3 Nd5 41. Kxg3 Nc5 42. Rg7 Rb8 43. Ra7 Rg8+ 44. Kf3
Ne4 45. Ra6+ Ke7 46. Rxa5 Rg3+ 47. Ke2 Rxe3+ 48. Kf1 Rxb3 49. Ra7+ Kf6 50.
Ra8 Nxf4 51. Ra1 Rb2 52. a5 Rf2+ 0-1

But it results in this:

chess: pgn decode error chess: failed to decode notation text "N5f6" for position r7/1R1nk3/2R1p3/p2n1p2/P5p1/4P3/1P2KPP1/8 b - - 1 35 on move 35

I assume it might be a problem because of that slightly uncommon case where the 5 is required to distinguish the knights on the same file when both could jump to f6.

Another World Championship example that fails to parse:

chess: pgn decode error chess: failed to decode notation text "Ndxb5" for position rn2k2r/pp3ppp/4pB2/qb6/1b1NP3/2N5/PP3PPP/R2QK2R w KQkq - 0 11 on move 11
notnil commented 2 years ago

Yep definitely seeing it as well

https://lichess.org/v1WPmjuC#70

https://go.dev/play/p/oKtaveXp8_F

The notation is right it should first distinguish by file, then by rank, then by file + rank. This only needs to disambiguate by rank.

https://en.wikipedia.org/wiki/Algebraic_notation_(chess)

sumnerevans commented 2 years ago

Note that in r7/1R1nk3/2R1p3/p2n1p2/P5p1/4P3/1P2KPP1/8 b - - 1 35, the move Nf6 is actually totally unambiguous because the knight on d7 is pinned.

I think that the key here is that it needs to be able to handle extraneous additional rank/file specifiers.

notnil commented 2 years ago

@sumnerevans good call, I missed the pin. Yes we do have a strict parser at the moment and I agree it should be able to ignore superfluous ranks and files.

notnil commented 2 years ago

@sumnerevans your PR looks good at first glance. I'll take some time to review and merge if tests hold and there is no other side effects. Thanks!