From a948fa38f42e96acfa7911e73bfa343fec6b334c Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Sat, 18 Dec 2021 16:58:44 -0700 Subject: [PATCH] notation parsing: parse promotions to non-queen pieces --- fixtures/pgns/0011.pgn | 17 +++++++++++++++++ notation.go | 2 +- pgn.go | 2 +- pgn_test.go | 4 ++++ 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 fixtures/pgns/0011.pgn diff --git a/fixtures/pgns/0011.pgn b/fixtures/pgns/0011.pgn new file mode 100644 index 0000000..0c01ccb --- /dev/null +++ b/fixtures/pgns/0011.pgn @@ -0,0 +1,17 @@ +[Event "Rated Blitz game"] +[Site "https://lichess.org/JXpwpOJf"] +[White "georgekontos"] +[Black "zev105"] +[Result "1-0"] +[UTCDate "2014.08.31"] +[UTCTime "22:03:44"] +[WhiteElo "1292"] +[BlackElo "1429"] +[WhiteRatingDiff "+15"] +[BlackRatingDiff "-15"] +[ECO "C42"] +[Opening "Russian Game: Three Knights Game"] +[TimeControl "300+3"] +[Termination "Normal"] + +1. e4 e5 2. Nf3 Nf6 3. Nc3 Bc5 4. Nxe5 Qe7 5. Bd3 Qxe5 6. O-O O-O 7. a3 d6 8. b4 Bd4 9. Bb2 Nbd7 10. Bc4 Nb6 11. Qe2 Nxc4 12. Qxc4 Be6 13. Qxc7 Nxe4 14. Nxe4 Bxb2 15. Rae1 Rac8 16. Qxd6 Bc4 17. Qxe5 Bxe5 18. d3 Be6 19. Nc5 Bxh2+ 20. Kxh2 Bd5 21. c4 Bc6 22. Re7 Rcd8 23. Rd1 b6 24. b5 Bxb5 25. cxb5 bxc5 26. Rxa7 Rd6 27. Rc7 Rh6+ 28. Kg1 Re8 29. Rxc5 f5 30. b6 f4 31. b7 Rb8 32. Rc8+ Rxc8 33. bxc8=Q+ Kf7 34. Qf5+ Rf6 35. Qxh7 f3 36. g3 Ra6 37. Qh5+ g6 38. Qh7+ Kf6 39. Qh8+ Kg5 40. Re1 Rxa3 41. Re5+ Kg4 42. Qh4# 1-0 diff --git a/notation.go b/notation.go index 8e64766..9205b4f 100644 --- a/notation.go +++ b/notation.go @@ -122,7 +122,7 @@ func (AlgebraicNotation) Encode(pos *Position, m *Move) string { return pChar + s1Str + capChar + m.s2.String() + promoText + checkChar } -var pgnRegex = regexp.MustCompile(`^(?:([RNBQKP]?)([abcdefgh]?)(\d?)(x?)([abcdefgh])(\d)(=Q)?|(O-O(?:-O)?))([+#!?]|e\.p\.)*$`) +var pgnRegex = regexp.MustCompile(`^(?:([RNBQKP]?)([abcdefgh]?)(\d?)(x?)([abcdefgh])(\d)(=[QRBN])?|(O-O(?:-O)?))([+#!?]|e\.p\.)*$`) func algebraicNotationParts(s string) (string, string, string, string, string, string, string, string, error) { submatches := pgnRegex.FindStringSubmatch(s) diff --git a/pgn.go b/pgn.go index 673ca67..2b8d7b7 100644 --- a/pgn.go +++ b/pgn.go @@ -228,7 +228,7 @@ type moveWithComment struct { Comments []string } -var moveListTokenRe = regexp.MustCompile(`(?:\d+\.)|(O-O(?:-O)?|\w*[abcdefgh][12345678]\w*(?:=Q)?)|(?:\{([^}]*)\})|(?:\([^)]*\))|(\*|0-1|1-0|1\/2-1\/2)`) +var moveListTokenRe = regexp.MustCompile(`(?:\d+\.)|(O-O(?:-O)?|\w*[abcdefgh][12345678]\w*(?:=[QRBN])?(?:\+|#)?)|(?:\{([^}]*)\})|(?:\([^)]*\))|(\*|0-1|1-0|1\/2-1\/2)`) func moveListWithComments(pgn string) ([]moveWithComment, Outcome) { pgn = stripTagPairs(pgn) diff --git a/pgn_test.go b/pgn_test.go index 9088c7b..db0f868 100644 --- a/pgn_test.go +++ b/pgn_test.go @@ -42,6 +42,10 @@ var ( PostPos: unsafeFEN("r1bqkbnr/1ppp1ppp/p1n5/1B2p3/4P3/5N2/PPPP1PPP/RNBQK2R w KQkq - 0 4"), PGN: mustParsePGN("fixtures/pgns/0010.pgn"), }, + { + PostPos: unsafeFEN("8/8/6p1/4R3/6kQ/r2P1pP1/5P2/6K1 b - - 3 42"), + PGN: mustParsePGN("fixtures/pgns/0011.pgn"), + }, } )