BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
Material.hpp
Go to the documentation of this file.
1/*
2 * ======================================================================================
3 *
4 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
5 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
6 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
7 * ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
8 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
9 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
10 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
11 *
12 * ======================================================================================
13 */
14
19
20#pragma once
21
22#include <cstdint>
27
28namespace ben_bot::eval {
29
30using chess::game::Position;
32
36namespace piece_values {
37
40
42 inline constexpr Value PAWN { UINT16_C(100) };
43
45 inline constexpr Value KNIGHT { UINT16_C(320) };
46
48 inline constexpr Value BISHOP { UINT16_C(330) };
49
51 inline constexpr Value ROOK { UINT16_C(500) };
52
54 inline constexpr Value QUEEN { UINT16_C(900) };
55
60 inline constexpr Value KING { UINT16_C(10000) };
61
63
67 [[nodiscard, gnu::const]] constexpr auto get(PieceType type) noexcept -> Value;
68
69} // namespace piece_values
70
76[[nodiscard, gnu::const]] constexpr auto score_material(const Position& position) noexcept -> Value;
77
78/*
79 ___ ,--,
80 ,---, ,--.'|_ ,--, ,--.'|
81 ,---.'| | | :,' ,--.'| | | :
82 | | : : : ' : | |, : : ' .--.--.
83 | | | ,---. .;__,' / ,--.--. `--'_ | ' | / / '
84 ,--.__| | / \ | | | / \ ,' ,'| ' | | | : /`./
85 / ,' | / / |:__,'| : .--. .-. | ' | | | | : | : ;_
86. ' / |. ' / | ' : |__ \__\/: . . | | : ' : |__ \ \ `.
87' ; |: |' ; /| | | '.'| ," .--.; | ' : |__ | | '.'| `----. \
88| | '/ '' | / | ; : ;/ / ,. | | | '.'|; : ;/ /`--' /__ ___ ___
89| : :|| : | | , /; : .' \; : ;| , /'--'. / .\/ .\/ .\
90 \ \ / \ \ / ---`-' | , .-./| , / ---`-' `--'---'\ ; \ ; \ ; |
91 `----' `----' `--`---' ---`-' `--" `--" `--"
92
93 */
94
95namespace piece_values {
96
97 constexpr auto get(const PieceType type) noexcept -> Value
98 {
99 switch (type) {
100 case PieceType::Pawn : return PAWN;
101 case PieceType::Knight: return KNIGHT;
102 case PieceType::Bishop: return BISHOP;
103 case PieceType::Rook : return ROOK;
104 case PieceType::Queen : return QUEEN;
105 default : [[fallthrough]];
106 case PieceType::King : return KING;
107 }
108 }
109
110} // namespace piece_values
111
112namespace detail {
113
114 template <bool IncludePawns = true>
115 [[nodiscard, gnu::const]] constexpr auto count_material(
116 const chess::board::Pieces& pieces) noexcept
117 -> Value
118 {
119 auto total = (static_cast<Value>(pieces.knights.count()) * piece_values::KNIGHT)
120 + (static_cast<Value>(pieces.bishops.count()) * piece_values::BISHOP)
121 + (static_cast<Value>(pieces.rooks.count()) * piece_values::ROOK)
122 + (static_cast<Value>(pieces.queens.count()) * piece_values::QUEEN);
123
124 if constexpr (IncludePawns) {
125 total += (static_cast<Value>(pieces.pawns.count()) * piece_values::PAWN);
126 }
127
128 return static_cast<Value>(total);
129 }
130
131} // namespace detail
132
133constexpr auto score_material(const Position& position) noexcept -> Value
134{
135 return detail::count_material(position.our_pieces())
136 - detail::count_material(position.their_pieces());
137}
138
139} // namespace ben_bot::eval
constexpr auto get(PieceType type) noexcept -> Value
Definition Material.hpp:97
std::int16_t Value
Definition Score.hpp:38
constexpr auto score_material(const Position &position) noexcept -> Value
Definition Material.hpp:133
constexpr auto count_material(const chess::board::Pieces &pieces) noexcept -> Value
Definition Material.hpp:115
chess::pieces::Type PieceType
Definition Material.hpp:31