stellar

UCI Chess engine written in C++20
git clone git://git.dimitrijedobrota.com/stellar.git
Log | Files | Refs | README | LICENSE |

score.hpp (6899B)


1 #ifndef STELLAR_SCORE_H 2 #define STELLAR_SCORE_H 3 4 #include "utils.hpp" 5 #define MAX_PLY 64 6 7 #define SCORE_INFINITY 32000 8 #define MATE_VALUE 31000 9 #define MATE_SCORE 30000 10 11 namespace score { 12 13 inline constexpr const uint16_t value[2][6] = { 14 {82, 337, 365, 477, 1025, 12000}, 15 {94, 201, 297, 512, 936, 12000}, 16 }; 17 18 inline constexpr const int16_t position[2][6][64] = { 19 // clang-format off 20 { 21 { 22 0, 0, 0, 0, 0, 0, 0, 0, 23 -35, -1, -20, -23, -15, 24, 38, -22, 24 -26, -4, -4, -10, 3, 3, 33, -12, 25 -27, -2, -5, 12, 17, 6, 10, -25, 26 -14, 13, 6, 21, 23, 12, 17, -23, 27 -6, 7, 26, 31, 65, 56, 25, -20, 28 98, 134, 61, 95, 68, 126, 34, -11, 29 0, 0, 0, 0, 0, 0, 0, 0, 30 }, { 31 -105, -21, -58, -33, -17, -28, -19, -23, 32 -29, -53, -12, -3, -1, 18, -14, -19, 33 -23, -9, 12, 10, 19, 17, 25, -16, 34 -13, 4, 16, 13, 28, 19, 21, -8, 35 -9, 17, 19, 53, 37, 69, 18, 22, 36 -47, 60, 37, 65, 84, 129, 73, 44, 37 -73, -41, 72, 36, 23, 62, 7, -17, 38 -167, -89, -34, -49, 61, -97, -15, -107, 39 }, { 40 -33, -3, -14, -21, -13, -12, -39, -21, 41 4, 15, 16, 0, 7, 21, 33, 1, 42 0, 15, 15, 15, 14, 27, 18, 10, 43 -6, 13, 13, 26, 34, 12, 10, 4, 44 -4, 5, 19, 50, 37, 37, 7, -2, 45 -16, 37, 43, 40, 35, 50, 37, -2, 46 -26, 16, -18, -13, 30, 59, 18, -47, 47 -29, 4, -82, -37, -25, -42, 7, -8, 48 }, { 49 -19, -13, 1, 17, 16, 7, -37, -26, 50 -44, -16, -20, -9, -1, 11, -6, -71, 51 -45, -25, -16, -17, 3, 0, -5, -33, 52 -36, -26, -12, -1, 9, -7, 6, -23, 53 -24, -11, 7, 26, 24, 35, -8, -20, 54 -5, 19, 26, 36, 17, 45, 61, 16, 55 27, 32, 58, 62, 80, 67, 26, 44, 56 32, 42, 32, 51, 63, 9, 31, 43, 57 }, { 58 -1, -18, -9, 10, -15, -25, -31, -50, 59 -35, -8, 11, 2, 8, 15, -3, 1, 60 -14, 2, -11, -2, -5, 2, 14, 5, 61 -9, -26, -9, -10, -2, -4, 3, -3, 62 -27, -27, -16, -16, -1, 17, -2, 1, 63 -13, -17, 7, 8, 29, 56, 47, 57, 64 -24, -39, -5, 1, -16, 57, 28, 54, 65 -28, 0, 29, 12, 59, 44, 43, 45, 66 }, { 67 -15, 36, 12, -54, 8, -28, 24, 14, 68 1, 7, -8, -64, -43, -16, 9, 8, 69 -14, -14, -22, -46, -44, -30, -15, -27, 70 -49, -1, -27, -39, -46, -44, -33, -51, 71 -17, -20, -12, -27, -30, -25, -14, -36, 72 -9, 24, 2, -16, -20, 6, 22, -22, 73 29, -1, -20, -7, -8, -4, -38, -29, 74 -65, 23, 16, -15, -56, -34, 2, 13, 75 } 76 }, { 77 { 78 0, 0, 0, 0, 0, 0, 0, 0, 79 13, 8, 8, 10, 13, 0, 2, -7, 80 4, 7, -6, 1, 0, -5, -1, -8, 81 13, 9, -3, -7, -7, -8, 3, -1, 82 32, 24, 13, 5, -2, 4, 17, 17, 83 94, 100, 85, 67, 56, 53, 82, 84, 84 178, 173, 158, 134, 147, 132, 165, 187, 85 0, 0, 0, 0, 0, 0, 0, 0, 86 }, { 87 -29, -51, -23, -15, -22, -18, -50, -64, 88 -42, -20, -10, -5, -2, -20, -23, -44, 89 -23, -3, -1, 15, 10, -3, -20, -22, 90 -18, -6, 16, 25, 16, 17, 4, -18, 91 -17, 3, 22, 22, 22, 11, 8, -18, 92 -24, -20, 10, 9, -1, -9, -19, -41, 93 -25, -8, -25, -2, -9, -25, -24, -52, 94 -58, -38, -13, -28, -31, -27, -63, -99, 95 }, { 96 -23, -9, -23, -5, -9, -16, -5, -17, 97 -14, -18, -7, -1, 4, -9, -15, -27, 98 -12, -3, 8, 10, 13, 3, -7, -15, 99 -6, 3, 13, 19, 7, 10, -3, -9, 100 -3, 9, 12, 9, 14, 10, 3, 2, 101 2, -8, 0, -1, -2, 6, 0, 4, 102 -8, -4, 7, -12, -3, -13, -4, -14, 103 -14, -21, -11, -8, -7, -9, -17, -24, 104 }, { 105 -9, 2, 3, -1, -5, -13, 4, -20, 106 -6, -6, 0, 2, -9, -9, -11, -3, 107 -4, 0, -5, -1, -7, -12, -8, -16, 108 3, 5, 8, 4, -5, -6, -8, -11, 109 4, 3, 13, 1, 2, 1, -1, 2, 110 7, 7, 7, 5, 4, -3, -5, -3, 111 11, 13, 13, 11, -3, 3, 8, 3, 112 13, 10, 18, 15, 12, 12, 8, 5, 113 }, { 114 -33, -28, -22, -43, -5, -32, -20, -41, 115 -22, -23, -30, -16, -16, -23, -36, -32, 116 -16, -27, 15, 6, 9, 17, 10, 5, 117 -18, 28, 19, 47, 31, 34, 39, 23, 118 3, 22, 24, 45, 57, 40, 57, 36, 119 -20, 6, 9, 49, 47, 35, 19, 9, 120 -17, 20, 32, 41, 58, 25, 30, 0, 121 -9, 22, 22, 27, 27, 19, 10, 20, 122 }, { 123 -53, -34, -21, -11, -28, -14, -24, -43, 124 -27, -11, 4, 13, 14, 4, -5, -17, 125 -19, -3, 11, 21, 23, 16, 7, -9, 126 -18, -4, 21, 24, 27, 23, 9, -11, 127 -8, 22, 24, 27, 26, 33, 26, 3, 128 10, 17, 23, 15, 20, 45, 44, 13, 129 -12, 17, 14, 17, 17, 38, 23, 11, 130 -74, -35, -18, -18, -11, 15, 4, -17, 131 } 132 } 133 // clang-format on 134 }; 135 136 inline constexpr const uint16_t phase_opening = 6192; 137 inline constexpr const uint16_t phase_endgame = 518; 138 139 inline constexpr int16_t interpolate(int16_t phase_score, int16_t opening, int16_t endgame) { 140 return (opening * phase_score + endgame * (phase_opening - phase_score)) / phase_opening; 141 } 142 143 enum Phase { 144 OPENING, 145 ENDGAME, 146 }; 147 148 inline constexpr int16_t get(const piece::Type piece, const Phase phase = OPENING) { 149 return value[to_underlying(phase)][to_underlying(piece)]; 150 } 151 152 inline constexpr int16_t get(const piece::Type piece, const color::Color color, const square::Square square, 153 const Phase phase = ENDGAME) { 154 uint8_t square_i = to_underlying(color == color::WHITE ? square : square::mirror(square)); 155 return position[to_underlying(phase)][to_underlying(piece)][square_i]; 156 } 157 158 inline constexpr const uint8_t pawn_double_opening = 5; 159 inline constexpr const uint8_t pawn_double_endgame = 10; 160 inline constexpr const uint8_t pawn_isolated_opening = 5; 161 inline constexpr const uint8_t pawn_isolated_endgame = 10; 162 163 inline constexpr const std::array<std::array<int16_t, 8>, 2> pawn_passed = { 164 {{0, 10, 30, 50, 75, 100, 150, 200}, {200, 150, 100, 75, 50, 30, 10, 0}}}; 165 166 inline constexpr const uint8_t file_open_semi = 10; 167 inline constexpr const uint8_t file_open = 15; 168 169 } // namespace score 170 171 #endif