BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
PGN.hpp
Go to the documentation of this file.
1/*
2 * ======================================================================================
3 *
4 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
5 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
6 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
7 * ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
8 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
9 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
10 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
11 *
12 * ======================================================================================
13 */
14
19
20#pragma once
21
22#include <cstdint> // IWYU pragma: keep - for std::uint_least8_t
23#include <expected>
27#include <optional>
28#include <string>
29#include <string_view>
30#include <unordered_map>
31#include <vector>
32
33namespace chess::notation {
34
35using game::Position;
36using std::string;
37
67
72[[nodiscard]] auto format_nag(NAG nag) -> std::string_view;
73
82struct [[nodiscard]] GameRecord final {
87 std::unordered_map<string, string> metadata;
88
97 std::optional<game::Result> result;
98
99 struct Move;
100
102 struct Variation final {
104 std::vector<Move> moves;
105
108
110 size_t variationID { 0uz };
111 };
112
114 struct Move final {
117
121 string comment;
122
130 std::vector<NAG> nags;
131
136 std::vector<Variation> variations;
137 };
138
141
143 [[nodiscard]] auto get_starting_position() const -> Position { return moves.startingPosition; }
144
146 [[nodiscard]] auto get_final_position() const -> Position;
147
149 [[nodiscard]] static auto from_starting_position(const Position& pos) -> GameRecord
150 {
152
153 game.moves.startingPosition = pos;
154
155 return game;
156 }
157
158 struct PositionPointer final {
159 size_t totalPlyFromRoot { 0uz };
160
161 std::vector<size_t> variationIDs;
162 };
163
164 [[nodiscard]] auto get_move_at_pos(const PositionPointer& pos) const -> const Move*;
165};
166
175[[nodiscard]] auto from_pgn(
176 std::string_view pgnText)
177 -> std::expected<GameRecord, std::string>;
178
189[[nodiscard]] auto parse_all_pgns(
190 std::string_view fileContent)
191 -> std::vector<GameRecord>;
192
204[[nodiscard]] auto to_pgn(
205 const GameRecord& game, bool useBlockComments = true)
206 -> string;
207
208} // namespace chess::notation
auto parse_all_pgns(std::string_view fileContent) -> std::vector< GameRecord >
auto format_nag(NAG nag) -> std::string_view
auto to_pgn(const GameRecord &game, bool useBlockComments=true) -> string
auto from_pgn(std::string_view pgnText) -> std::expected< GameRecord, std::string >
@ BlackInitiative
Indicates that black has the initiative in this position.
Definition PGN.hpp:65
@ WhiteSlightAdvantage
Indicates that white has a slight advantage in this position.
Definition PGN.hpp:54
@ Good
A good move, typically displayed as !.
Definition PGN.hpp:45
@ WhiteDecisiveAdvantage
Indicates that white has a decisive advantage in this position.
Definition PGN.hpp:56
@ Brilliant
A brilliant move, typically displayed as !!.
Definition PGN.hpp:46
@ Interesting
A speculative or interesting move, typically displayed as !?.
Definition PGN.hpp:49
@ Unclear
Indicates that the position is unclear, typically displayed as an infinity symbol.
Definition PGN.hpp:53
@ WhiteModerateAdvantage
Indicates that white has a moderate advantage in this position.
Definition PGN.hpp:55
@ Inaccuracy
A mistake, typically displayed as ?.
Definition PGN.hpp:47
@ BlackModerateAdvantage
Indicates that black has a moderate advantage in this position.
Definition PGN.hpp:59
@ WhiteCrushingAdvantage
Indicates that white has a crushing advantage in this position (black should resign).
Definition PGN.hpp:57
@ WhiteInitiative
Indicates that white has the initiative in this position.
Definition PGN.hpp:64
@ Dubious
A questionable or dubious move, typically displayed as ?!.
Definition PGN.hpp:50
@ BlackCrushingAdvantage
Indicates that black has a crushing advantage in this position (white should resign).
Definition PGN.hpp:61
@ Null
A null annotation. Provided for usage as a placeholder value; should not appear in PGN files and has ...
Definition PGN.hpp:44
@ BlackZugzwang
Indicates that black is in Zugzwang in this position.
Definition PGN.hpp:63
@ Forced
A forced or only move, typically displayed as a white square.
Definition PGN.hpp:51
@ Blunder
A blunder, typically displayed as ??.
Definition PGN.hpp:48
@ Drawish
Indicates that the position is drawish or double-sided, typically displayed as =.
Definition PGN.hpp:52
@ WhiteZugzwang
Indicates that white is in Zugzwang in this position.
Definition PGN.hpp:62
@ BlackSlightAdvantage
Indicates that black has a slight advantage in this position.
Definition PGN.hpp:58
@ BlackDecisiveAdvantage
Indicates that black has a decisive advantage in this position.
Definition PGN.hpp:60
std::vector< NAG > nags
Definition PGN.hpp:130
std::vector< Variation > variations
Definition PGN.hpp:136
auto get_starting_position() const -> Position
Definition PGN.hpp:143
auto get_move_at_pos(const PositionPointer &pos) const -> const Move *
auto get_final_position() const -> Position
std::optional< game::Result > result
Definition PGN.hpp:97
std::unordered_map< string, string > metadata
Definition PGN.hpp:87
static auto from_starting_position(const Position &pos) -> GameRecord
Definition PGN.hpp:149