-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinters.cpp
More file actions
167 lines (155 loc) · 6.08 KB
/
printers.cpp
File metadata and controls
167 lines (155 loc) · 6.08 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
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/>.
*/
#include "printers.h"
#include "moves_io.h"
#include "position.h"
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <unordered_map>
namespace chess {
template <typename T> using DescriptiveNameNotation = std::unordered_map<T, std::string>;
template <typename PieceC, typename> std::ostream &operator<<(std::ostream &os, const _Position<PieceC, void> &pos) {
// RAII guard to save/restore stream state
struct ios_guard {
std::ostream &strm;
std::ios::fmtflags flags;
std::streamsize prec;
std::ostream::char_type fill;
ios_guard(std::ostream &s) : strm(s), flags(s.flags()), prec(s.precision()), fill(s.fill()) {}
~ios_guard() {
strm.flags(flags);
strm.precision(prec);
strm.fill(fill);
}
} guard(os);
os << "\n +---+---+---+---+---+---+---+---+\n";
for (Rank r = RANK_8; r >= RANK_1; --r) {
for (File f = FILE_A; f <= FILE_H; ++f)
os << " | " << pos.piece_on(make_sq(r, f));
os << " | " << (1 + r) << "\n +---+---+---+---+---+---+---+---+\n";
}
os << " a b c d e f g h\n";
// Ensure key is printed in hex, but restores after this function
os << "\nFen: " << pos.fen() << "\nKey: " << std::hex << std::uppercase << std::setfill('0') << std::setw(16) << pos.key()
<< '\n';
return os;
}
std::ostream &operator<<(std::ostream &os, const Move mv) {
os << uci::moveToUci(mv);
return os;
}
std::ostream &operator<<(std::ostream &os, const Color c) {
DescriptiveNameNotation<Color> colors = {
{ WHITE, "WHITE" },
{ BLACK, "BLACK" },
{ COLOR_NB, "COLOR_NB" }
};
return os << colors[c];
}
std::ostream &operator<<(std::ostream &os, const PieceType c) {
DescriptiveNameNotation<PieceType> pieces = {
{ NO_PIECE_TYPE, "NO_PIECE_TYPE/ALL_PIECES" },
{ PAWN, "PAWN" },
{ KNIGHT, "KNIGHT" },
{ BISHOP, "BISHOP" },
{ ROOK, "ROOK" },
{ QUEEN, "QUEEN" },
{ KING, "KING" },
{ PIECE_TYPE_NB, "PIECE_TYPE_NB" },
};
return os << pieces[c];
}
std::ostream &operator<<(std::ostream &os, const CastlingRights cr) {
DescriptiveNameNotation<CastlingRights> castlingFlags = {
{ NO_CASTLING, "NO_CASTLING" },
{ WHITE_OO, "WHITE_OO" },
{ WHITE_OOO, "WHITE_OOO" },
{ BLACK_OO, "BLACK_OO" },
{ BLACK_OOO, "BLACK_OOO" },
{ WHITE_CASTLING, "WHITE_CASTLING" },
{ BLACK_CASTLING, "BLACK_CASTLING" },
{ KING_SIDE, "KING_SIDE" },
{ QUEEN_SIDE, "QUEEN_SIDE" },
{ ANY_CASTLING, "ANY_CASTLING" },
{ WHITE_OOO | BLACK_OO, "WHITE_OOO | BLACK_OO" },
{ WHITE_OO | BLACK_OOO, "WHITE_OO | BLACK_OOO" },
{ WHITE_OO | WHITE_OOO | BLACK_OO, "WHITE_OO | WHITE_OOO | BLACK_OO" },
{ WHITE_OO | WHITE_OOO | BLACK_OOO, "WHITE_OO | WHITE_OOO | BLACK_OOO" },
{ WHITE_OO | BLACK_OO | BLACK_OOO, "WHITE_OO | BLACK_OO | BLACK_OOO" },
{ WHITE_OOO | BLACK_OO | BLACK_OOO, "WHITE_OOO | BLACK_OO | BLACK_OOO" },
};
return os << castlingFlags[cr];
}
static std::string str_toupper(std::string s) {
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::toupper(c); });
return s;
}
std::ostream &operator<<(std::ostream &os, const Square sq) {
os << "SQ_" << str_toupper(chess::uci::squareToString(sq));
return os;
}
template <typename PieceC, typename> std::ostream &operator<<(std::ostream &os, PieceC p) {
char c = '.';
switch (p) {
case PieceC::WPAWN:
c = 'P';
break;
case PieceC::BPAWN:
c = 'p';
break;
case PieceC::WKNIGHT:
c = 'N';
break;
case PieceC::BKNIGHT:
c = 'n';
break;
case PieceC::WBISHOP:
c = 'B';
break;
case PieceC::BBISHOP:
c = 'b';
break;
case PieceC::WROOK:
c = 'R';
break;
case PieceC::BROOK:
c = 'r';
break;
case PieceC::WQUEEN:
c = 'Q';
break;
case PieceC::BQUEEN:
c = 'q';
break;
case PieceC::WKING:
c = 'K';
break;
case PieceC::BKING:
c = 'k';
break;
default:
break;
}
return os << c;
}
#define INSTANTITATE(PieceC) \
template std::ostream &operator<<(std::ostream &, const _Position<PieceC, void> &); \
template std::ostream &operator<<(std::ostream &, PieceC);
INSTANTITATE(EnginePiece)
INSTANTITATE(PolyglotPiece)
INSTANTITATE(ContiguousMappingPiece)
} // namespace chess