From ed487d1227c21a0a5341e53e69a6fedde58bc229 Mon Sep 17 00:00:00 2001 From: Jean Bovet Date: Tue, 4 May 2021 21:31:35 -0700 Subject: [PATCH] Fixed issue where a manual move was not replacing the variations --- BChessTests/PGNTests.cpp | 2 +- Shared/Bridge/FEngine.mm | 2 +- Shared/Bridge/FEngineInfo.mm | 2 +- Shared/Engine/Engine/ChessEngine.hpp | 4 ++-- Shared/Engine/Engine/ChessGame.cpp | 35 +++++++++++++++++----------- Shared/Engine/Engine/ChessGame.hpp | 2 +- Shared/Engine/Helpers/FPGN.cpp | 4 ++-- 7 files changed, 30 insertions(+), 21 deletions(-) diff --git a/BChessTests/PGNTests.cpp b/BChessTests/PGNTests.cpp index 3a87092..d29a038 100644 --- a/BChessTests/PGNTests.cpp +++ b/BChessTests/PGNTests.cpp @@ -166,7 +166,7 @@ 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); + game.move(move, false); auto pgn = FPGN::getGame(game); 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+ *"); diff --git a/Shared/Bridge/FEngine.mm b/Shared/Bridge/FEngine.mm index 519199b..a41c2fe 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); + engine.move((Move)move, true); [self fireUpdate:self.stateIndex]; } diff --git a/Shared/Bridge/FEngineInfo.mm b/Shared/Bridge/FEngineInfo.mm index 45b671b..cb5e5e7 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 ChessGame::allMoves() { return all; } -void ChessGame::move(Move move) { +// 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) { assert(MOVE_ISVALID(move)); // Lookup the node representing the last move by `moveIndexes` - root.lookupNode(0, moveIndexes.moveCursor, moveIndexes, [&move, this](auto & node) { - // First try to match the move with an existing move for that node. - // This happens, for example, when a PGN has been loaded and - // the player is playing a series of move that correspond to that PGN. + root.lookupNode(0, moveIndexes.moveCursor, moveIndexes, [&move, replace, this](auto & node) { bool found = false; - for (int index=0; index movesAt(File file, Rank rank); std::vector allMoves(); - void move(Move move); + void move(Move move, 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 feaeeb2..c7fe468 100644 --- a/Shared/Engine/Helpers/FPGN.cpp +++ b/Shared/Engine/Helpers/FPGN.cpp @@ -557,7 +557,7 @@ bool FPGN::parseMoveText(std::string pgn, unsigned &cursor, ChessGame &game, boo return false; } - game.move(whiteMove); + game.move(whiteMove, false); // std::cout << to_string(whiteMove, SANType::full) << std::endl; // game.board.print(); @@ -609,7 +609,7 @@ bool FPGN::parseMoveText(std::string pgn, unsigned &cursor, ChessGame &game, boo // std::cout << MOVE_DESCRIPTION(blackMove) << std::endl; // game.board.print(); - game.move(blackMove); + game.move(blackMove, false); parseComment(pgn, cursor);