Skip to content

Commit

Permalink
Now parse move annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-bovet committed May 27, 2021
1 parent 560cf9b commit b301daf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
24 changes: 24 additions & 0 deletions BChessTests/PGNTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,27 @@ TEST_F(PGN, Game1) {
auto pgnAgain = FPGN::getGame(game, FPGN::Formatting::history);
ASSERT_EQ("1. e4 e5 2. Nf3 Nc6 3. Nc3 Nf6 4. Bc4 Nxe4 5. Nxe4 d5 6. Bxd5 Qxd5 7. Nc3 Qd6 8. O-O Bg4 9. h3 Bh5 10. Nb5 Qe7 11. c3 O-O-O 12. Qc2 Qf6 13. Nh2 Be2 14. Nxa7+ Nxa7 15. Re1 Bd3 16. Qa4 Bc5 17. Ng4 Qh4 18. b4 Bb6 19. Rxe5 h5 20. Ne3 Rh6 21. Bb2 Rf6 22. f3 Rxf3 23. Re8 Qf2+ 24. Kh1 Rg3 25. Rd1 Qxg2+ 26. Nxg2 Rxh3# 0-1", pgnAgain);
}

TEST_F(PGN, MovesWithAnnotationSymbols) {
std::string pgn = "1.e4 e5! 2.Nf3?? Nc6?! *";

ChessGame game;
ASSERT_TRUE(FPGN::setGame(pgn, game));

ASSERT_EQ(4, game.getNumberOfMoves());

auto pgnAgain = FPGN::getGame(game, FPGN::Formatting::history);
ASSERT_EQ("1. e4 e5 2. Nf3 Nc6 *", pgnAgain);
}

TEST_F(PGN, MovesWithConsecutiveComments) {
std::string pgn = "1.e4 e5 2.Nf3 Nc6 { and } { the } { game } { continues } *";

ChessGame game;
ASSERT_TRUE(FPGN::setGame(pgn, game));

ASSERT_EQ(4, game.getNumberOfMoves());

auto pgnAgain = FPGN::getGame(game, FPGN::Formatting::history);
ASSERT_EQ("1. e4 e5 2. Nf3 Nc6 *", pgnAgain);
}
26 changes: 26 additions & 0 deletions Shared/Engine/Helpers/FPGN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,30 @@ bool FPGN::parseTerminationMarker() {
}
}

bool FPGN::parseMoveAnnotation() {
if (pgn[cursor] == '?' && pgn[cursor+1] == '!') {
cursor += 2;
return true;
} else if (pgn[cursor] == '!' && pgn[cursor+1] == '?') {
cursor += 2;
return true;
} else if (pgn[cursor] == '!' && pgn[cursor+1] == '!') {
cursor += 2;
return true;
} else if (pgn[cursor] == '?' && pgn[cursor+1] == '?') {
cursor += 2;
return true;
} else if (pgn[cursor] == '!') {
cursor++;
return true;
} else if (pgn[cursor] == '?') {
cursor++;
return true;
} else {
return false;
}
}

bool FPGN::parseMove(Move &move) {
PARSE_BEGIN

Expand Down Expand Up @@ -458,6 +482,8 @@ bool FPGN::parseMove(Move &move) {
// 8.2.3.5: Check and checkmate indication characters
if (isCheckOrMate(pgn[cursor])) {
cursor++;
} else if (parseMoveAnnotation()) {
// Check for annotation style (ie g6?!)
}

// Handle promotion
Expand Down
3 changes: 2 additions & 1 deletion Shared/Engine/Helpers/FPGN.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class FPGN {
bool parseMoveNumber(unsigned &moveNumber, bool &isMoveForBlack);
bool parsePiece(Piece &p);
bool parseMove(Move &move);

bool parseMoveAnnotation();

bool parseTerminationMarker();
bool parseComment(std::string & comment);

Expand Down

0 comments on commit b301daf

Please sign in to comment.