-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathboard.cpp
More file actions
559 lines (496 loc) · 16.3 KB
/
Copy pathboard.cpp
File metadata and controls
559 lines (496 loc) · 16.3 KB
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#include <cstdio>
#include <cassert>
#include "board.hpp"
#define HEURISTIC_CUTOFF 0.99
__host__ __device__ int xyToIndex(int x, int y) { return 8 * y + x; }
__host__ __device__ int moveToIndex(Move m) { return xyToIndex(m.x, m.y); }
__host__ __device__ Move indexToMove(int i) { return Move(i % 8, i / 8); }
__host__ __device__ bitboard indexToBitboard(int i) { return 1ULL << i; }
__host__ __device__ bitboard xyToBitboard(int x, int y) { return indexToBitboard(xyToIndex(x,y)); }
__host__ __device__ bitboard moveToBitboard(Move m) { return indexToBitboard(moveToIndex(m)); }
/*
* Make a standard 8x8 Othello board and initialize it to the standard setup.
*/
__host__ __device__
Board::Board() {
occupied[WHITE] = 0ULL;
occupied[WHITE] |= xyToBitboard(3, 3);
occupied[WHITE] |= xyToBitboard(4, 4);
occupied[BLACK] = 0ULL;
occupied[BLACK] |= xyToBitboard(3, 4);
occupied[BLACK] |= xyToBitboard(4, 3);
}
/*
* Destructor for the board.
*/
__host__ __device__
Board::~Board() {
}
// Assume that the board is sparsely populated
__host__ __device__
int countSparse(bitboard b) {
int count = 0;
while (b) {
count++;
b &= b - 1; // reset LS1B
}
return count;
}
// Faster if board is highly populated
// http://chessprogramming.wikispaces.com/Population+Count
#define k1 0x5555555555555555ULL
#define k2 0x3333333333333333ULL
#define k4 0x0f0f0f0f0f0f0f0fULL
#define kf 0x0101010101010101ULL
__host__ __device__
int count(bitboard b) {
b = b - ((b >> 1) & k1); /* put count of each 2 bits into those 2 bits */
b = (b & k2) + ((b >> 2) & k2); /* put count of each 4 bits into those 4 bits */
b = (b + (b >> 4)) & k4 ; /* put count of each 8 bits into those 8 bits */
b = (b * kf) >> 56; /* returns 8 most significant bits of b + (b<<8) + (b<<16) + (b<<24) + ... */
return (int) b;
}
void print(bitboard b) {
fprintf(stderr, " 01234567\n");
for (int y = 0; y < 8; y++) {
fprintf(stderr, "%d ", y);
for (int x = 0; x < 8; x++) {
if (b & xyToBitboard(x, y)) {
fprintf(stderr, "*");
}
else {
fprintf(stderr, "-");
}
}
fprintf(stderr, "\n");
}
fprintf(stderr, "\n");
}
__host__ __device__
bitboard Board::getOccupied() {
return occupied[WHITE] | occupied[BLACK];
}
__host__ __device__
bitboard Board::getEmpty() {
return ~getOccupied();
}
__host__ __device__
bitboard Board::getMoves(Side side) {
return allAttack(occupied[side], occupied[!side]) & getEmpty();
}
__host__ __device__
int Board::getMovesAsArray(Move *output_moves, Side side) {
bitboard moves = getMoves(side);
if (!moves) return 0;
int numMoves = 0;
do {
int idx;
#ifdef __CUDA_ARCH__
idx = __ffsll(moves);
#else
idx = __builtin_ffsll(moves);
#endif
assert(numMoves < 32);
output_moves[numMoves] = indexToMove(idx-1);
numMoves++;
} while (moves &= moves-1); // reset LS1B
return numMoves;
}
__host__ __device__
int Board::getHeuristicMovesAsArray(Move *output_moves, Side side) {
bitboard moves = getMoves(side);
if (!moves) return 0;
float scores[MAX_NUM_MOVES];
float avg_score = 0;
int numMoves = 0;
int idx;
Board b;
do {
#ifdef __CUDA_ARCH__
idx = __ffsll(moves);
#else
idx = __builtin_ffsll(moves);
#endif
b = *this;
b.doMove(idx-1, side);
scores[numMoves] = b.getHeuristic(side);
avg_score += scores[numMoves];
output_moves[numMoves] = indexToMove(idx-1);
numMoves++;
} while (moves &= moves-1); // reset LS1B
avg_score /= numMoves;
assert(numMoves < MAX_NUM_MOVES);
// Remove moves with much worse than average heuristic score.
int numHeuristicMoves = 0;
idx = 0;
for(idx = 0; idx < numMoves; idx++) {
if ((1 + scores[idx]) > HEURISTIC_CUTOFF * (1 + avg_score)) {
output_moves[numHeuristicMoves] = output_moves[idx];
numHeuristicMoves++;
}
}
// Filter out moves
return numHeuristicMoves;
}
__host__ __device__
bool Board::get(Side side, int x, int y) {
return occupied[side] & xyToBitboard(x, y);
}
__host__ __device__
bool Board::isDone() {
return !(hasMoves(WHITE) || hasMoves(BLACK));
}
__host__ __device__
bool Board::hasMoves(Side side) {
return getMoves(side);
}
__host__ __device__
int Board::numMoves(Side side) {
return countSparse(getMoves(side));
}
__host__ __device__
bool Board::checkMove(Move m, Side side) {
return getMoves(side) & moveToBitboard(m);
}
__host__ __device__
bool Board::doMove(int m, Side side) {
// Return false if move is not valid
// if (!(getMoves(side) & indexToBitboard(m))) {
// return false;
// }
bitboard move = indexToBitboard(m);
bitboard flipped = allSandwiched(move, occupied[side], occupied[!side]);
occupied[side] |= move;
occupied[side] |= flipped;
occupied[!side] ^= flipped;
return true;
}
__host__ __device__
bool Board::doMove(Move m, Side side) {
// Return false if move is not valid
// if (!(getMoves(side) & moveToBitboard(m))) {
// return false;
// }
bitboard move = moveToBitboard(m);
bitboard flipped = allSandwiched(move, occupied[side], occupied[!side]);
occupied[side] |= move;
occupied[side] |= flipped;
occupied[!side] ^= flipped;
return true;
}
__host__ __device__
int Board::countPieces(Side side) {
return count(occupied[side]);
}
__host__ __device__
int Board::countPieces() {
return count(getOccupied());
}
__host__ __device__
int Board::countEmpty() {
return count(getEmpty());
}
// #define K_CORNERS 0.54
// #define K_X_SQUARES 0.15
// #define K_C_SQUARES 0.1
// #define K_PARITY 0.1
// #define K_MOBILITY 0.05
// #define K_FRONTIER 0.05
// #define K_PIECES 0.01
// #define K_CORNERS 0.39
// #define K_X_SQUARES 0.15
// #define K_C_SQUARES 0.1
// #define K_PARITY 0.15
// #define K_MOBILITY 0.1
// #define K_FRONTIER 0.1
// #define K_PIECES 0.01
#define K_CORNERS 100
#define K_X_SQUARES 30
#define K_C_SQUARES 30
#define K_PARITY 20
#define K_MOBILITY 15
#define K_FRONTIER 15
#define K_PIECES 1
// Heuristic functions
__host__ __device__
float Board::getHeuristic(Side side) {
return (K_CORNERS * getCornersHeuristic(side)
+ K_C_SQUARES * getCSquaresHeuristic(side)
+ K_X_SQUARES * getXSquaresHeuristic(side)
+ K_PARITY * getParityHeuristic(side)
+ K_MOBILITY * getMobilityHeuristic(side)
+ K_FRONTIER * getFrontierHeuristic(side)
+ K_PIECES * getPiecesHeuristic(side)
)
/ (K_CORNERS + K_C_SQUARES + K_X_SQUARES + K_PARITY + K_MOBILITY + K_FRONTIER + K_PIECES);
}
// Return whether or not this side gets to make the last move
__host__ __device__
float Board::getParityHeuristic(Side side) {
return (countPieces() % 2)? 1.0f:-1.0f;
}
__host__ __device__
float Board::getMobilityHeuristic(Side side) {
int my_moves = numMoves(side);
int opp_moves = numMoves(OTHER(side));
if (my_moves + opp_moves != 0)
return (float)(my_moves - opp_moves) / (my_moves + opp_moves);
else
return 0.f;
}
__host__ __device__
float Board::getFrontierHeuristic(Side side) {
bitboard empty = getEmpty();
int my_front = countSparse(allShift(occupied[side]) & empty);
int opp_front = countSparse(allShift(occupied[OTHER(side)]) & empty);
if (my_front + opp_front != 0)
return (float)(opp_front - my_front) / (my_front + opp_front);
else
return 0.f;
}
#define CORNERS_MASK 0x8100000000000081ULL
__host__ __device__
float Board::getCornersHeuristic(Side side) {
int my_corners = countSparse(occupied[side] & CORNERS_MASK);
int opp_corners = countSparse(occupied[OTHER(side)] & CORNERS_MASK);
return (my_corners - opp_corners) / 4.f;
}
__host__ __device__
float Board::getCSquaresHeuristic(Side side) {
bitboard good_c_mask = cardShift(~getEmpty() & CORNERS_MASK);
bitboard bad_c_mask = cardShift(getEmpty() & CORNERS_MASK);
int my_c = countSparse(occupied[side] & good_c_mask)
- countSparse(occupied[side] & bad_c_mask);
int opp_c = countSparse(occupied[OTHER(side)] & good_c_mask)
- countSparse(occupied[OTHER(side)] & bad_c_mask);
return (my_c - opp_c) / 8.f;
// bitboard c_mask = cardShift(getEmpty() & CORNERS_MASK);
// int my_c = countSparse(occupied[side] & c_mask);
// int opp_c = countSparse(occupied[OTHER(side)] & c_mask);
// int num_c = countSparse(c_mask);
// if (num_c != 0)
// return (float)(opp_c - my_c) / num_c;
// else
// return 0;
}
__host__ __device__
float Board::getXSquaresHeuristic(Side side) {
bitboard bad_x_mask = diagShift(getEmpty() & CORNERS_MASK);
bitboard good_x_mask = diagShift(~getEmpty() & CORNERS_MASK);
int my_x = countSparse(occupied[side] & good_x_mask)
- countSparse(occupied[side] & bad_x_mask);
int opp_x = countSparse(occupied[OTHER(side)] & good_x_mask)
- countSparse(occupied[OTHER(side)] & bad_x_mask);
return (my_x - opp_x) / 4.f;
// int my_x = countSparse(occupied[side] & x_mask);
// int opp_x = countSparse(occupied[OTHER(side)] & x_mask);
// int num_x = countSparse(x_mask);
// if (num_x != 0)
// return (float)(opp_x - my_x) / num_x;
// else
// return 0;
}
__host__ __device__
float Board::getPiecesHeuristic(Side side) {
int my_pieces = countPieces(side);
int opp_pieces = countPieces(OTHER(side));
if (my_pieces + opp_pieces != 0)
return (float)(my_pieces - opp_pieces) / (my_pieces + opp_pieces);
else
return 0.f;
}
__host__ __device__ bool Board::operator==(const Board &other) const {
return this->occupied[0] == other.occupied[0] &&
this->occupied[1] == other.occupied[1];
}
__host__ __device__ bool Board::operator!=(const Board &other) const {
return !(*this == other);
}
void Board::printBoard() {
fprintf(stderr, " 01234567\n");
for (int y = 0; y < 8; y++) {
fprintf(stderr, "%d ", y);
for (int x = 0; x < 8; x++) {
if (occupied[BLACK] & xyToBitboard(x, y)) {
fprintf(stderr, "B");
}
else if (occupied[WHITE] & xyToBitboard(x, y)) {
fprintf(stderr, "W");
}
else {
fprintf(stderr, "-");
}
}
fprintf(stderr, "\n");
}
fprintf(stderr, "\n");
}
// Bitboard algorithms
/**
* NW N NE
* -9 -8 -7
* \ | /
* W -1 <- 0 -> +1 E
* / | \
* +7 +8 +9
* S S SE
*/
// Bitmasks to prevent wrapping around the E and W directions
#define notE 0xFEFEFEFEFEFEFEFEULL
#define notW 0x7F7F7F7F7F7F7F7FULL
__host__ __device__
bitboard allSandwiched(bitboard gen1, bitboard gen2, bitboard prop) {
bitboard flood = SFill(gen1, prop) & NFill(gen2, prop);
flood |= NFill(gen1, prop) & SFill(gen2, prop);
flood |= EFill(gen1, prop) & WFill(gen2, prop);
flood |= SEFill(gen1, prop) & NWFill(gen2, prop);
flood |= NEFill(gen1, prop) & SWFill(gen2, prop);
flood |= WFill(gen1, prop) & EFill(gen2, prop);
flood |= SWFill(gen1, prop) & NEFill(gen2, prop);
flood |= NWFill(gen1, prop) & SEFill(gen2, prop);
return flood;
}
__host__ __device__
bitboard allAttack(bitboard gen, bitboard prop) {
bitboard flood = SShift( SFill(gen, prop));
flood |= NShift( NFill(gen, prop));
flood |= EShift( EFill(gen, prop));
flood |= SEShift(SEFill(gen, prop));
flood |= NEShift(NEFill(gen, prop));
flood |= WShift( WFill(gen, prop));
flood |= SWShift(SWFill(gen, prop));
flood |= NWShift(NWFill(gen, prop));
return flood;
}
__host__ __device__
bitboard allShift(bitboard gen) {
bitboard result = SShift(gen);
result |= NShift(gen);
result |= EShift(gen);
result |= SEShift(gen);
result |= NEShift(gen);
result |= WShift(gen);
result |= SWShift(gen);
result |= NWShift(gen);
return result;
}
__host__ __device__
bitboard diagShift(bitboard gen) {
bitboard result = SEShift(gen);
result |= NEShift(gen);
result |= SWShift(gen);
result |= NWShift(gen);
return result;
}
__host__ __device__
bitboard cardShift(bitboard gen) {
bitboard result = SShift(gen);
result |= NShift(gen);
result |= EShift(gen);
result |= WShift(gen);
return result;
}
// Dumb7Fill: http://chessprogramming.wikispaces.com/Dumb7Fill
__host__ __device__
bitboard SFill(bitboard gen, bitboard prop) {
bitboard flood = 0ULL;
flood |= gen = (gen << 8) & prop;
flood |= gen = (gen << 8) & prop;
flood |= gen = (gen << 8) & prop;
flood |= gen = (gen << 8) & prop;
flood |= gen = (gen << 8) & prop;
flood |= (gen << 8) & prop;
return flood;
}
__host__ __device__
bitboard NFill(bitboard gen, bitboard prop) {
bitboard flood = 0ULL;
flood |= gen = (gen >> 8) & prop;
flood |= gen = (gen >> 8) & prop;
flood |= gen = (gen >> 8) & prop;
flood |= gen = (gen >> 8) & prop;
flood |= gen = (gen >> 8) & prop;
flood |= (gen >> 8) & prop;
return flood;
}
__host__ __device__
bitboard EFill(bitboard gen, bitboard prop) {
bitboard flood = 0ULL;
prop &= notE;
flood |= gen = (gen << 1) & prop;
flood |= gen = (gen << 1) & prop;
flood |= gen = (gen << 1) & prop;
flood |= gen = (gen << 1) & prop;
flood |= gen = (gen << 1) & prop;
flood |= (gen << 1) & prop;
return flood & notE;
}
__host__ __device__
bitboard NEFill(bitboard gen, bitboard prop) {
bitboard flood = 0ULL;
prop &= notE;
flood |= gen = (gen >> 7) & prop;
flood |= gen = (gen >> 7) & prop;
flood |= gen = (gen >> 7) & prop;
flood |= gen = (gen >> 7) & prop;
flood |= gen = (gen >> 7) & prop;
flood |= (gen >> 7) & prop;
return flood & notE;
}
__host__ __device__
bitboard SEFill(bitboard gen, bitboard prop) {
bitboard flood = 0ULL;
prop &= notE;
flood |= gen = (gen << 9) & prop;
flood |= gen = (gen << 9) & prop;
flood |= gen = (gen << 9) & prop;
flood |= gen = (gen << 9) & prop;
flood |= gen = (gen << 9) & prop;
flood |= (gen << 9) & prop;
return flood & notE;
}
__host__ __device__
bitboard WFill(bitboard gen, bitboard prop) {
bitboard flood = 0ULL;
prop &= notW;
flood |= gen = (gen >> 1) & prop;
flood |= gen = (gen >> 1) & prop;
flood |= gen = (gen >> 1) & prop;
flood |= gen = (gen >> 1) & prop;
flood |= gen = (gen >> 1) & prop;
flood |= (gen >> 1) & prop;
return flood & notW;
}
__host__ __device__
bitboard SWFill(bitboard gen, bitboard prop) {
bitboard flood = 0ULL;
prop &= notW;
flood |= gen = (gen << 7) & prop;
flood |= gen = (gen << 7) & prop;
flood |= gen = (gen << 7) & prop;
flood |= gen = (gen << 7) & prop;
flood |= gen = (gen << 7) & prop;
flood |= (gen << 7) & prop;
return flood & notW;
}
__host__ __device__
bitboard NWFill(bitboard gen, bitboard prop) {
bitboard flood = 0ULL;
prop &= notW;
flood |= gen = (gen >> 9) & prop;
flood |= gen = (gen >> 9) & prop;
flood |= gen = (gen >> 9) & prop;
flood |= gen = (gen >> 9) & prop;
flood |= gen = (gen >> 9) & prop;
flood |= (gen >> 9) & prop;
return flood & notW;
}
// Shift algorithms
__host__ __device__ bitboard SShift (bitboard b) {return b << 8;}
__host__ __device__ bitboard NShift (bitboard b) {return b >> 8;}
__host__ __device__ bitboard EShift (bitboard b) {return (b << 1) & notE;}
__host__ __device__ bitboard SEShift (bitboard b) {return (b << 9) & notE;}
__host__ __device__ bitboard NEShift (bitboard b) {return (b >> 7) & notE;}
__host__ __device__ bitboard WShift (bitboard b) {return (b >> 1) & notW;}
__host__ __device__ bitboard SWShift (bitboard b) {return (b << 7) & notW;}
__host__ __device__ bitboard NWShift (bitboard b) {return (b >> 9) & notW;}