From b7748dbc3a8d2232615cb952837a67e5770551f1 Mon Sep 17 00:00:00 2001 From: Isarhamster Date: Sun, 15 Dec 2024 19:43:45 +0100 Subject: [PATCH] Move counter in / after variations --- src/database/bitboard.cpp | 2 +- src/database/gamecursor.cpp | 34 +++++++++++++++++++++++++--------- src/database/gamecursor.h | 10 +++++----- src/database/pgndatabase.cpp | 8 ++++++-- src/database/pgndatabase.h | 1 + 5 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/database/bitboard.cpp b/src/database/bitboard.cpp index 6c994378..826d5646 100644 --- a/src/database/bitboard.cpp +++ b/src/database/bitboard.cpp @@ -3246,7 +3246,7 @@ QString BitBoard::toHumanFen() const w.append(charLists[p].join(",")); } } - for(Piece p = BlackKing; p <= BlackPawn; ++p) + for(Piece p = BlackKing; p != Empty; ++p) // After BlackPawn there comes Empty! { if(charLists.contains(p)) { diff --git a/src/database/gamecursor.cpp b/src/database/gamecursor.cpp index 9d95cc3f..c99a62a4 100644 --- a/src/database/gamecursor.cpp +++ b/src/database/gamecursor.cpp @@ -10,10 +10,7 @@ #include #include -#include "annotation.h" #include "gamecursor.h" -#include "settings.h" -#include "tags.h" using namespace chessx; @@ -26,7 +23,6 @@ GameCursor::GameCursor() : m_currentBoard(new BoardX) , m_nodes() , m_currentNode(0) - , m_startPly(0) , m_startingBoard() { m_startingBoard.setStandardPosition(); @@ -37,7 +33,6 @@ GameCursor::GameCursor(const GameCursor& rhs) : m_currentBoard(new BoardX) , m_nodes(rhs.m_nodes) , m_currentNode(rhs.m_currentNode) - , m_startPly(rhs.m_startPly) , m_startingBoard(rhs.m_startingBoard) { if (rhs.m_currentBoard) @@ -52,7 +47,6 @@ GameCursor& GameCursor::operator=(const GameCursor& rhs) { m_nodes = rhs.m_nodes; m_currentNode = rhs.m_currentNode; - m_startPly = rhs.m_startPly; m_startingBoard = rhs.m_startingBoard; if (m_currentBoard && rhs.m_currentBoard) { @@ -95,7 +89,6 @@ void GameCursor::clear() { m_nodes.clear(); m_startingBoard.setStandardPosition(); - m_startPly = 0; initCursor(); } @@ -104,7 +97,6 @@ void GameCursor::clear(const QString& fen, bool chess960) m_nodes.clear(); m_startingBoard.setChess960(chess960); m_startingBoard.fromFen(fen); - m_startPly = (m_startingBoard.moveNumber()) * 2 - 1 + ((m_startingBoard.toMove() == Black) ? 1:0); initCursor(); } @@ -126,6 +118,11 @@ MoveId GameCursor::makeNodeIndex(MoveId moveId) const { moveId = m_nodes[m_currentNode].nextNode; } + else if (moveId == PARENT_MOVE) + { + MoveId p = m_nodes[m_currentNode].parentNode; + moveId = (p != NO_MOVE) ? p : m_currentNode; + } bool rangeOk = 0 <= moveId && moveId < m_nodes.size(); if (!rangeOk || m_nodes[moveId].Removed()) { @@ -255,9 +252,27 @@ int GameCursor::moveNumber(MoveId moveId) const { MoveId node = makeNodeIndex(moveId); if(node != NO_MOVE) + { + int plyNum = plyNumber(node)-1; + if (m_startingBoard.blackToMove()) plyNum++; + return (m_startingBoard.moveNumber()+(plyNum/2)); + } + return -1; +} + +int GameCursor::nextMoveNumber(bool atStartOfLine, bool atEndOfLine) const +{ + int select = CURRENT_MOVE; + if (atEndOfLine && !atStartOfLine) + select = PARENT_MOVE; + else if (atStartOfLine) + select = PREV_MOVE; + MoveId node = makeNodeIndex(select); + if(node != NO_MOVE) { int plyNum = plyNumber(node); - return (m_startPly + plyNum - 1) / 2 + 1; + if (m_startingBoard.blackToMove()) plyNum++; + return (m_startingBoard.moveNumber()+(plyNum/2)); } return -1; } @@ -853,6 +868,7 @@ void GameCursor::dumpMoveNode(MoveId moveId) const qDebug() << " Move : " << m_nodes.at(moveId).move.toAlgebraic() << " (" << m_nodes.at(moveId).move.rawMove() << ", " << m_nodes.at(moveId).move.rawUndo() + << ", " << m_nodes.at(moveId).Ply() << ")"; } } diff --git a/src/database/gamecursor.h b/src/database/gamecursor.h index 720f2910..fdc732c4 100644 --- a/src/database/gamecursor.h +++ b/src/database/gamecursor.h @@ -10,6 +10,7 @@ #define CURRENT_MOVE -2 #define PREV_MOVE -3 #define NEXT_MOVE -4 +#define PARENT_MOVE -5 typedef int MoveId; @@ -18,9 +19,9 @@ class GameCursor public: struct Node { - MoveId previousNode; - MoveId nextNode; - MoveId parentNode; + MoveId previousNode; /* points to the previous node in a line, in case of a line start, it also points to the parent node */ + MoveId nextNode; /* points to the next node in a line */ + MoveId parentNode; /* points to the parent node when inside a line (all nodes in the line have this!) */ short m_ply; Move move; QList variations; @@ -122,6 +123,7 @@ class GameCursor int plyNumber(MoveId moveId = CURRENT_MOVE) const; /** @return current move. Equals to (ply-1)/2+1 for standard games, but may be different */ int moveNumber(MoveId moveId = CURRENT_MOVE) const; + int nextMoveNumber(bool atStartOfLine, bool atEndOfLine) const; /** @return number of move nodes in the main line */ int countMoves() const; @@ -205,8 +207,6 @@ class GameCursor QList m_nodes; /** Keeps the current node in the game */ MoveId m_currentNode; - /** Keeps the start ply of the game, 0 for standard starting position */ - short m_startPly; /** Keeps the start position of the game */ BoardX m_startingBoard; diff --git a/src/database/pgndatabase.cpp b/src/database/pgndatabase.cpp index 0f8fec5f..23026a88 100644 --- a/src/database/pgndatabase.cpp +++ b/src/database/pgndatabase.cpp @@ -457,6 +457,7 @@ void PgnDatabase::initialise() m_filename = QString(); m_count = 0; m_allocated = 0; + m_endVariation = false; } void PgnDatabase::readLine() @@ -846,15 +847,17 @@ inline void PgnDatabase::parseMoveToken(GameX* game, QString token) if (found) { - int currentMoveNumber = game->cursor().moveNumber(); - if (m_newVariation && !white) currentMoveNumber--; + int currentMoveNumber = game->cursor().nextMoveNumber(m_newVariation, m_endVariation); if (currentMoveNumber != moveNumberFound) { + m_endVariation = false; m_variation = -1; return; } } + m_endVariation = false; + if(m_newVariation) { bool dummyNeeded = found && (((white && game->board().whiteToMove()) || @@ -925,6 +928,7 @@ void PgnDatabase::parseToken(GameX* game, const QStringRef& token) game->forward(); m_newVariation = false; m_variation = 0; + m_endVariation = true; break; case '{': m_comment.clear(); diff --git a/src/database/pgndatabase.h b/src/database/pgndatabase.h index 82628490..4247d4d7 100644 --- a/src/database/pgndatabase.h +++ b/src/database/pgndatabase.h @@ -140,6 +140,7 @@ class PgnDatabase : public Database QString m_comment; QString m_precomment; bool m_newVariation; + bool m_endVariation; int m_variation; //game index