Skip to content

Commit

Permalink
Fixed issue with multiple variations
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-bovet committed May 8, 2021
1 parent 7355656 commit 69d1b4a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion BChessTests/PGNTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ TEST_F(PGN, PGNWithVariation2) {
ChessGame game;
ASSERT_TRUE(FPGN::setGame(pgn, game));

ASSERT_EQ(12, game.getNumberOfMoves());
ASSERT_EQ(3, game.getNumberOfMoves());

auto pgnAgain = FPGN::getGame(game);
ASSERT_EQ("1. e4 e5 2. Nc3 (2. Nf3 Nc6 3. Nc3 Nf6) *", pgnAgain);
Expand Down
6 changes: 2 additions & 4 deletions Shared/Engine/Engine/ChessGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ void ChessGame::move(Move move, bool replace) {
// 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++;
moveIndexes.add(index);
found = true;
break;
}
Expand All @@ -95,8 +94,7 @@ void ChessGame::move(Move move, bool replace) {
node.variations.push_back(newNode);

// Insert the index of that new variation to the current move indexes
moveIndexes.moves.push_back((int)node.variations.size() - 1);
moveIndexes.moveCursor++;
moveIndexes.add((int)node.variations.size() - 1);
}
});

Expand Down
11 changes: 10 additions & 1 deletion Shared/Engine/Engine/ChessGame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ class ChessGame {
// The current index in the index of moves above
int moveCursor = 0;

void add(int move) {
if (moveCursor < moves.size()) {
moves[moveCursor] = move;
} else {
moves.push_back(move);
}
moveCursor++;
}

void reset() {
moveCursor = 0;
moves.clear();
Expand Down Expand Up @@ -110,7 +119,7 @@ class ChessGame {

void setMoveIndexes(MoveIndexes indexes) {
moveIndexes = indexes;

// Always replay the moves to update the internal
// board state when the move indexes change,
// because that means we are starting at another
Expand Down

0 comments on commit 69d1b4a

Please sign in to comment.