notnil / chess

chess package for go
MIT License
509 stars 127 forks source link

PGN reader is not compatible with cutechess-cli #78

Closed amanjpro closed 2 years ago

amanjpro commented 3 years ago

Reading this PGN:

[Event "self-play-1"]
[Site "?"]
[Date "2021.09.06"]
[Round "98"]
[White "zahak-linux-amd64-6.2"]
[Black "zahak-linux-amd64-6.2"]
[Result "0-1"]
[FEN "r1bqkbnr/1pppp1pp/5p2/p3n3/P1P3PP/8/1P1PPP2/RNBQKBNR w KQkq - 0 1"]
[GameDuration "00:00:02"]
[GameEndTime "2021-09-06T17:23:16.165 EDT"]
[GameStartTime "2021-09-06T17:23:14.028 EDT"]
[PlyCount "77"]
[SetUp "1"]
[Termination "adjudication"]
[TimeControl "inf"]

1. Nf3 {-0.17/9 0.041s} Nxc4 {+0.09/9 0.023s} 2. Nc3 {-0.23/9 0.020s}
d5 {+0.12/9 0.017s} 3. d3 {-0.42/9 0.019s} Ne5 {+0.38/9 0.012s}
4. Nxe5 {-0.02/9 0.016s} fxe5 {+0.28/9 0.023s} 5. g5 {-0.12/9 0.033s}
e6 {+0.30/9 0.042s} 6. h5 {-0.55/9 0.085s} Bc5 {+0.48/9 0.045s}
7. h6 {-0.67/9 0.077s} gxh6 {+0.92/9 0.019s} 8. e4 {-0.30/9 0.027s}
d4 {+0.90/9 0.052s} 9. Na2 {-0.67/9 0.017s} Kd7 {+0.12/9 0.039s}
10. gxh6 {-0.30/9 0.028s} Nf6 {+0.29/9 0.023s} 11. Rg1 {-0.05/9 0.015s}
Rg8 {+0.31/9 0.020s} 12. Rg7+ {-0.21/9 0.017s} Rxg7 {-0.03/9 0.078s}
13. hxg7 {-0.07/9 0.016s} Qe7 {+0.20/9 0.014s} 14. Bh6 {-0.37/9 0.034s}
b6 {+0.20/9 0.028s} 15. Qd2 {-0.49/9 0.047s} Qf7 {+0.53/9 0.062s}
16. Be2 {-1.03/9 0.15s} Ng8 {+0.87/9 0.038s} 17. Qc1 {-1.23/9 0.037s}
Qf6 {+1.76/9 0.010s} 18. Bg5 {-1.53/9 0.027s} Qxg7 {+1.64/9 0.010s}
19. Bh4 {-1.85/9 0.034s} Bb7 {+2.01/9 0.013s} 20. Bg3 {-1.83/9 0.034s}
Ne7 {+1.96/9 0.033s} 21. Bf1 {-2.08/9 0.048s} Ng6 {+2.28/9 0.031s}
22. b3 {-2.65/9 0.052s} Rg8 {+2.68/9 0.026s} 23. Bh3 {-2.61/9 0.058s}
Ba6 {+3.02/9 0.030s} 24. Bf1 {-3.14/9 0.022s} h5 {+3.10/9 0.014s}
25. Bh2 {-3.24/9 0.013s} h4 {+3.24/9 0.012s} 26. Qd2 {-2.91/9 0.018s}
Qf8 {+3.32/9 0.015s} 27. Qg5 {-3.59/9 0.031s} Bb4+ {+4.06/9 0.013s}
28. Nxb4 {-3.54/9 0.012s} Qxb4+ {+3.53/9 0.018s} 29. Qd2 {-3.48/9 0.017s}
Qxb3 {+3.53/9 0.007s} 30. Rc1 {-3.55/9 0.015s} c6 {+3.73/9 0.014s}
31. Qc2 {-3.67/9 0.020s} Qxc2 {+3.32/9 0.008s} 32. Rxc2 {-3.63/9 0.007s}
Nf4 {+3.53/9 0.011s} 33. Bxf4 {-3.74/9 0.009s} exf4 {+4.21/9 0.006s}
34. Ke2 {-3.77/9 0.006s} Rg1 {+3.76/9 0.008s} 35. Bh3 {-4.45/9 0.014s}
Ra1 {+4.52/9 0.019s} 36. e5 {-4.63/9 0.027s} Ra3 {+4.49/9 0.014s}
37. Rd2 {-4.74/9 0.021s} Rxa4 {+4.70/9 0.003s} 38. Kf3 {-4.52/9 0.006s}
Ra3 {+4.58/9 0.015s} 39. Bf1 {-4.55/9 0.009s, Black wins by adjudication} 0-1

Will result in this error:

panic: chess: pgn decode error chess: failed to decode notation text "{+0.40/9" for position rnbqkbnr/2pp1p1p/1p2p3/p5p1/PP6/N6N/1BPPPPPP/R2QKB1R b KQkq - 1 1 on move 1

goroutine 1 [running]:
main.main()
    /home/amanj/Documents/fengen/fengen.go:47 +0xbfa
notnil commented 3 years ago

@amanjpro after reviewing the issue, it appears that the previous code only handles comments with spaces. {-4.52/9 0.006s} wouldn't be parsed correctly but { -4.52/9 0.006s } would. Clearly the more general case is "everything inside the curly brackets". Your solution works but is a bit of a hack on top of my less general solution so I will take another crack at it. Thanks for spotting this.

amanjpro commented 3 years ago

I agree it's a hack, I only did it to unblock myself for my engine... I appreciate a cleaner solution :)

I love this library and will soon use it for my other projects ;)