Skip to content

Commit

Permalink
Fixed issue where a manual move was not replacing the variations
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-bovet committed May 5, 2021
1 parent d1d0c00 commit ed487d1
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion BChessTests/PGNTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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+ *");
Expand Down
2 changes: 1 addition & 1 deletion Shared/Bridge/FEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
2 changes: 1 addition & 1 deletion Shared/Bridge/FEngineInfo.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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<self.info.line.count; index++) {
Move move = self.info.line[index];
lineGame.move(move);
lineGame.move(move, false);
}

// Get the PGN representation of the best line only
Expand Down
4 changes: 2 additions & 2 deletions Shared/Engine/Engine/ChessEngine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ class ChessEngine {
return game.movesAt(file, rank);
}

void move(Move move) {
game.move(move);
void move(Move move, bool replace) {
game.move(move, replace);
}

void move(std::string from, std::string to) {
Expand Down
35 changes: 22 additions & 13 deletions Shared/Engine/Engine/ChessGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,36 @@ std::vector<Move> 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<node.variations.size(); index++) {
if (node.variations[index].move == move) {
moveIndexes.moves.push_back(index);
moveIndexes.moveCursor++;
found = true;
break;
if (replace) {
// Clear all the variations so this move
// becomes the only one available.
node.variations.clear();
} else {
// 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.
for (int index=0; index<node.variations.size(); index++) {
if (node.variations[index].move == move) {
moveIndexes.moves.push_back(index);
moveIndexes.moveCursor++;
found = true;
break;
}
}
}

// If the move is not found in any variations of this node, this mean
// this is a new variation (either main variation if no variation exists yet).
if (!found) {
if (!found || replace) {
MoveNode newNode = MoveNode();
newNode.move = move;
node.variations.push_back(newNode);
Expand Down Expand Up @@ -115,7 +124,7 @@ void ChessGame::move(Move move) {
void ChessGame::move(std::string from, std::string to) {
auto move = board.getMove(from, to);
if (MOVE_ISVALID(move)) {
ChessGame::move(move);
ChessGame::move(move, false);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Shared/Engine/Engine/ChessGame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class ChessGame {
std::vector<Move> movesAt(File file, Rank rank);
std::vector<Move> allMoves();

void move(Move move);
void move(Move move, bool replace);
void move(std::string from, std::string to);

bool canUndoMove();
Expand Down
4 changes: 2 additions & 2 deletions Shared/Engine/Helpers/FPGN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit ed487d1

Please sign in to comment.