-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboardstate.cpp
110 lines (81 loc) · 2.35 KB
/
boardstate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "boardstate.h"
#include "board.h"
#include <algorithm>
#include <random>
using namespace std;
void Boardstate::HashPiece(int piece, int square)
{
this->position_key ^= Boardstate::piece_keys[piece][square];
}
void Boardstate::HashEnpassant(int square)
{
this->position_key ^= Boardstate::enpassant_keys[square];
}
void Boardstate::HashCastle(int castle)
{
this->position_key ^= Boardstate::castle_keys[castle];
}
void Boardstate::HashSideToMove()
{
this->position_key ^= Boardstate::side_to_move_key;
}
void Boardstate::GenerateNewKey()
{
this->position_key = 0;
//piece square
for (int piece = WHITE_PAWN; piece <= BLACK_KING; ++piece)
{
for (Bitboard cpy_bitboard = this->bitboards[piece]; cpy_bitboard; cpy_bitboard.PopBit())
{
int square = cpy_bitboard.GetLeastSigBit();
this->position_key ^= Boardstate::piece_keys[piece][square];
}
}
//enpassant
if (this->enpassant_square != INVALID_SQUARE) this->position_key ^= Boardstate::enpassant_keys[this->enpassant_square];
//castle
this->position_key ^= Boardstate::castle_keys[this->castle];
//side to move
if (side_to_move == BLACK) this->position_key ^= Boardstate::side_to_move_key;
}
void Boardstate::Clear()
{
fill(begin(this->bitboards), end(this->bitboards), 0);
fill(begin(this->occupancies), end(this->occupancies), 0);
this->side_to_move = WHITE;
this->castle = 0;
this->enpassant_square = INVALID_SQUARE;
this->fifty_moves = 0;
this->position_key = 0;
}
Boardstate::Boardstate() : bitboards{}, occupancies{}, side_to_move(WHITE), castle(0), enpassant_square(INVALID_SQUARE), fifty_moves(0), position_key(0)
{
}
//static members
U64 Boardstate::piece_keys[12][64] = {};
U64 Boardstate::enpassant_keys[64] = {};
U64 Boardstate::castle_keys[16] = {};
U64 Boardstate::side_to_move_key = {};
void Boardstate::InitZobristKeys()
{
random_device dev;
mt19937_64 rng(dev());
uniform_int_distribution<U64> distribution;
for (int piece = WHITE_PAWN; piece <= BLACK_KING; ++piece)
{
for (int square = 0; square < 64; ++square)
{
Boardstate::piece_keys[piece][square] = distribution(rng);
}
}
for (int square = 0; square < 64; ++square)
{
Boardstate::enpassant_keys[square] = distribution(rng);
}
// WK | WQ | BK | BQ == 16
for (int i = 0; i < 16; ++i)
{
Boardstate::castle_keys[i] = distribution(rng);
}
Boardstate::side_to_move_key = distribution(rng);
}