HooverChessUtils_PgnReader 0.9.0
Loading...
Searching...
No Matches
chessboard-types.h
Go to the documentation of this file.
1// Hoover Chess Utilities / PGN reader
2// Copyright (C) 2024-2025 Sami Kiminki
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17#ifndef HOOVER_CHESS_UTILS__PGN_READER__CHESSBOARD_TYPES_H_INCLUDED
18#define HOOVER_CHESS_UTILS__PGN_READER__CHESSBOARD_TYPES_H_INCLUDED
19
20#include <array>
21#include <bit>
22#include <cassert>
23#include <cinttypes>
24#include <cstdint>
25#include <string_view>
26#include <type_traits>
27
28
30{
31
34
38using SquareUnderlyingType = std::uint_fast8_t;
39
42using SquareCompactType = std::uint8_t;
43
47using ColorUnderlyingType = std::uint_fast8_t;
48
51using ColorCompactType = std::uint8_t;
52
56using PieceUnderlyingType = std::uint_fast8_t;
57
60using PieceCompactType = std::uint8_t;
61
65using PieceAndColorUnderlyingType = std::uint_fast8_t;
66
69using PieceAndColorCompactType = std::uint8_t;
70
74using PositionStatusUnderlyingType = std::uint_fast8_t;
75
78using PositionStatusCompactType = std::uint8_t;
79
83using RowColumnUnderlyingType = std::uint_fast8_t;
84
87using RowColumnCompactType = std::uint8_t;
88
93
122{
123 A1 = 0U,
124 B1 = 1U,
125 C1 = 2U,
126 D1 = 3U,
127 E1 = 4U,
128 F1 = 5U,
129 G1 = 6U,
130 H1 = 7U,
131 A2 = 8U,
132 B2 = 9U,
133 C2 = 10U,
134 D2 = 11U,
135 E2 = 12U,
136 F2 = 13U,
137 G2 = 14U,
138 H2 = 15U,
139 A3 = 16U,
140 B3 = 17U,
141 C3 = 18U,
142 D3 = 19U,
143 E3 = 20U,
144 F3 = 21U,
145 G3 = 22U,
146 H3 = 23U,
147 A4 = 24U,
148 B4 = 25U,
149 C4 = 26U,
150 D4 = 27U,
151 E4 = 28U,
152 F4 = 29U,
153 G4 = 30U,
154 H4 = 31U,
155 A5 = 32U,
156 B5 = 33U,
157 C5 = 34U,
158 D5 = 35U,
159 E5 = 36U,
160 F5 = 37U,
161 G5 = 38U,
162 H5 = 39U,
163 A6 = 40U,
164 B6 = 41U,
165 C6 = 42U,
166 D6 = 43U,
167 E6 = 44U,
168 F6 = 45U,
169 G6 = 46U,
170 H6 = 47U,
171 A7 = 48U,
172 B7 = 49U,
173 C7 = 50U,
174 D7 = 51U,
175 E7 = 52U,
176 F7 = 53U,
177 G7 = 54U,
178 H7 = 55U,
179 A8 = 56U,
180 B8 = 57U,
181 C8 = 58U,
182 D8 = 59U,
183 E8 = 60U,
184 F8 = 61U,
185 G8 = 62U,
186 H8 = 63U,
187
189 NONE = 64U,
190};
191
194{
196 WHITE = 0U,
197
199 BLACK = 8U,
200};
201
204{
205 NONE = 0U,
206 PAWN,
207 KNIGHT,
208 BISHOP,
209 ROOK,
210 QUEEN,
211 KING,
212};
213
219{
220 NONE = 0U,
221
222 WHITE_PAWN = 1U,
225 WHITE_ROOK,
227 WHITE_KING,
228
229 BLACK_PAWN = 9U,
232 BLACK_ROOK,
234 BLACK_KING,
235
236 WHITE_NONE = 0U,
237 BLACK_NONE = 8U,
238};
239
248{
249 NONE = 0U,
250
251 WHITE_PAWN = 1U,
254 WHITE_ROOK,
256 WHITE_KING,
257
258 BLACK_PAWN = 9U,
261 BLACK_ROOK,
263 BLACK_KING,
264
265 WHITE_NONE = 0U,
266 BLACK_NONE = 8U,
267};
268
276{
278 NORMAL,
279
281 CHECK,
282
284 STALEMATE,
285
287 MATE
288};
289
295constexpr inline bool isValidValue(Square sq) noexcept
296{
297 return (sq <= Square::NONE);
298}
299
306constexpr inline bool isValidSquare(Square sq) noexcept
307{
308 return (sq <= Square::H8);
309}
310
316constexpr inline Square makeSquare(RowColumn col, RowColumn row) noexcept
317{
318 assert(col <= 7U);
319 assert(row <= 7U);
320
321 [[assume(row <= 7U)]];
322 [[assume(col <= 7U)]];
323
324 return Square((row * 8U) + col);
325}
326
331constexpr inline RowColumn columnOf(Square sq) noexcept
332{
333 assert(sq <= Square::H8);
334 [[assume(sq <= Square::H8)]];
335
336 return static_cast<SquareUnderlyingType>(sq) & 7U;
337}
338
343constexpr inline RowColumn rowOf(Square sq) noexcept
344{
345 assert(sq <= Square::H8);
346 [[assume(sq <= Square::H8)]];
347
348 return static_cast<SquareUnderlyingType>(sq) / 8U;
349}
350
358constexpr inline Square getSquareForIndex(std::size_t index) noexcept
359{
360 assert(index <= 63U);
361 [[assume(index <= 63U)]];
362
363 return Square(index);
364}
365
372constexpr inline std::size_t getIndexOfSquare(Square sq) noexcept
373{
374 assert(sq <= Square::H8);
375 [[assume(sq <= Square::H8)]];
376
377 return static_cast<std::size_t>(sq);
378}
379
390constexpr inline Square addToSquareNoOverflowCheck(Square sq, std::int_fast8_t shift) noexcept
391{
392 return Square(static_cast<SquareUnderlyingType>(sq) + shift);
393}
394
400constexpr inline bool isValidValue(Color c) noexcept
401{
402 return c == Color::WHITE || c == Color::BLACK;
403}
404
409constexpr inline Color oppositeColor(Color c) noexcept
410{
411 return Color(static_cast<ColorUnderlyingType>(c) ^ 0x08U);
412}
413
418constexpr inline Color colorOf(PieceAndColor pc) noexcept
419{
422
423 return Color(static_cast<PieceAndColorUnderlyingType>(pc) & 0x8U);
424}
425
426
431constexpr inline bool isValidValue(Piece p) noexcept
432{
433 return p <= Piece::KING;
434}
435
440constexpr inline bool isValidValue(PieceAndColor pc) noexcept
441{
443}
444
449constexpr inline Piece pieceOf(PieceAndColor pc) noexcept
450{
451 assert(isValidValue(pc));
452 [[assume(pc <= PieceAndColor::BLACK_KING)]];
453
454 return Piece(static_cast<PieceAndColorUnderlyingType>(pc) & 0x7U);
455}
456
462constexpr inline PieceAndColor makePieceAndColor(Piece p, Color c) noexcept
463{
464 assert(isValidValue(p));
465 assert(isValidValue(c));
466
467 return PieceAndColor(
468 static_cast<PieceUnderlyingType>(p) |
469 static_cast<ColorUnderlyingType>(c));
470}
471
472
479{
480 return PieceAndColorCompact(static_cast<PieceAndColorUnderlyingType>(pc));
481}
482
488constexpr inline PieceAndColor toFastType(PieceAndColorCompact pc) noexcept
489{
490 return PieceAndColor(static_cast<PieceAndColorCompactType>(pc));
491}
492
493
495
496}
497
498#endif
std::uint_fast8_t PieceAndColorUnderlyingType
Underlying type of PieceAndColor
Definition chessboard-types.h:65
constexpr PieceAndColorCompact toCompactType(PieceAndColor pc) noexcept
Converts PieceAndColor (fast type) to PieceAndColorCompact (compact type).
Definition chessboard-types.h:478
std::uint8_t ColorCompactType
Compact type for Color. The compact type can store all legal enumeration values.
Definition chessboard-types.h:51
constexpr bool isValidSquare(Square sq) noexcept
Checks whether a value is a square.
Definition chessboard-types.h:306
constexpr RowColumn rowOf(Square sq) noexcept
Returns row number of square.
Definition chessboard-types.h:343
constexpr Color oppositeColor(Color c) noexcept
Flips the color.
Definition chessboard-types.h:409
std::uint_fast8_t ColorUnderlyingType
Underlying type of Color
Definition chessboard-types.h:47
std::uint_fast8_t SquareUnderlyingType
Underlying type of Square
Definition chessboard-types.h:38
constexpr Square addToSquareNoOverflowCheck(Square sq, std::int_fast8_t shift) noexcept
Adds to square. This function performs no overflow checking.
Definition chessboard-types.h:390
constexpr Piece pieceOf(PieceAndColor pc) noexcept
Returns piece of a piece and color enumeration value.
Definition chessboard-types.h:449
constexpr PieceAndColor toFastType(PieceAndColorCompact pc) noexcept
Converts PieceAndColorCompact (compact type) to PieceAndColor (fast type).
Definition chessboard-types.h:488
PieceAndColor
Named piece and color.
Definition chessboard-types.h:219
std::uint_fast8_t PositionStatusUnderlyingType
Underlying type of PositionStatus
Definition chessboard-types.h:74
constexpr std::size_t getIndexOfSquare(Square sq) noexcept
Returns an index for a square.
Definition chessboard-types.h:372
std::uint8_t PositionStatusCompactType
Compact type for PositionStatus. The compact type can store all legal enumeration values.
Definition chessboard-types.h:78
constexpr Color colorOf(PieceAndColor pc) noexcept
Returns color of a piece.
Definition chessboard-types.h:418
std::uint8_t SquareCompactType
Compact type for Square. The compact type can store all legal enumeration values.
Definition chessboard-types.h:42
std::uint8_t RowColumnCompactType
Compact type for RowColumn. The compact type can store all legal enumeration values.
Definition chessboard-types.h:87
constexpr PieceAndColor makePieceAndColor(Piece p, Color c) noexcept
Constructs a PieceAndColor enumeration value from Piece and Color.
Definition chessboard-types.h:462
Piece
Named piece.
Definition chessboard-types.h:204
PieceAndColorCompact
Named piece and color (compact representation)
Definition chessboard-types.h:248
Color
Color of a piece or side to move.
Definition chessboard-types.h:194
Square
Named square.
Definition chessboard-types.h:122
std::uint_fast8_t PieceUnderlyingType
Underlying type of Piece
Definition chessboard-types.h:56
std::uint_fast8_t RowColumnUnderlyingType
Underlying type of RowColumn
Definition chessboard-types.h:83
constexpr bool isValidValue(Square sq) noexcept
Checks whether a value is a valid enumeration value.
Definition chessboard-types.h:295
PositionStatus
Status of a position.
Definition chessboard-types.h:276
RowColumnUnderlyingType RowColumn
Row/column coordinate type. Valid range: [0, 7] signifying ranks 1–8 (as rows) or files A–H (as colum...
Definition chessboard-types.h:92
constexpr RowColumn columnOf(Square sq) noexcept
Returns column number of square.
Definition chessboard-types.h:331
constexpr Square makeSquare(RowColumn col, RowColumn row) noexcept
Constructs a square from column and row.
Definition chessboard-types.h:316
std::uint8_t PieceCompactType
Compact type for Piece. The compact type can store all legal enumeration values.
Definition chessboard-types.h:60
std::uint8_t PieceAndColorCompactType
Compact type for PieceAndColor. The compact type can store all legal enumeration values.
Definition chessboard-types.h:69
constexpr Square getSquareForIndex(std::size_t index) noexcept
Returns a square for an index. In essence, this is the ordinal of the square.
Definition chessboard-types.h:358
@ BLACK_NONE
Special value to make makePieceAndColor(Piece::NONE, Color::BLACK) well-defined.
@ WHITE_NONE
Special value to make makePieceAndColor(Piece::NONE, Color::WHITE) well-defined.
@ BLACK
Black piece or black side to move.
@ WHITE
White piece or white side to move.
@ NORMAL
Regular position (not in check, mate, or stalemate)
@ CHECK
King is checked (but not mated)
@ NONE
Null token (placeholder only)
Definition pgnscannertokens.h:39
Definition chessboard-types-squareset.h:30