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