libscid 0.1.0
Chess applications made easy.
Loading...
Searching...
No Matches
game_positions.h
1#pragma once
2
3#include "scid/core/game_cursor.h"
4#include "scid/core/notation.h"
5#include "scid/core/pgn/traversal.h"
6
7#include <cassert>
8#include <cstdint>
9#include <string>
10#include <vector>
11
12namespace scid::core::gamepos {
13
14struct GamePos {
15 uint32_t RAVdepth;
16 uint32_t RAVnum;
17 std::string FEN; // "Forsyth-Edwards Notation" describing the position.
18 std::vector<int> NAGs; // "Numeric Annotation Glyph"
19 std::string comment; // text annotation of the position.
20 std::string lastMoveSAN; // move that was played to reach the position.
21};
22
41template <typename TCont>
42inline void collectPositions(const scid::core::Game &game, TCont &dest) {
43 scid::core::GameCursor cursor(game);
44 do {
45 if (cursor.isAtVariationStart() && !cursor.isAtGameStart())
46 continue;
47
48 dest.emplace_back();
49 auto &gamepos = dest.back();
50 char strBuf[256];
51 auto position = cursor.currentPosition();
52 assert(position);
53 position->PrintFEN(strBuf, sizeof(strBuf));
54 gamepos.FEN = strBuf;
55 gamepos.RAVdepth = cursor.variationDepth();
56 gamepos.RAVnum = cursor.variationIndex();
57 if (auto move = cursor.previousMove()) {
58 for (auto nag : move->metadata.nags)
59 gamepos.NAGs.push_back(scid::core::nagCode(nag));
60 gamepos.comment = move->metadata.comment;
61 } else if (auto variation = cursor.currentVariation()) {
62 gamepos.comment = variation->initialComment;
63 } else {
64 gamepos.comment = game.movetext().initialComment;
65 }
66 gamepos.lastMoveSAN =
67 scid::core::notation::previousSan(game, cursor.location());
68
69 } while (scid::core::pgn::nextLocation(cursor));
70}
71
78inline std::vector<GamePos> collectPositions(const scid::core::Game &game) {
79 std::vector<GamePos> res;
80 collectPositions(game, res);
81 return res;
82}
83
84} // namespace scid::core::gamepos
Definition game_cursor.h:13
Definition game.h:85
Definition game_positions.h:14