-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.cpp
executable file
·167 lines (138 loc) · 4.6 KB
/
init.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**********************************************************************
|
| Bogart
| Chess Engine
|
| Copyright (C) 2009-2013 Dr.Kameleon
|
|**********************************************************************
| init.cpp
|**********************************************************************/
#include "bogart.h"
//=======================================================
// Global Variables
//=======================================================
U64_HASH ROOK_SLIDES[64];
U64_HASH BISHOP_SLIDES[64];
U64_HASH WPAWN_SLIDES[64];
U64_HASH BPAWN_SLIDES[64];
U64 ZOBRIST_HASHKEY[3][13][64];
INT_HASH ZOBRIST;
UINT SQUARE_DISTANCE[64][64];
//=======================================================
// Main Initialization Method
//=======================================================
void Init::all()
{
// Square Distance
Init::squareDistance();
// Zobrist Keys
Init::zobristKeys();
// Slider Tables
Init::rookSlides();
Init::bishopSlides();
Init::pawnSlides();
}
//=======================================================
// Square Distance
//=======================================================
void Init::squareDistance()
{
for (int sq1=0; sq1<64; sq1++)
{
for (int sq2=0; sq2<64; sq2++)
{
int rowDist = abs (NROW(sq2) - NROW(sq1));
int colDist = abs (NCOL(sq2) - NCOL(sq1));
SQUARE_DISTANCE[sq1][sq2] = max(rowDist,colDist);
}
}
}
//=======================================================
// Zobrist Keys Initialization
//=======================================================
void Init::zobristKeys()
{
for (int playing=0; playing<3; playing++)
for (int piece=0; piece<13; piece++)
for (int pos=0; pos<64; pos++)
ZOBRIST_HASHKEY[playing][piece][pos] = Bitboard::getRandom();
}
//=======================================================
// Slider Tables Initialization
//=======================================================
void Init::rookSlides()
{
for (int pos=0; pos<64; pos++)
{
U64 n = ROOK_MASK[pos] ^ (1ULL<<pos); // Get all possible moves for a Rook at pos, except pos
vector<UINT> bits = BITSSET(n); // Get all possible positions
LOOP14
{
U64 num = n;
for (int i=0; i<14; i++) if (loop[i]==0) num &= ~(1ULL<<bits[i]); // Get combination using pattern
U64 res = 0ULL, bit; int sh; bool go;
/* UP */ RESET_POS; while ((sh+8<64)&&(go)) { sh+=8; bit = 1ULL<<sh; res |= bit; (num&bit)?go=false:go; }
/* DOWN */ RESET_POS; while ((sh-8>=0)&&(go)) { sh-=8; bit = 1ULL<<sh; res |= bit; (num&bit)?go=false:go; }
/* LEFT */ RESET_POS; while ((sh+1<64)&&(ROW_MASK[sh+1]&ROW_MASK[pos])&&(go)) { sh+=1; bit = 1ULL<<sh; res |= bit; (num&bit)?go=false:go; }
/* RIGHT */ RESET_POS; while ((sh-1>=0)&&(ROW_MASK[sh-1]&ROW_MASK[pos])&&(go)) { sh-=1; bit = 1ULL<<sh; res |= bit; (num&bit)?go=false:go; }
ROOK_SLIDES[pos][num|(1ULL<<pos)] = res;
}
}
}
void Init::bishopSlides()
{
for (int pos=0; pos<64; pos++)
{
U64 n = BISHOP_MASK[pos] ^ (1ULL<<pos); // Get all possible moves for a Bishop at pos, except pos
vector<UINT> bits = BITSSET(n); // Get all possible positions
LOOP14
{
U64 num = n;
for (int i=0; i<14; i++) if (loop[i]==0) num &= ~(1ULL<<bits[i]); // Get combination using pattern
U64 res = 0ULL, bit; int sh; bool go;
/* LEFT-UP */ RESET_POS; while ((sh+9<64)&&((sh+9)%8>sh%8)&&(go)) { sh+=9; bit = 1ULL<<sh; res |= bit; (num&bit)?go=false:go; }
/* LEFT-DOWN */ RESET_POS; while ((sh-7>=0)&&((sh-7)%8>sh%8)&&(go)) { sh-=7; bit = 1ULL<<sh; res |= bit; (num&bit)?go=false:go; }
/* RIGHT-UP */ RESET_POS; while ((sh+7<64)&&((sh+7)%8<sh%8)&&(go)) { sh+=7; bit = 1ULL<<sh; res |= bit; (num&bit)?go=false:go; }
/* RIGHT-DOWN */ RESET_POS; while ((sh-9>=0)&&((sh-9)%8<sh%8)&&(go)) { sh-=9; bit = 1ULL<<sh; res |= bit; (num&bit)?go=false:go; }
BISHOP_SLIDES[pos][num|(1ULL<<pos)] = res;
}
}
}
void Init::pawnSlides()
{
for (int pos=0; pos<64; pos++)
{
U64 n = WPAWN_MASK[pos];
vector<UINT> bits = BITSSET(n);
if (bits.size()==1)
{
WPAWN_SLIDES[pos][n] = 0ULL;
WPAWN_SLIDES[pos][0] = n;
}
else // bits = 2 (first move)
{
WPAWN_SLIDES[pos][n] = 0ULL;
WPAWN_SLIDES[pos][n^(1ULL<<(pos+8))] = 1ULL<<(pos+8);
WPAWN_SLIDES[pos][n^(1ULL<<(pos+16))] = 0ULL;
WPAWN_SLIDES[pos][0] = n;
}
}
for (int pos=0; pos<64; pos++)
{
U64 n = BPAWN_MASK[pos];
vector<UINT> bits = BITSSET(n);
if (bits.size()==1)
{
BPAWN_SLIDES[pos][n] = 0ULL;
BPAWN_SLIDES[pos][0] = n;
}
else // bits = 2 (first move)
{
BPAWN_SLIDES[pos][n] = 0ULL;
BPAWN_SLIDES[pos][n^(1ULL<<(pos-8))] = 1ULL<<(pos-8);
BPAWN_SLIDES[pos][n^(1ULL<<(pos-16))] = 0ULL;
BPAWN_SLIDES[pos][0] = n;
}
}
}