square.hpp (2016B)
1 #ifndef STELLAR_SQUARE_H 2 #define STELLAR_SQUARE_H 3 4 #include "utils.hpp" 5 #include <string> 6 7 namespace square { 8 9 enum Square { 10 // clang-format off 11 a1, b1, c1, d1, e1, f1, g1, h1, 12 a2, b2, c2, d2, e2, f2, g2, h2, 13 a3, b3, c3, d3, e3, f3, g3, h3, 14 a4, b4, c4, d4, e4, f4, g4, h4, 15 a5, b5, c5, d5, e5, f5, g5, h5, 16 a6, b6, c6, d6, e6, f6, g6, h6, 17 a7, b7, c7, d7, e7, f7, g7, h7, 18 a8, b8, c8, d8, e8, f8, g8, h8, no_sq 19 // clang-format on 20 }; 21 22 typedef Iterator<Square, Square::a1, Square::h8> Iter; 23 24 inline constexpr const Square mirror_array[]{ 25 // clang-format off 26 a8, b8, c8, d8, e8, f8, g8, h8, 27 a7, b7, c7, d7, e7, f7, g7, h7, 28 a6, b6, c6, d6, e6, f6, g6, h6, 29 a5, b5, c5, d5, e5, f5, g5, h5, 30 a4, b4, c4, d4, e4, f4, g4, h4, 31 a3, b3, c3, d3, e3, f3, g3, h3, 32 a2, b2, c2, d2, e2, f2, g2, h2, 33 a1, b1, c1, d1, e1, f1, g1, h1, no_sq 34 // clang-format on 35 }; 36 37 inline constexpr const char *coordinates_array[] = { 38 // clang-format off 39 "a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1", 40 "a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2", 41 "a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3", 42 "a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4", 43 "a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5", 44 "a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6", 45 "a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7", 46 "a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8", " " 47 // clang-format on 48 }; 49 50 inline constexpr const uint8_t file(const Square square) { return to_underlying(square) & 0x07; } 51 inline constexpr const uint8_t rank(const Square square) { return to_underlying(square) >> 3; } 52 inline constexpr const Square mirror(const Square square) { return mirror_array[square]; } 53 54 inline constexpr const std::string to_coordinates(const Square square) { 55 return coordinates_array[to_underlying(square)]; 56 } 57 58 inline const Square from_coordinates(const std::string &cord) { 59 return static_cast<Square>((cord[1] - '1') * 8 + (cord[0] - 'a')); 60 } 61 62 } // namespace square 63 64 #endif