-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoves_io.h
More file actions
114 lines (99 loc) · 4.05 KB
/
Copy pathmoves_io.h
File metadata and controls
114 lines (99 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
a chess library (bonus: you can integrate more piece types!) which
supports Chess960 and is decently fast enough
Copyright (C) 2025-2026 winapiadmin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "fwd_decl.h"
#include "types.h"
#include <cstdint>
#include <stdexcept>
#include <string>
#include <string_view>
/// @file moves_io.h
namespace chess::uci {
/// @brief Convert a Move to UCI coordinate string (e.g. "e2e4", "e7e8q").
/// @param move The move.
/// @param chess960 Whether to use Chess960 castling encoding.
/// @return UCI string.
std::string moveToUci(Move move, bool chess960 = false);
/// @brief Convert a Square to algebraic notation (e.g. "e4").
/// @param sq The square.
/// @return Two-character string.
std::string squareToString(Square sq);
/// @brief Exception thrown when a SAN string represents an illegal move.
class IllegalMoveException : public std::exception {
public:
/// @brief Construct with an explanatory message.
IllegalMoveException(const std::string &message) : message_(message) {}
/**
* Provides the exception's message.
* @returns A C-string containing the exception message.
*/
const char *what() const noexcept override { return message_.c_str(); }
private:
std::string message_;
};
/**
* @brief Create an exception for an ambiguous SAN move.
* @param message The exception message.
*/
class AmbiguousMoveException : public std::exception {
public:
/// @brief Construct ambiguous-move exception with message.
AmbiguousMoveException(const std::string &message) : message_(message) {}
/**
* Provides the exception's message.
* @returns A C-string containing the exception message.
*/
const char *what() const noexcept override { return message_.c_str(); }
private:
std::string message_;
};
/// @brief Parse a UCI string into a Move for the given position.
/// @tparam T Piece enum type.
/// @tparam P Position tag.
/// @param pos The position.
/// @param uci UCI string (e.g. "e2e4").
/// @return The parsed Move.
template <typename T, typename P = void> Move uciToMove(const _Position<T, P> &pos, std::string_view uci);
/// @brief Parse a SAN string into a Move for the given position.
/// @tparam T Piece enum type.
/// @tparam P Position tag.
/// @param pos The position.
/// @param raw_san SAN string (e.g. "Nf3", "O-O").
/// @param remove_illegals If true, return Move::NO_MOVE instead of throwing.
/// @return The parsed Move.
template <typename T, typename P = void>
Move parseSan(const _Position<T, P> &pos, std::string_view raw_san, bool remove_illegals = false);
/// @copydoc parseSan
template <typename T, typename P = void>
inline Move parse_san(const _Position<T, P> &pos, std::string_view raw_san, bool remove_illegals = false) {
return parseSan(pos, raw_san, remove_illegals);
}
/// @brief Convert a Move to SAN string for the given position.
/// @tparam T Piece enum type.
/// @tparam P Position tag.
/// @param pos The position.
/// @param move The move.
/// @param long_ Use long algebraic notation.
/// @param suffix Include check/mate suffix (+/#).
/// @return SAN string.
template <typename T, typename P = void>
std::string moveToSan(const _Position<T, P> &pos, Move move, bool long_ = false, bool suffix = true);
/// @copydoc moveToSan
template <typename T, typename P = void>
inline std::string move_to_san(const _Position<T, P> &pos, Move move, bool long_ = false, bool suffix = true) {
return moveToSan(pos, move, long_, suffix);
}
} // namespace chess::uci