-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.cpp
453 lines (402 loc) · 12.4 KB
/
board.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#include "board.hpp"
#define GET(bits, x, y) ((bool)(bits & (0x1ULL << ( (7-x) + 8 * (7-y) ))))
#define FLIP(bits, x, y) bits ^= (0x1ULL << ( (7-x) + 8 * (7-y) ))
#define BLANK 0x0000000000000000ULL
#define LEFT_MASK 0xfefefefefefefefeULL
#define RIGHT_MASK 0x7f7f7f7f7f7f7f7fULL
static Direction directions[8] = {NW, N, NE, E, SE, S, SW, W};
#define PIECE_HASH(x, y, side) pieceHash[side*64 + y*8 + x]
#define MOVE_HASH(x, y, direction, length) moveHash[direction*384 + (length-1)*64 + y*8 + x]
bool Board::isHashInit = false;
unsigned long long Board::pieceHash[128];
unsigned long long Board::moveHash[3072];
unsigned long long Board::defaultHash;
void Board::initHash(){
mt19937_64 generator(1337);
for (int side = 0; side <= 1; side++) {
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
PIECE_HASH(x, y, side) = generator();
}
}
}
defaultHash = PIECE_HASH(3, 3, WHITE) ^ PIECE_HASH(4, 3, BLACK)
^ PIECE_HASH(3, 4, BLACK) ^ PIECE_HASH(4, 4, WHITE);
// NW, N, NE, E, SE, S, SW, W
int xDirectionDelta[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int yDirectionDelta[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
for (int dir = 0; dir < 8; dir++) {
for (int len = 1; len <= 6; len++) {
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
bool isValidMove = true;
unsigned long long hash = BLANK;
int length = len;
int yPos = y;
int xPos = x;
while (length > 0) {
yPos += yDirectionDelta[dir];
xPos += xDirectionDelta[dir];
length --;
if((yDirectionDelta[dir] != 0 && (yPos <= 0 || yPos >= 7)) ||
(xDirectionDelta[dir] != 0 && (xPos <= 0 || xPos >= 7))) {
isValidMove = false;
break;
}
hash ^= PIECE_HASH(xPos, yPos, BLACK);
hash ^= PIECE_HASH(xPos, yPos, WHITE);
}
if (isValidMove) {
MOVE_HASH(x, y, dir, len) = hash;
}
else {
MOVE_HASH(x, y, dir, len) = BLANK;
}
}
}
}
}
}
/**
* Constructs a new board
*/
Board::Board(){
if(!isHashInit) {
initHash();
isHashInit = true;
}
pieces[WHITE] = 0x0000001008000000ULL;
pieces[BLACK] = 0x0000000810000000ULL;
isMovesCalc[WHITE] = false;
isMovesCalc[BLACK] = false;
parity = WHITE;
hash = defaultHash;
}
/**
* Deconstructs a board
*/
Board::~Board(){
}
/**
* Copies this board
* @return Pointer to copy of board
*/
Board* Board::copy(){
Board *newBoard = new Board();
newBoard->pieces[WHITE] = pieces[WHITE];
newBoard->pieces[BLACK] = pieces[BLACK];
for(int i = 0; i < 8; i++){
newBoard->moves[WHITE][i] = moves[WHITE][i];
newBoard->moves[BLACK][i] = moves[BLACK][i];
}
newBoard->allMoves[WHITE] = allMoves[WHITE];
newBoard->allMoves[BLACK] = allMoves[BLACK];
newBoard->isMovesCalc[WHITE] = isMovesCalc[WHITE];
newBoard->isMovesCalc[BLACK] = isMovesCalc[BLACK];
newBoard->parity = parity;
newBoard->hash = hash;
return newBoard;
}
/**
* Prints the current board state. Used for debugging
*/
void Board::printBoard(){
for(int y = 0; y < 8; y++){
for(int x = 0; x < 8; x++){
if(GET(pieces[WHITE], x, y)){
cerr << "W";
}
else if(GET(pieces[BLACK], x, y)){
cerr << "B";
}
else{
cerr << "-";
}
}
cerr << endl;
}
cerr << endl;
}
/**
* Shifts a grid of bits in the specified direction
* @param bits Grid of bits to shift
* @param dir Direction to shift bits
*/
unsigned long long Board::shiftBits(unsigned long long bits, Direction dir){
switch(dir){
case W:
return (bits << 1) & LEFT_MASK;
case NW:
return (bits << 9) & LEFT_MASK;
case N:
return (bits << 8);
case NE:
return (bits << 7) & RIGHT_MASK;
case E:
return (bits >> 1) & RIGHT_MASK;
case SE:
return (bits >> 9) & RIGHT_MASK;
case S:
return (bits >> 8);
case SW:
return (bits >> 7) & LEFT_MASK;
default:
return bits;
}
}
/**
* Calculates the possible moves for a given side and stores them in private
* variables for later use.
* @param side Side to calculate the possible moves for
*/
void Board::calcMoves(bool side){
unsigned long long empty = ~(pieces[side] | pieces[!side]);
for(int i = 0; i < 8; i++){
moves[side][i] = BLANK;
}
allMoves[side] = BLANK;
for(int i = 0; i < 8; i++){
unsigned long long candidates = pieces[!side] & shiftBits(pieces[side], directions[i]);
while(candidates != BLANK){
moves[side][(i + 4) % 8] |= empty & shiftBits(candidates, directions[i]);
allMoves[side] |= empty & shiftBits(candidates, directions[i]);
candidates = pieces[!side] & shiftBits(candidates, directions[i]);
}
}
isMovesCalc[side] = true;
}
/**
* Determines whether the specified side has any moves
* @param side Side to check for moves on
* @return True if there are >0 possible moves, false otherwise
*/
bool Board::hasMoves(bool side){
if(!isMovesCalc[side]){
calcMoves(side);
}
return (bool)allMoves[side];
}
/**
* Counts the number of moves available to the given side
* @param side Side to check for moves on
* @return Number of available moves
*/
int Board::countMoves(bool side){
if(!isMovesCalc[side]){
calcMoves(side);
}
return __builtin_popcountll(allMoves[side]);
}
/**
* Returns a vector of possible moves that a given side can make
* @param side Side to calculate moves for
*/
vector<Move> Board::possibleMoves(bool side){
if(!isMovesCalc[side]){
calcMoves(side);
}
vector<Move> ret = vector<Move>();
for(int y = 0; y < 8; y++){
for(int x = 0; x < 8; x++){
if(GET(allMoves[side], x, y)){
ret.push_back(Move(x, y, side));
}
}
}
if(ret.size() == 0) {
ret.push_back(NULL_MOVE(side));
}
return ret;
}
/**
* Checks if a certain move is valid for the specified side
* @param m Move to check
* @return True if move is valid, false otherwise
*/
bool Board::checkMove(Move m){
bool side = m.getSide();
if(!isMovesCalc[side]){
calcMoves(side);
}
return GET(allMoves[side], m.getX(), m.getY());
}
/**
* Does a given move on the board
* @param m Move to do on the board
*/
void Board::doMove(Move m){
if (m.isNull()) {
parity = !parity;
return;
}
if (!checkMove(m)) {return;}
bool side = m.getSide();
int x = m.getX();
int y = m.getY();
unsigned long long move = BLANK;
FLIP(move, x, y);
FLIP(pieces[side], x, y);
hash ^= PIECE_HASH(x, y, side);
for(int i = 0; i < 8; i++){
if(move & moves[side][i]){
int len = 0;
unsigned long long target = shiftBits(move, directions[i]);
while(!(target & pieces[side])){
len ++;
pieces[side] |= target;
pieces[!side] ^= target;
target = shiftBits(target, directions[i]);
}
hash ^= MOVE_HASH(x, y, i, len);
}
}
isMovesCalc[WHITE] = false;
isMovesCalc[BLACK] = false;
}
/**
* Counts the number of pieces on a given side
* @param side Side to check for pieces on
* @return Number of pieces for that side
*/
int Board::count(bool side){
return __builtin_popcountll(pieces[side]);
}
/**
* Determines if the game is finished (no moves can be played)
* @return True if no moves can be played, false otherwise
*/
bool Board::isDone(){
if ((pieces[BLACK] | pieces[WHITE]) == ~BLANK) {
return true;
}
return !(hasMoves(BLACK) || hasMoves(WHITE));
}
/**
* Overrides the board state to match given data. Used for testsuites
* @param data Data to change board state to
*/
void Board::setBoard(char data[]){
pieces[BLACK] = BLANK;
pieces[WHITE] = BLANK;
isMovesCalc[WHITE] = false;
isMovesCalc[BLACK] = false;
for(int i = 0; i < 64; i++){
if(data[i] == 'b'){
FLIP(pieces[BLACK], i % 8, i/8);
}
else if(data[i] == 'w'){
FLIP(pieces[WHITE], i % 8, i/8);
}
}
hash = BLANK;
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
if(GET(pieces[BLACK], x, y)) {
hash ^= PIECE_HASH(x, y, BLACK);
}
else if(GET(pieces[WHITE], x, y)) {
hash ^= PIECE_HASH(x, y, WHITE);
}
}
}
}
/**
* Finds the frontier size for a given side
* @param side Side to calculate frontier of
* @return Frontier of given side
*/
int Board::getFrontierSize(bool side){
unsigned long long frontier = BLANK;
unsigned long long empty = ~(pieces[side] | pieces[!side]);
for(int i = 0; i < 8; i++){
frontier |= (shiftBits(pieces[side], directions[i]) & empty);
}
return __builtin_popcountll(frontier);
}
/**
* Counts the number of stable pieces for a given side
* @param side Side to calculate stability for
* @return Number of stable pieces
*/
int Board::countStable(bool side){
unsigned long long fullStable = BLANK;
unsigned long long partialStable[4];
// Add partial stability to edges/corners
// NW/SE
partialStable[0] = pieces[side] & 0xff818181818181ffULL;
// N/S
partialStable[1] = pieces[side] & 0xff000000000000ffULL;
// NE/SW
partialStable[2] = pieces[side] & 0xff818181818181ffULL;
// E/W
partialStable[3] = pieces[side] & 0x8181818181818181ULL;
// Add partial stability to pieces in full rows/columns/diagonals
// Diagonals NW/SE
unsigned long long filter = 0x0000000000000080ULL;
for(int i = 0; i < 15; i++){
if(filter == (filter & (pieces[BLACK] | pieces[WHITE]))){
partialStable[0] |= (filter & pieces[side]);
}
filter = shiftBits(filter, N) | shiftBits(filter, E);
}
// Columns N/S
filter = 0x8080808080808080ULL;
for(int i = 0; i < 8; i++){
if(filter == (filter & (pieces[BLACK] | pieces[WHITE]))){
partialStable[1] |= (filter & pieces[side]);
}
filter = shiftBits(filter, E);
}
// Diagonals NE/SW
filter = 0x8000000000000000ULL;
for(int i = 0; i < 15; i++){
if(filter == (filter & (pieces[BLACK] | pieces[WHITE]))){
partialStable[2] |= (filter & pieces[side]);
}
filter = shiftBits(filter, S) | shiftBits(filter, E);
}
// Rows E/W
filter = 0xff00000000000000ULL;
for(int i = 0; i < 8; i++){
if(filter == (filter & (pieces[BLACK] | pieces[WHITE]))){
partialStable[3] |= (filter & pieces[side]);
}
filter = shiftBits(filter, S);
}
// Define full stable as intersection of partials
fullStable = partialStable[0] & partialStable[1] & partialStable[2] & partialStable[3];
// Loop until no changes are made to find all chained stable pieces
unsigned long long updateStable = fullStable;
do{
fullStable = updateStable;
for(int i = 0; i < 4; i++){
partialStable[i] |= pieces[side] & shiftBits(fullStable, directions[i]);
partialStable[i] |= pieces[side] & shiftBits(fullStable, directions[i+4]);
}
updateStable = partialStable[0] & partialStable[1] & partialStable[2] & partialStable[3];
} while(fullStable != updateStable);
return __builtin_popcountll(fullStable);
}
/**
* Prints the bits of a 64 bit int as a board. Used for debugging
* @param bits Set of bits to print
*/
void Board::printBits(unsigned long long bits){
for(int y = 0; y < 8; y++){
for(int x = 0; x < 8; x++){
cerr << GET(bits, x, y);
}
cerr << endl;
}
cerr << endl;
}
/**
* Finds the parity of a given board
* @return Side that the move parity gives advantage to
*/
bool Board::getParity() {
return parity;
}
unsigned long long Board::getHash() {
return hash;
}