Disservin / chess-library

C++ chess library
https://disservin.github.io/chess-library/
MIT License
63 stars 20 forks source link

pgn::StreamParser fails when game ends before first move #68

Closed Parranoh closed 5 months ago

Parranoh commented 5 months ago

My PGN file, taken from Lichess:

[Event "Rated Blitz tournament https://lichess.org/tournament/UnR37DjL"]
[Site "https://lichess.org/PqdJBiQ3"]
[Date "2023.12.01"]
[Round "-"]
[White "eliasevinotintos"]
[Black "bianney1963"]
[Result "0-1"]

 0-1

When I try to parse this with pgn::StreamParser, it skips 0 as the move counter, then parses -1 as a move, and finally tries to read a second move until it reaches EOF (or finds the next [Event two lines down).

This patch fixes the issue for me, but the code isn't very pleasant to look at.

Disservin commented 5 months ago

Thanks, I will have a look.

Disservin commented 5 months ago

Should be fixed now.

Parranoh commented 5 months ago

Thank you very much for the quick repsonse!

If the game is the only game in the file, it works fine, yes. But take this file for example:

[Event "Rated Blitz tournament https://lichess.org/tournament/UnR37DjL"]
[Site "https://lichess.org/PqdJBiQ3"]
[Date "2023.12.01"]
[Round "-"]
[White "eliasevinotintos"]
[Black "bianney1963"]
[Result "0-1"]

 0-1

[Event "Rated Blitz tournament https://lichess.org/tournament/j6Yz21wo"]
[Site "https://lichess.org/r8UlPxeR"]
[Date "2023.12.01"]
[Round "-"]
[White "Flavinn"]
[Black "PauladaPower"]
[Result "0-1"]

1. d4 e6 2. c3 d5 3. e3 c6 4. Nf3 f5 5. Bd3 Nf6 6. Qe2 Bd6 7. O-O O-O 8. Nbd2 a5 9. Bb1 Nbd7 10. h3 Qe8 11. Qd3 b5 12. Re1 a4 13. Ng5 h6 14. Ngf3 Qh5 15. Nh2 g5 16. Bc2 Kg7 17. Bd1 Qg6 18. Kh1 Ne4 19. Rf1 Ndf6 20. Kg1 Rh8 21. f3 Ng3 22. Rf2 Ngh5 23. Ndf1 Ng3 24. Bd2 Bd7 25. Bc2 Be8 26. Be1 Ngh5 27. g4 fxg4 28. fxg4 Qxd3 29. Bxd3 Ng3 30. Rf3 Nge4 31. Nd2 Bg6 32. Nhf1 Raf8 33. Bxe4 Nxe4 34. Rxf8 Rxf8 35. Nxe4 Bxe4 36. Bg3 Bxg3 37. Nxg3 Rf3 38. Nxe4 dxe4 39. Re1 Rxh3 40. Kg2 Rf3 41. Re2 Kf6 42. b3 axb3 43. axb3 e5 44. c4 bxc4 45. bxc4 Ke6 46. Re1 Kd6 47. Rh1 Rxe3 48. Rxh6+ Kd7 49. c5 exd4 50. Rd6+ Kc7 51. Rxd4 Rc3 52. Rxe4 Rxc5 53. Kf3 Kd6 54. Rd4+ Ke5 55. Re4+ Kf6 56. Rd4 Rd5 57. Rc4 c5 58. Ke4 Re5+ 59. Kf3 Ke6 60. Kg3 Kd5 61. Rc2 Re3+ 62. Kf2 Rb3 63. Rd2+ Ke4 0-1

Now the parser skips over 0-1 and the blank line and returns [Event as the first move of the game.

Disservin commented 5 months ago

Uh yikes... let me check