Skip to content

Commit

Permalink
Move counter in / after variations
Browse files Browse the repository at this point in the history
  • Loading branch information
Isarhamster committed Dec 15, 2024
1 parent d7cc10d commit b7748db
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/database/bitboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
34 changes: 25 additions & 9 deletions src/database/gamecursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

#include <QtDebug>
#include <QFile>
#include "annotation.h"
#include "gamecursor.h"
#include "settings.h"
#include "tags.h"

using namespace chessx;

Expand All @@ -26,7 +23,6 @@ GameCursor::GameCursor()
: m_currentBoard(new BoardX)
, m_nodes()
, m_currentNode(0)
, m_startPly(0)
, m_startingBoard()
{
m_startingBoard.setStandardPosition();
Expand All @@ -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)
Expand All @@ -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)
{
Expand Down Expand Up @@ -95,7 +89,6 @@ void GameCursor::clear()
{
m_nodes.clear();
m_startingBoard.setStandardPosition();
m_startPly = 0;
initCursor();
}

Expand All @@ -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();
}

Expand All @@ -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())
{
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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()
<< ")";
}
}
10 changes: 5 additions & 5 deletions src/database/gamecursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define CURRENT_MOVE -2
#define PREV_MOVE -3
#define NEXT_MOVE -4
#define PARENT_MOVE -5

typedef int MoveId;

Expand All @@ -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<MoveId> variations;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -205,8 +207,6 @@ class GameCursor
QList<Node> 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;

Expand Down
8 changes: 6 additions & 2 deletions src/database/pgndatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ void PgnDatabase::initialise()
m_filename = QString();
m_count = 0;
m_allocated = 0;
m_endVariation = false;
}

void PgnDatabase::readLine()
Expand Down Expand Up @@ -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()) ||
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/database/pgndatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b7748db

Please sign in to comment.