Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re #225 Duplicate Positions Finder #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions chessx.pro
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ HEADERS += src/database/board.h \
src/gui/databaselistmodel.h \
src/gui/digitalclock.h \
src/gui/dockwidgetex.h \
src/gui/duplicatepositionswidget.h \
src/gui/ecolistwidget.h \
src/gui/ecothread.h \
src/gui/engineoptiondialog.h \
Expand Down Expand Up @@ -466,6 +467,7 @@ SOURCES += \
src/gui/databaselistmodel.cpp \
src/gui/digitalclock.cpp \
src/gui/dockwidgetex.cpp \
src/gui/duplicatepositionswidget.cpp \
src/gui/ecolistwidget.cpp \
src/gui/engineoptiondialog.cpp \
src/gui/engineoptionlist.cpp \
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ add_library(gui STATIC
gui/digitalclock.h
gui/dockwidgetex.cpp
gui/dockwidgetex.h
gui/duplicatepositionswidget.cpp
gui/duplicatepositionswidget.h
gui/ecolistwidget.cpp
gui/ecolistwidget.h
gui/ecothread.h
Expand Down
136 changes: 136 additions & 0 deletions src/database/gamex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1826,3 +1826,139 @@ int GameX::isBetterOrEqual(const GameX& game) const
(m_annotations.count() >= game.m_annotations.count()) &&
(m_variationStartAnnotations.count() >= game.m_variationStartAnnotations.count()));
}

// Given a game at a certain position, return all the moves that led to that position
// starting with the first move.
static DuplicateMoveList getMoves(GameX const & game) noexcept
{
DuplicateMoveList ret;
ret.moves.reserve(game.plyCount());
MoveId currentSpot {game.currentMove()};
// Save the tip of this move list so that it can be linked in the frontend
ret.lastMove = currentSpot;
// Walk back toward the root node recording the moves on the way
while (currentSpot != NO_MOVE && currentSpot != ROOT_NODE)
{
ret.moves.push_back(game.move(currentSpot));
currentSpot = game.cursor().prevMove(currentSpot);
}
std::reverse(ret.moves.begin(), ret.moves.end());
return ret;
}

// Traverses all positions of all variations of the game recording the position (FEN) and the
// move list that resulted in that position.
// positions is a map from a position to the various sets of moves that resulted in that position
static void getPositions(GameX & game, std::unordered_map<QString, DuplicatedPosition> & positions) noexcept
{
if (game.nextMove() == NO_MOVE)
{
return;
}
game.forward();
DuplicateMoveList moves {getMoves(game)};
// If two positions are transpositionally equivalent but one of them ended with
// a double-advance of a pawn, they will be considered different because there
// may be an ability to capture en passant (even if there is no pawn that could
// capture en passant).
QString fenForBoard {game.board().toFen(true)};
// We don't care about the number of moves to reach this position. Id est,
// if the two positions were reachable in a different amount of moves,
// everything else being equal, it doesn't matter, they're still duplicates.
// The location of the space before the halfmove clock
qsizetype const ultimateSpace{fenForBoard.lastIndexOf(' ')};
if (ultimateSpace < 1)
{
// Unable to find the halfmove clock
return;
}
// The location of the space before the fullmove number
qsizetype const penultimateSpace{fenForBoard.lastIndexOf(' ', ultimateSpace-1)};
if (penultimateSpace < 1)
{
// Unable to find the fullmove number
return;
}
fenForBoard = fenForBoard.first(penultimateSpace);
// This will create an item in the positions map if it doesn't exist, or add to the
// set of move lists that resulted in that position.
positions[fenForBoard].moveLists.push_back(moves);
// We only get subsequent positions if this position isn't a duplicate.
// If this position is a duplicated position, then all the subsequent positions
// will also be duplicates. We short-circuit that duplicating of duplicates with this
// if statement.
if (positions[fenForBoard].moveLists.size() < 2)
{
getPositions(game, positions);
}
// Next we go back one step in the game to put it back where we found it
game.backward();
if (positions[fenForBoard].moveLists.size() > 1)
{
// We don't want subsequent duplicated positions, even if they're in variations
return;
}
if (!game.variationCount())
{
return;
}
for (MoveId const & variation_move : game.variations())
{
game.enterVariation(variation_move);
getPositions(game, positions);
game.backward();
}
}

// Duplicate positions are divided into classes:
// 1. At most one move list that leads to a position continues
// 2. Both move lists that lead to a position continue past the duplicated position
static void addWarnings(GameX & game, std::vector<DuplicatedPosition> & positions) noexcept
{
for (DuplicatedPosition & position : positions)
{
unsigned continuations{0};
for (DuplicateMoveList & moves : position.moveLists)
{
game.moveToStart();
// Get the game to the end of this move list
for (Move const & move : moves.moves)
{
if (!game.findNextMove(move))
{
// This is an (fatal?) internal error.
break;
}
}
// The game is at the end of the move list. Is there a continuation?
if (game.cursor().nextMove() != NO_MOVE)
{
++continuations;
}
}
position.warning = (continuations > 1) ? DuplicatedPosition::BothMove : DuplicatedPosition::None;
}
}

std::vector<DuplicatedPosition> GameX::getDuplicatePositions() const noexcept
{
std::unordered_map<QString, DuplicatedPosition> positions;
GameX copy {*this};
copy.moveToStart();
if (copy.nextMove() == NO_MOVE)
{
return {};
}
getPositions(copy, positions);
std::vector<DuplicatedPosition> duplicates;
for (auto it{positions.begin()}; it != positions.end(); ++it)
{
if (it->second.moveLists.size() > 1)
{
duplicates.emplace_back(std::move(it->second));
duplicates.back().fen = it->first;
}
}
addWarnings(copy, duplicates);
return duplicates;
}
25 changes: 25 additions & 0 deletions src/database/gamex.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ class SaveRestoreMove;
typedef QHash<QString, QString> TagMap;
typedef QHashIterator<QString, QString> TagMapIterator;

class DuplicateMoveList
{
public:
std::vector<Move> moves;
MoveId lastMove;
};

class DuplicatedPosition final
{
public:
enum WarningLevel
{
// At most only one variation has moves after it
None,
// More than one variation has moves after
BothMove,
};
std::vector<DuplicateMoveList> moveLists;
WarningLevel warning;
QString fen;
};

class GameX : public QObject
{
Q_OBJECT
Expand Down Expand Up @@ -155,6 +177,9 @@ public :
bool editAnnotation(QString annotation, MoveId moveId = CURRENT_MOVE, Position position = AfterMove);
/** Append to existing annotations associated with move at node @p moveId */
bool appendAnnotation(QString annotation, MoveId moveId = CURRENT_MOVE, Position position = AfterMove);
/** Finds duplicate positions within a game.
* Returns a set of positions that have more than one move list leading to that position. */
std::vector<DuplicatedPosition> getDuplicatePositions() const noexcept;

/** Append a square to the existing lists of square annotations, if there is none, create one */
bool appendSquareAnnotation(chessx::Square s, QChar colorCode);
Expand Down
Loading