diff --git a/BChessTests/PGNTests.cpp b/BChessTests/PGNTests.cpp index 952ec00..aa09997 100644 --- a/BChessTests/PGNTests.cpp +++ b/BChessTests/PGNTests.cpp @@ -35,7 +35,7 @@ static void assertOutputPGN(const char *pgnGame, std::string expectedFEN, std::s std::string actualFEN = FFEN::getFEN(game.board); EXPECT_STREQ(expectedFEN.c_str(), actualFEN.c_str()); - auto generatedPGN = FPGN::getGame(game); + auto generatedPGN = FPGN::getGame(game, FPGN::Formatting::history); EXPECT_STREQ(outputPGN.c_str(), generatedPGN.c_str()); // Now let's try again the assert the game using the generated PGN and the expected FEN of the game @@ -127,7 +127,7 @@ TEST_F(PGN, ParseMove) { auto fpgn = FPGN("e4 e5"); ASSERT_TRUE(fpgn.parseMove(move)); - fpgn.getGame().move(move, false); + fpgn.getGame().move(move, "", false); ASSERT_TRUE(fpgn.parseMove(move)); ASSERT_EQ(squareForName("e7"), MOVE_FROM(move)); ASSERT_EQ(squareForName("e5"), MOVE_TO(move)); @@ -175,13 +175,13 @@ TEST_F(PGN, GameInput) { } TEST_F(PGN, GameOutput) { - assertOutputPGN(move1to3, "r1bqkbnr/1ppp1ppp/p1n5/1B2p3/4P3/5N2/PPPP1PPP/RNBQK2R w KQkq - 0 4", "1. e4 e5 2. Nf3 Nc6 3. Bb5 a6 *"); + assertOutputPGN(move1to3, "r1bqkbnr/1ppp1ppp/p1n5/1B2p3/4P3/5N2/PPPP1PPP/RNBQK2R w KQkq - 0 4", "1. e4 e5 2. Nf3 Nc6 3. Bb5 a6 {This opening is called the Ruy Lopez.} *"); - assertOutputPGN(move1to10, "r1bq1rk1/2pnbppp/p2p1n2/1p2p3/3PP3/1BP2N1P/PP3PP1/RNBQR1K1 w - - 1 11", "1. e4 e5 2. Nf3 Nc6 3. Bb5 a6 4. Ba4 Nf6 5. O-O Be7 6. Re1 b5 7. Bb3 d6 8. c3 O-O 9. h3 Nb8 10. d4 Nbd7 *"); + assertOutputPGN(move1to10, "r1bq1rk1/2pnbppp/p2p1n2/1p2p3/3PP3/1BP2N1P/PP3PP1/RNBQR1K1 w - - 1 11", "1. e4 e5 2. Nf3 Nc6 3. Bb5 a6 {This opening is called the Ruy Lopez.} 4. Ba4 Nf6 5. O-O Be7 6. Re1 b5 7. Bb3 d6 8. c3 O-O 9. h3 Nb8 10. d4 Nbd7 *"); - assertOutputPGN(move1to34, "8/6p1/3k4/R2n1bp1/1p6/1P3P1P/5KP1/8 w - - 3 35", "1. e4 e5 2. Nf3 Nc6 3. Bb5 a6 4. Ba4 Nf6 5. O-O Be7 6. Re1 b5 7. Bb3 d6 8. c3 O-O 9. h3 Nb8 10. d4 Nbd7 11. c4 c6 12. cxb5 axb5 13. Nc3 Bb7 14. Bg5 b4 15. Nb1 h6 16. Bh4 c5 17. dxe5 Nxe4 18. Bxe7 Qxe7 19. exd6 Qf6 20. Nbd2 Nxd6 21. Nc4 Nxc4 22. Bxc4 Nb6 23. Ne5 Rae8 24. Bxf7+ Rxf7 25. Nxf7 Rxe1+ 26. Qxe1 Kxf7 27. Qe3 Qg5 28. Qxg5 hxg5 29. b3 Ke6 30. a3 Kd6 31. axb4 cxb4 32. Ra5 Nd5 33. f3 Bc8 34. Kf2 Bf5 *"); + assertOutputPGN(move1to34, "8/6p1/3k4/R2n1bp1/1p6/1P3P1P/5KP1/8 w - - 3 35", "1. e4 e5 2. Nf3 Nc6 3. Bb5 a6 {This opening is called the Ruy Lopez.} 4. Ba4 Nf6 5. O-O Be7 6. Re1 b5 7. Bb3 d6 8. c3 O-O 9. h3 Nb8 10. d4 Nbd7 11. c4 c6 12. cxb5 axb5 13. Nc3 Bb7 14. Bg5 b4 15. Nb1 h6 16. Bh4 c5 17. dxe5 Nxe4 18. Bxe7 Qxe7 19. exd6 Qf6 20. Nbd2 Nxd6 21. Nc4 Nxc4 22. Bxc4 Nb6 23. Ne5 Rae8 24. Bxf7+ Rxf7 25. Nxf7 Rxe1+ 26. Qxe1 Kxf7 27. Qe3 Qg5 28. Qxg5 hxg5 29. b3 Ke6 30. a3 Kd6 31. axb4 cxb4 32. Ra5 Nd5 33. f3 Bc8 34. Kf2 Bf5 *"); - assertOutputPGN(allMoves, "8/8/4R1p1/2k3p1/1p4P1/1P1b1P2/3K1n2/8 b - - 2 43", "1. e4 e5 2. Nf3 Nc6 3. Bb5 a6 4. Ba4 Nf6 5. O-O Be7 6. Re1 b5 7. Bb3 d6 8. c3 O-O 9. h3 Nb8 10. d4 Nbd7 11. c4 c6 12. cxb5 axb5 13. Nc3 Bb7 14. Bg5 b4 15. Nb1 h6 16. Bh4 c5 17. dxe5 Nxe4 18. Bxe7 Qxe7 19. exd6 Qf6 20. Nbd2 Nxd6 21. Nc4 Nxc4 22. Bxc4 Nb6 23. Ne5 Rae8 24. Bxf7+ Rxf7 25. Nxf7 Rxe1+ 26. Qxe1 Kxf7 27. Qe3 Qg5 28. Qxg5 hxg5 29. b3 Ke6 30. a3 Kd6 31. axb4 cxb4 32. Ra5 Nd5 33. f3 Bc8 34. Kf2 Bf5 35. Ra7 g6 36. Ra6+ Kc5 37. Ke1 Nf4 38. g3 Nxh3 39. Kd2 Kb5 40. Rd6 Kc5 41. Ra6 Nf2 42. g4 Bd3 43. Re6 1/2-1/2"); + assertOutputPGN(allMoves, "8/8/4R1p1/2k3p1/1p4P1/1P1b1P2/3K1n2/8 b - - 2 43", "1. e4 e5 2. Nf3 Nc6 3. Bb5 a6 {This opening is called the Ruy Lopez.} 4. Ba4 Nf6 5. O-O Be7 6. Re1 b5 7. Bb3 d6 8. c3 O-O 9. h3 Nb8 10. d4 Nbd7 11. c4 c6 12. cxb5 axb5 13. Nc3 Bb7 14. Bg5 b4 15. Nb1 h6 16. Bh4 c5 17. dxe5 Nxe4 18. Bxe7 Qxe7 19. exd6 Qf6 20. Nbd2 Nxd6 21. Nc4 Nxc4 22. Bxc4 Nb6 23. Ne5 Rae8 24. Bxf7+ Rxf7 25. Nxf7 Rxe1+ 26. Qxe1 Kxf7 27. Qe3 Qg5 28. Qxg5 hxg5 29. b3 Ke6 30. a3 Kd6 31. axb4 cxb4 32. Ra5 Nd5 33. f3 Bc8 34. Kf2 Bf5 35. Ra7 g6 36. Ra6+ Kc5 37. Ke1 Nf4 38. g3 Nxh3 39. Kd2 Kb5 40. Rd6 Kc5 41. Ra6 Nf2 42. g4 Bd3 43. Re6 1/2-1/2"); } TEST_F(PGN, GameWithBlackFromFEN) { @@ -190,7 +190,7 @@ TEST_F(PGN, GameWithBlackFromFEN) { game.move("c1", "d1"); auto pgn = FPGN::getGame(game); - ASSERT_EQ(pgn, "[FEN \"1K1k4/1P6/8/8/8/8/r7/2R5 w - - 0 1\"]\n[Setup \"1\"]\n1. Rd1+ *"); + ASSERT_EQ("[Event \"\"]\n[Site \"\"]\n[Date \"\"]\n[Round \"\"]\n[White \"\"]\n[Black \"\"]\n[Result \"\"]\n[FEN \"1K1k4/1P6/8/8/8/8/r7/2R5 w - - 0 1\"]\n[Setup \"1\"]\n\n 1. Rd1+ *", pgn); } TEST_F(PGN, GameWithBlackPromotion) { @@ -198,9 +198,9 @@ TEST_F(PGN, GameWithBlackPromotion) { ASSERT_TRUE(FPGN::setGame("1.e4 Nf6 2.Nc3 Nxe4 3.Nxe4 d5 4.Nc3 Qd6 5.Nf3 h5 6.d4 Qd8 7.Bb5+ c6 8.Ba4 b5 9.Bb3 a5 10.a4 b4 11.Na2 Bg4 12.Qd3 Bxf3 13.Qxf3 h4 14.h3 Qd6 15.Bf4 Qe6+ 16.Be3 Qd6 17.O-O g6 18.c4 bxc3 19.bxc3 Nd7 20.c4 dxc4 21.Bxc4 Qf6 22.Qg4 e5 23.Rfe1 Qg7 24.dxe5 Nc5 25.Bxc5 Bxc5 26.e6 Bd4 27.exf7+ Kf8 28.Qe6 Qf6 29.Rad1 Qxe6 30.Bxe6 c5 31.Bd5 Rb8 32.Nc1 Rh5 33.Bc4 Rf5 34.Re2 Rf4 35.Rde1 Bxf2+ 36.Rxf2 Rxc4 37.Nd3 Kg7 38.Re7 Kf8 39.Re6 Rxa4 40.Nxc5 Ra1+ 41.Kh2 Rd1 42.Ne4 Kg7 43.Ng5 Rf8 44.Rfe2 Rxf7 45.Nxf7 Kxf7 46.R6e4 Ra1 47.Rxh4 Kg8 48.Rf2 Kg7 49.Rhf4 a4 50.Rf7+ Kh6 51.R2f4 a3 52.Rg4 a2 53.Rf6 Rh1+ 54.Kxh1 *", game)); auto move = createPromotion(squareForName("a2"), squareForName("a1"), BLACK, PAWN, QUEEN); - game.move(move, false); + game.move(move, "", false); - auto pgn = FPGN::getGame(game); + auto pgn = FPGN::getGame(game, FPGN::Formatting::history); ASSERT_EQ(pgn, "1. e4 Nf6 2. Nc3 Nxe4 3. Nxe4 d5 4. Nc3 Qd6 5. Nf3 h5 6. d4 Qd8 7. Bb5+ c6 8. Ba4 b5 9. Bb3 a5 10. a4 b4 11. Na2 Bg4 12. Qd3 Bxf3 13. Qxf3 h4 14. h3 Qd6 15. Bf4 Qe6+ 16. Be3 Qd6 17. O-O g6 18. c4 bxc3 19. bxc3 Nd7 20. c4 dxc4 21. Bxc4 Qf6 22. Qg4 e5 23. Rfe1 Qg7 24. dxe5 Nc5 25. Bxc5 Bxc5 26. e6 Bd4 27. exf7+ Kf8 28. Qe6 Qf6 29. Rad1 Qxe6 30. Bxe6 c5 31. Bd5 Rb8 32. Nc1 Rh5 33. Bc4 Rf5 34. Re2 Rf4 35. Rde1 Bxf2+ 36. Rxf2 Rxc4 37. Nd3 Kg7 38. Re7 Kf8 39. Re6 Rxa4 40. Nxc5 Ra1+ 41. Kh2 Rd1 42. Ne4 Kg7 43. Ng5 Rf8 44. Rfe2 Rxf7 45. Nxf7 Kxf7 46. Re6e4 Ra1 47. Rxh4 Kg8 48. Rf2 Kg7 49. Rhf4 a4 50. Rf7+ Kh6 51. Rf2f4 a3 52. Rg4 a2 53. Rf6 Rh1+ 54. Kxh1 a1=Q+ *"); } @@ -208,11 +208,11 @@ TEST_F(PGN, OutputFromInitialPosition) { ChessGame game; ASSERT_EQ(StartFEN, game.initialFEN); - ASSERT_EQ("*", FPGN::getGame(game)); + ASSERT_EQ("*", FPGN::getGame(game, FPGN::Formatting::history)); game.move("e2", "e4"); - ASSERT_EQ("1. e4 *", FPGN::getGame(game)); + ASSERT_EQ("1. e4 *", FPGN::getGame(game, FPGN::Formatting::history)); } TEST_F(PGN, InputFromInitialPosition) { @@ -222,7 +222,7 @@ TEST_F(PGN, InputFromInitialPosition) { FPGN::setGame("1. e4 *", game); - ASSERT_EQ("1. e4 *", FPGN::getGame(game)); + ASSERT_EQ("1. e4 *", FPGN::getGame(game, FPGN::Formatting::history)); } TEST_F(PGN, OutputFromPosition) { @@ -233,7 +233,7 @@ TEST_F(PGN, OutputFromPosition) { ASSERT_EQ(fen.c_str(), game.initialFEN); auto pgn = FPGN::getGame(game); - ASSERT_EQ("[FEN \"r1bqkbnr/ppp1pppp/2n5/3p4/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 2 3\"]\n[Setup \"1\"]\n*", pgn); + ASSERT_EQ("[Event \"\"]\n[Site \"\"]\n[Date \"\"]\n[Round \"\"]\n[White \"\"]\n[Black \"\"]\n[Result \"\"]\n[FEN \"r1bqkbnr/ppp1pppp/2n5/3p4/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 2 3\"]\n[Setup \"1\"]\n\n *", pgn); } TEST_F(PGN, InputFromPosition) { @@ -260,7 +260,7 @@ TEST_F(PGN, PGNWithSimpleWhiteVariation) { ASSERT_EQ(1, game.getNumberOfMoves()); ASSERT_EQ(2, game.getRoot().variations.size()); - auto pgnAgain = FPGN::getGame(game); + auto pgnAgain = FPGN::getGame(game, FPGN::Formatting::history); ASSERT_EQ("1. e4 (1. d4) *", pgnAgain); } @@ -274,7 +274,7 @@ TEST_F(PGN, PGNWithSimpleBlackVariation) { ASSERT_EQ(1, game.getRoot().variations.size()); ASSERT_EQ(2, game.getRoot().variations[0].variations.size()); - auto pgnAgain = FPGN::getGame(game); + auto pgnAgain = FPGN::getGame(game, FPGN::Formatting::history); ASSERT_EQ("1. e4 e5 (1... c5) *", pgnAgain); } @@ -286,7 +286,7 @@ TEST_F(PGN, PGNWithVariation1) { ASSERT_EQ(12, game.getNumberOfMoves()); - auto pgnAgain = FPGN::getGame(game); + auto pgnAgain = FPGN::getGame(game, FPGN::Formatting::history); ASSERT_EQ("1. e4 e5 2. Nc3 Nf6 3. f4 d5 4. fxe5 Nxe4 5. Qf3 Nxc3 (5... Nc6) 6. bxc3 Be7 *", pgnAgain); } @@ -298,8 +298,8 @@ TEST_F(PGN, PGNWithVariation2) { ASSERT_EQ(3, game.getNumberOfMoves()); - auto pgnAgain = FPGN::getGame(game); - ASSERT_EQ("1. e4 e5 2. Nc3 (2. Nf3 Nc6 3. Nc3 Nf6) *", pgnAgain); + auto pgnAgain = FPGN::getGame(game, FPGN::Formatting::history); + ASSERT_EQ("1. e4 e5 { Normal opening } 2. Nc3 (2. Nf3 Nc6 3. Nc3 Nf6) *", pgnAgain); } TEST_F(PGN, LineFromCursor) { @@ -307,7 +307,7 @@ TEST_F(PGN, LineFromCursor) { ASSERT_TRUE(FPGN::setGame(move1to3, game)); auto line = FPGN::getGame(game, FPGN::Formatting::line, 2); - ASSERT_EQ("Nf3 Nc6 Bb5 a6", line); + ASSERT_EQ("Nf3 Nc6 Bb5 a6 {This opening is called the Ruy Lopez.}", line); } TEST_F(PGN, MoveWithNumberInFront) { @@ -318,7 +318,7 @@ TEST_F(PGN, MoveWithNumberInFront) { ASSERT_EQ(4, game.getNumberOfMoves()); - auto pgnAgain = FPGN::getGame(game); + auto pgnAgain = FPGN::getGame(game, FPGN::Formatting::history); ASSERT_EQ("1. e4 e5 2. Nf3 Nc6 *", pgnAgain); } @@ -330,6 +330,6 @@ TEST_F(PGN, Game1) { ASSERT_EQ(52, game.getNumberOfMoves()); - auto pgnAgain = FPGN::getGame(game); + 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); } diff --git a/Shared/Actions.swift b/Shared/Actions.swift index 2de35b3..5a1566a 100644 --- a/Shared/Actions.swift +++ b/Shared/Actions.swift @@ -30,19 +30,19 @@ struct Actions { func analyzeReset() { document.selection = Selection.empty() document.lastMove = nil - document.pgn = document.pgnBeforeAnalyzing + document.pgn = document.mode.pgnBeforeAnalyzing document.engine.setPGN(document.pgn) } - func changeBoardState(state: ChessDocument.State) { - if document.state == .play { - document.state = state - document.pgnBeforeAnalyzing = document.pgn + func changeBoardState(state: GameMode.Value) { + if document.mode.value == .play { + document.mode.value = state + document.mode.pgnBeforeAnalyzing = document.pgn if state == .train { engine.setFEN(StartPosFEN) } } else { - document.state = .play + document.mode.value = .play analyzeReset() } } diff --git a/Shared/Bridge/FEngine.mm b/Shared/Bridge/FEngine.mm index a41c2fe..38f1357 100644 --- a/Shared/Bridge/FEngine.mm +++ b/Shared/Bridge/FEngine.mm @@ -132,7 +132,7 @@ - (FEngineMove*)engineMoveFromMove:(Move)fmove { } - (void)move:(NSUInteger)move { - engine.move((Move)move, true); + engine.move((Move)move, "", true); [self fireUpdate:self.stateIndex]; } diff --git a/Shared/Bridge/FEngineInfo.mm b/Shared/Bridge/FEngineInfo.mm index cb5e5e7..518c3be 100644 --- a/Shared/Bridge/FEngineInfo.mm +++ b/Shared/Bridge/FEngineInfo.mm @@ -105,7 +105,7 @@ - (NSString*)bestLine:(BOOL)uci { lineGame.history = NEW_HISTORY; // TODO hack to avoid coping the history from self.game and got it overwritten here for (int index=0; index FileWrapper { - // If analyzing, don't save the pgn but rather the savedPGN which is the PGN before the analyze started - let actualPGN = state != .play ? pgnBeforeAnalyzing : pgn - + let pgnToWrite = mode.value != .play ? mode.pgnBeforeAnalyzing : pgn switch configuration.contentType { case .exampleText: - let state = GameState(pgn: actualPGN, rotated: rotated, white: whitePlayer, black: blackPlayer) + let state = GameState(pgn: pgnToWrite, rotated: rotated, white: whitePlayer, black: blackPlayer) let encoder = JSONEncoder() let data = try encoder.encode(state) return .init(regularFileWithContents: data) case .pgn: - if let data = actualPGN.data(using: .utf8) { + if let data = pgnToWrite.data(using: .utf8) { return .init(regularFileWithContents: data) } else { throw CocoaError(.fileWriteInapplicableStringEncoding) diff --git a/Shared/Engine/Engine/ChessEngine.hpp b/Shared/Engine/Engine/ChessEngine.hpp index 357d712..4f9b7e7 100644 --- a/Shared/Engine/Engine/ChessEngine.hpp +++ b/Shared/Engine/Engine/ChessEngine.hpp @@ -79,8 +79,8 @@ class ChessEngine { return game.movesAt(file, rank); } - void move(Move move, bool replace) { - game.move(move, replace); + void move(Move move, std::string comment, bool replace) { + game.move(move, comment, replace); } void move(std::string from, std::string to) { diff --git a/Shared/Engine/Engine/ChessGame.cpp b/Shared/Engine/Engine/ChessGame.cpp index 42ea832..7df0e05 100644 --- a/Shared/Engine/Engine/ChessGame.cpp +++ b/Shared/Engine/Engine/ChessGame.cpp @@ -63,11 +63,11 @@ std::vector ChessGame::allMoves() { // move: the move to perform // replace: true to replace any variations by this move only, // false to add as a new variation if the move does not exist already -void ChessGame::move(Move move, bool replace) { +void ChessGame::move(Move move, std::string comment, bool replace) { assert(MOVE_ISVALID(move)); // Lookup the node representing the last move by `moveIndexes` - root.lookupNode(0, moveIndexes.moveCursor, moveIndexes, [&move, replace, this](auto & node) { + root.lookupNode(0, moveIndexes.moveCursor, moveIndexes, [&move, &comment, replace, this](auto & node) { bool found = false; if (replace) { // Clear all the variations so this move @@ -90,6 +90,7 @@ void ChessGame::move(Move move, bool replace) { // this is a new variation (either main variation if no variation exists yet). if (!found || replace) { MoveNode newNode = MoveNode(); + newNode.comment = comment; newNode.move = move; node.variations.push_back(newNode); @@ -122,7 +123,7 @@ void ChessGame::move(Move move, bool replace) { void ChessGame::move(std::string from, std::string to) { auto move = board.getMove(from, to); if (MOVE_ISVALID(move)) { - ChessGame::move(move, false); + ChessGame::move(move, "", false); } } diff --git a/Shared/Engine/Engine/ChessGame.hpp b/Shared/Engine/Engine/ChessGame.hpp index 91f4a2e..b9045f8 100644 --- a/Shared/Engine/Engine/ChessGame.hpp +++ b/Shared/Engine/Engine/ChessGame.hpp @@ -53,6 +53,8 @@ class ChessGame { struct MoveNode { // Representation of this node's move Move move; + + std::string comment; // List representing the next node following this one. // In other words, this list contains the moves that can be @@ -161,7 +163,7 @@ class ChessGame { std::vector movesAt(File file, Rank rank); std::vector allMoves(); - void move(Move move, bool replace); + void move(Move move, std::string comment, bool replace); void move(std::string from, std::string to); bool canUndoMove(); diff --git a/Shared/Engine/Helpers/FPGN.cpp b/Shared/Engine/Helpers/FPGN.cpp index 93823e2..8817c46 100644 --- a/Shared/Engine/Helpers/FPGN.cpp +++ b/Shared/Engine/Helpers/FPGN.cpp @@ -566,8 +566,11 @@ bool FPGN::parseMoveText() { if (!parseMove(whiteMove)) { RETURN_FAILURE("Invalid move") } - - game.move(whiteMove, false); + + std::string comment; + parseComment(comment); // optional + + game.move(whiteMove, comment, false); // if (isMoveForBlack) { // std::cout << "Parse black move: " << to_string(whiteMove, SANType::full) << std::endl; @@ -576,7 +579,6 @@ bool FPGN::parseMoveText() { // } // game.board.print(); - parseComment(); // optional while (parseVariation()) { } // Parse zero or more variations // Return now if the moveText was actually only for black @@ -612,9 +614,12 @@ bool FPGN::parseMoveText() { // std::cout << "Parse black move: " << to_string(blackMove, SANType::full) << std::endl; // game.board.print(); - game.move(blackMove, false); - parseComment(); // optional + comment = ""; + parseComment(comment); // optional + + game.move(blackMove, comment, false); + while (parseVariation()) { } // Parse zero or more variations return true; @@ -622,12 +627,12 @@ bool FPGN::parseMoveText() { // Parse comments in the form: { this is a comment }. // Don't do anything with the comment itself for now. -bool FPGN::parseComment() { +bool FPGN::parseComment(std::string & comment) { PARSE_BEGIN if (character() == '{') { cursor++; - std::string comment = ""; + comment = ""; if (!parseUntil(pgn, cursor, comment, '}')) { RETURN_FAILURE("Unable to parse until a specific character") } @@ -828,7 +833,15 @@ static void getPGN(ChessBoard board, // The chess board representation which is } pgn += FPGN::to_string(move, sanType); } - + + // Output the comment for the move + if (!skip && !node.comment.empty()) { + if (pgn.size() > 0) { + pgn += " "; + } + pgn += "{"+node.comment+"}"; + } + // Execute the move on the board board.move(move); @@ -880,11 +893,47 @@ static void getPGN(ChessBoard board, // The chess board representation which is } std::string FPGN::getGame(ChessGame game, Formatting formatting, int fromIndex, int toIndex) { + std::string pgn; + + // Write the tag pairs + if (formatting == Formatting::storage) { + // Seven Tag Roster + std::vector tagRoster = { "Event", "Site", "Date", "Round", "White", "Black", "Result" }; + for (int index=0; indexfirst) == tagRoster.end()) { + // Write only non-roster tags + pgn += "["+it->first+" \""+it->second+"\"]\n"; + } + it++; + } + + if (game.initialFEN != StartFEN) { + // [FEN "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1"] + // [SetUp "1"] + std::string tags = "[FEN \"" + game.initialFEN + "\"]\n"; + tags += "[Setup \"1\"]\n"; + + pgn += tags; + } + + if (!pgn.empty()) { + // If there was any tag written, make sure + // to have a new line before the game starts + pgn += "\n"; + } + } + // Game used to compute the optimum PGN representation for each move ChessBoard outputBoard; FFEN::setFEN(game.initialFEN, outputBoard); - std::string pgn; unsigned fullMoveIndex = 0; auto rootNode = game.getRoot(); @@ -925,19 +974,6 @@ std::string FPGN::getGame(ChessGame game, Formatting formatting, int fromIndex, break; } - if (formatting == Formatting::storage) { - // Now let's add the tag pairs - // https://en.wikipedia.org/wiki/Portable_Game_Notation - if (game.initialFEN != StartFEN) { - // [FEN "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1"] - // [SetUp "1"] - std::string tags = "[FEN \"" + game.initialFEN + "\"]\n"; - tags += "[Setup \"1\"]\n"; - - pgn = tags + pgn; - } - } - return pgn; } diff --git a/Shared/Engine/Helpers/FPGN.hpp b/Shared/Engine/Helpers/FPGN.hpp index f661702..1db2774 100644 --- a/Shared/Engine/Helpers/FPGN.hpp +++ b/Shared/Engine/Helpers/FPGN.hpp @@ -49,7 +49,7 @@ class FPGN { bool parseMove(Move &move); bool parseTerminationMarker(); - bool parseComment(); + bool parseComment(std::string & comment); void eatWhiteSpaces(); diff --git a/Shared/Views/ContentView.swift b/Shared/Views/ContentView.swift index 7acac11..4af568a 100644 --- a/Shared/Views/ContentView.swift +++ b/Shared/Views/ContentView.swift @@ -22,10 +22,10 @@ struct ContentView: View { ColorInformationView(document: $document, isWhite: document.rotated ? true: false) ZStack { BoardView(document: $document) - .if(document.state == .analyze) { + .if(document.mode.value == .analyze) { $0.border(Color.yellow, width: 4) } - .if(document.state == .train) { + .if(document.mode.value == .train) { $0.border(Color.green, width: 4) } LabelsView(document: $document) @@ -38,7 +38,7 @@ struct ContentView: View { if (showInfo) { VStack(alignment: .leading) { - if (document.state != .play) { + if (document.mode.value != .play) { AnalyzeActionsView(document: $document) } @@ -68,11 +68,11 @@ struct ContentView_Previews: PreviewProvider { static var previews: some View { Group { - let doc = ChessDocument(state: .analyze) + let doc = ChessDocument(mode: GameMode(value: .analyze)) ContentView(document: .constant(doc)) } Group { - let doc = ChessDocument(state: .train) + let doc = ChessDocument(mode: GameMode(value: .train)) ContentView(document: .constant(doc)) } Group { diff --git a/Shared/Views/InformationView.swift b/Shared/Views/InformationView.swift index 9939f91..75cb5b9 100644 --- a/Shared/Views/InformationView.swift +++ b/Shared/Views/InformationView.swift @@ -68,7 +68,7 @@ struct InformationView: View { } Text(document.engine.pgnFormattedForDisplay()) Spacer() - if document.state == .play { + if document.mode.value == .play { HStack() { Image(systemName: "cpu") Text(value()) diff --git a/Shared/Views/LastMoveModifier.swift b/Shared/Views/LastMoveModifier.swift index 91735c8..6c20557 100644 --- a/Shared/Views/LastMoveModifier.swift +++ b/Shared/Views/LastMoveModifier.swift @@ -14,7 +14,7 @@ struct LastMoveModifier: ViewModifier { let document: ChessDocument func moveColor() -> Color { - if document.state == .train { + if document.mode.value == .train { if document.engine.isValidOpeningMoves() { return Color.green.opacity(0.8) } else { diff --git a/Shared/Views/PiecesView.swift b/Shared/Views/PiecesView.swift index c2c9810..9528bc1 100644 --- a/Shared/Views/PiecesView.swift +++ b/Shared/Views/PiecesView.swift @@ -70,7 +70,7 @@ struct PiecesView: View { func triggerEngineEvaluationIfSuitable() { // Don't play the engine while the user is analyzing the board - guard document.state == .play else { + guard document.mode.value == .play else { return }