stellar

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

zobrist.hpp (1584B)


      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 namespace zobrist {
     13 
     14 extern U32 keys_pawn[2][64];
     15 extern U64 keys_piece[2][12][64];
     16 extern U64 keys_enpassant[64];
     17 extern U64 keys_castle[16];
     18 
     19 const U64 keys_side = Random(C32(1699391443))();
     20 
     21 inline void init() {
     22     Random gen1(C64(1804289383));
     23     for (Type type = PAWN; type <= KING; ++type) {
     24         for (int square = 0; square < 64; square++) {
     25             keys_piece[WHITE][type][square] = gen1();
     26             keys_piece[BLACK][type][square] = gen1();
     27         }
     28     }
     29 
     30     Random gen2(C32(337245213));
     31     for (int castle = 0; castle < 64; castle++) {
     32         keys_enpassant[castle] = gen2();
     33     }
     34 
     35     Random gen3(C32(3642040919));
     36     for (int castle = 0; castle < 16; castle++) {
     37         keys_castle[castle] = gen3();
     38     }
     39 
     40     Random gen4(C32(3642040919));
     41     for (int c = 0; c < 2; c++) {
     42         for (int square = 0; square < 64; square++) {
     43             keys_pawn[c][square] = gen4.get_U32();
     44         }
     45     }
     46 };
     47 
     48 inline U64 hash(const Board &board);
     49 inline U32 hash_pawn(const Board &board);
     50 
     51 inline U64 key_side() { return keys_side; }
     52 inline U64 key_castle(int exp) { return keys_castle[exp]; }
     53 inline U64 key_enpassant(Square square) { return keys_enpassant[square]; }
     54 inline U64 key_pawn(Color color, Square square) { return keys_pawn[color][square]; }
     55 inline U64 key_piece(Type type, Color color, Square square) {
     56     return keys_piece[color][type][square];
     57 }
     58 
     59 }; // namespace zobrist
     60 
     61 #endif