stellar

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

zobrist.hpp (2289B)


0 #ifndef STELLAR_ZOBRIST_H
1 #define STELLAR_ZOBRIST_H
3 #include "piece.hpp"
4 #include "random.hpp"
6 #include <algorithm>
7 #include <array>
8 #include <random>
10 class Board;
11 class Zobrist {
12 public:
13 Zobrist() = delete;
15 static inline U64 hash(const Board &board);
16 static inline constexpr U64 key_side(void) { return keys_side; }
17 static inline constexpr U64 key_castle(int exp) { return keys_castle[exp]; }
18 static inline constexpr U64 key_enpassant(square::Square square) {
19 return keys_enpassant[to_underlying(square)];
20 }
21 static inline constexpr U64 key_piece(piece::Type type, color::Color color, square::Square square) {
22 return keys_piece[piece::get_index(type, color)][to_underlying(square)];
23 }
25 private:
26 typedef std::array<std::array<U64, 64>, 12> key_piece_array;
27 static inline constexpr const key_piece_array keys_piece = []() constexpr -> key_piece_array {
28 key_piece_array key_piece;
29 Random gen(C64(1804289383));
30 for (piece::Type type : piece::TypeIter()) {
31 int piece_index_white = piece::get(type, color::Color::WHITE).index;
32 int piece_index_black = piece::get(type, color::Color::BLACK).index;
33 for (int square = 0; square < 64; square++) {
34 key_piece[piece_index_white][square] = gen();
35 key_piece[piece_index_black][square] = gen();
36 }
37 }
38 return key_piece;
39 }();
41 typedef std::array<U64, 64> key_enpassant_array;
42 static inline constexpr const key_enpassant_array keys_enpassant = []() constexpr -> key_enpassant_array {
43 key_enpassant_array key_enpassant;
44 Random gen(C32(337245213));
45 for (int castle = 0; castle < 64; castle++) {
46 key_enpassant[castle] = gen();
47 }
48 return key_enpassant;
49 }();
51 typedef std::array<U64, 16> key_castle_array;
52 static inline constexpr const key_castle_array keys_castle = []() constexpr -> key_castle_array {
53 key_castle_array key_castle;
54 Random gen(C32(3642040919));
55 for (int castle = 0; castle < 16; castle++) {
56 key_castle[castle] = gen();
57 }
58 return key_castle;
59 }();
61 static inline constexpr const U64 keys_side = Random(C32(1699391443))();
62 };
64 #endif