-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveGenerator.cpp
More file actions
474 lines (388 loc) · 12.6 KB
/
Copy pathMoveGenerator.cpp
File metadata and controls
474 lines (388 loc) · 12.6 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
#include "Bitboard.h"
#include "MoveGenerator.h"
#include "constants.h"
#include <vector>
#include <unordered_map>
#include <iostream>
using namespace std;
MoveGenerator::MoveGenerator(Bitboard * allPiecesIn) {
allPieces = allPiecesIn;
}
// if a given piece complies with the Hive sliding rule, return true
bool checkLegalWalk(Bitboard& allPieces, Bitboard& board, Direction dir) {
Bitboard CW(board), CCW(board);
CW.shiftDirection(rotateClockWise(dir));
CCW.shiftDirection(rotateCounterClockWise(dir));
return !(allPieces.containsAny(CW) && allPieces.containsAny(CCW));
}
// return a bitboard representing all directions a piece can slide to
// (does not comply with the one hive rule)
Bitboard getLegalWalks(Bitboard& board, Bitboard& allPieces) {
Bitboard retBoard;
for (unsigned i = 0; i < hexagonalDirections.size(); i++) {
if (checkLegalWalk(allPieces, board, (Direction)i))
{
Bitboard test(board);
test.shiftDirection((Direction)i);
retBoard.unionWith(test);
}
}
return retBoard;
}
Bitboard MoveGenerator::getMoves() {
piecesExceptCurrent.initializeTo(*allPieces);
moves.clear();
//remove generatingPiece
piecesExceptCurrent.xorWith(*generatingPieceBoard);
perimeter = piecesExceptCurrent.getPerimeter();
generateMoves();
return moves;
}
//I know I should have made separate classes or sum'n ={
void MoveGenerator::generateMoves() {
switch(*generatingPieceName){
case GRASSHOPPER:
generateGrasshopperMoves();
break;
case QUEEN:
generateQueenMoves();
break;
case LADYBUG:
generateLadybugMoves();
break;
case PILLBUG:
generatePillbugMoves();
break;
case MOSQUITO:
generateMosquitoMoves();
break;
case BEETLE:
generateBeetleMoves();
break;
case ANT:
if (approximate)
generateApproxAntMoves();
else
generateLegalAntMoves();
break;
case SPIDER:
generateSpiderMoves();
break;
default:
{
cout << "error 11";
cout << " if you have reached here may god help you";
throw 'a';
}
}
//the initial location does not count as a move
//so delete
moves.notIntersectionWith(*generatingPieceBoard);
}
//TODO:optimize it is so slow
void MoveGenerator::generateGrasshopperMoves(){
extraInfo.clear();
for (Direction dir: hexagonalDirections){
Bitboard nextPiece(*generatingPieceBoard);
nextPiece.shiftDirection(dir);
if (extraInfoOn) extraInfo.unionWith(nextPiece);
//check if there is a piece to jump over
nextPiece.intersectionWith(*allPieces);
if (nextPiece.count() == 0) {
//cannot move in this Direction
//if no piece to jump over
continue;
}
//jump over pieces until it is not possible
do {
nextPiece.shiftDirection(dir);
extraInfo.unionWith(nextPiece);
} while(allPieces -> containsAny(nextPiece));
moves.unionWith(nextPiece);
}
}
void MoveGenerator::generateQueenMoves(){
Bitboard frontier;
frontier.initializeTo(*generatingPieceBoard);
Bitboard neighbors = frontier.getPerimeter();
neighbors.intersectionWith(*allPieces);
neighbors = neighbors.getPerimeter();
frontier = getLegalWalkPerimeter(frontier);
//only keep nodes that are along the board perimeter
frontier.intersectionWith(perimeter);
//maintain contact with any of original neighbors
frontier.intersectionWith(neighbors);
moves.unionWith(frontier);
}
//TODO:optimize currently most expensive function
void MoveGenerator::generateLadybugMoves(){
Bitboard frontier, path, visited, result;
frontier.initializeTo(*generatingPieceBoard);
//store extra info for move
ladybugStep(frontier, result, path, 0);
result.intersectionWith(perimeter);
moves = result;
}
void MoveGenerator::ladybugStep(Bitboard& frontier,
Bitboard& result,
Bitboard& path,
int step) {
if (step == 3){
result.unionWith(frontier);
return;
}
Bitboard newFrontier;
for (auto dir: hexagonalDirections ){
newFrontier = getLegalClimb(frontier, dir);
newFrontier.notIntersectionWith(path);
if (extraInfoOn)
graph.assignNode(newFrontier, step);
if (step < 2)
newFrontier.intersectionWith(piecesExceptCurrent);
if (newFrontier.count() == 0)
continue;
path.unionWith(newFrontier);
ladybugStep(newFrontier, result, path, step + 1);
path.notIntersectionWith(newFrontier);
}
}
void MoveGenerator::generatePillbugMoves(){
//the Pillbug moves just as a Queen does
generateQueenMoves();
}
//any piece in the returned Bitboard can be moved to any empty square in returned bit board
//assumes that they are not pinned
Bitboard MoveGenerator::getPillbugSwapSpaces() {
Bitboard legalClimbs;
for (Direction dir : hexagonalDirections) {
Bitboard legalClimb = getLegalClimb(*generatingPieceBoard, dir);
legalClimb.notIntersectionWith(*upperLevelPieces);
legalClimbs.unionWith(legalClimb);
}
return legalClimbs;
}
void MoveGenerator::generateMosquitoMoves(){
//no such thing as MosquitoMoves as it takes the moves from its neighbors
//and is a bettle on top of the hive (nothing more)
//takes no moves from adjacent mosquitoes
}
void MoveGenerator::generateBeetleMoves(){
//TODO: Beetle move generation is lazy
Bitboard frontier, neighbors;
frontier.initializeTo(*generatingPieceBoard);
neighbors = frontier.getPerimeter();
neighbors.intersectionWith(piecesExceptCurrent);
//if the piece is adjacent, assume the beetle can climb it
moves.unionWith(neighbors);
//set neighbors to perimeter of neighbors
neighbors = neighbors.getPerimeter();
frontier = getLegalWalkPerimeter(frontier);
//maintain contact with at least one of the original neighbors
frontier.intersectionWith(neighbors);
Bitboard legalClimbs(frontier);
legalClimbs.intersectionWith(*upperLevelPieces);
if (legalClimbs.count() > 1) {
legalClimbs.clear();
for (auto dir: hexagonalDirections) {
Bitboard legalClimb = getLegalClimb(frontier, dir);
legalClimbs.unionWith(legalClimb);
}
frontier.intersectionWith(legalClimbs);
}
moves.unionWith(frontier);
}
//TODO: this is so slowwww
void MoveGenerator::generateLegalAntMoves() {
Bitboard frontiers;
Bitboard visited;
Bitboard newFrontiers;
newFrontiers = *generatingPieceBoard;
//perform a flood fill step until it cannot anymore
//TODO: avoid resplitting the board by using a getWalkPerimeter function that can
//handle multiple bits at once
while(newFrontiers.count()) {
frontiers.initializeTo(newFrontiers);
newFrontiers.clear();
for (Bitboard& frontier : frontiers.splitIntoBitboards()) {
//perform a flood fill step with only legal directions
frontier = getLegalWalkPerimeter(frontier);
frontier.intersectionWith(perimeter);
frontier.notIntersectionWith(visited);
// store new visited nodes
visited.unionWith(frontier);
// store next locations to search
newFrontiers.unionWith(frontier);
}
}
moves.unionWith(visited);
}
void MoveGenerator::generateApproxAntMoves() {
moves.unionWith(perimeter);
moves.notIntersectionWith(*generatingPieceBoard);
}
Bitboard MoveGenerator::getLegalConnectedComponents(Bitboard test) {
Bitboard components(test);
Bitboard frontiers;
if (test.count()) {
frontiers = test.getRandom();
}
Bitboard newFrontiers = frontiers;
while (newFrontiers.count() ){
frontiers.clear();
for (auto frontier : newFrontiers.splitIntoBitboards()) {
frontier = getLegalWalkPerimeter(frontier);
frontier.intersectionWith(test);
test.notIntersectionWith(frontier);
frontiers.unionWith(frontier);
}
newFrontiers = frontiers;
}
components.notIntersectionWith(test);
return components;
}
/*
TODO: make this work when you have time for optimization
void MoveGenerator::generateAntMoves(){
Bitboard inaccessibleNodes = getInaccessibleNodes(gatesSplit);
moves.unionWith(perimeter);
moves.notIntersectionWith(inaccessibleNodes);
moves.notIntersectionWith(*generatingPieceBoard);
}
*/
void MoveGenerator::generateSpiderMoves(){
Bitboard frontier, visited, neighbors;
list <pair<Bitboard, Bitboard>> frontierVisited;
frontier.initializeTo(*generatingPieceBoard);
frontierVisited.push_back({frontier, visited});
//TODO: optimize by storing the intermediate search in a 9 KB lookup table
for (int depth = 0; depth < NUM_SPIDER_MOVES ; depth++) {
list <pair<Bitboard, Bitboard>> next;
for (auto pairOfBoards : frontierVisited) {
frontier.initializeTo(pairOfBoards.first);
visited.initializeTo(pairOfBoards.second);
//get pieces that touch the frontier
neighbors = frontier.getPerimeter();
neighbors.intersectionWith(piecesExceptCurrent);
visited.unionWith(frontier);
frontier = getLegalWalkPerimeter(frontier);
frontier.intersectionWith(perimeter);
frontier.notIntersectionWith(visited);
// get the perimeter of original neighbors
neighbors = neighbors.getPerimeter();
// make sure to maintain contact with any of the original neighbors
frontier.intersectionWith(neighbors);
if (frontier.count()) {
for (auto& node: frontier.splitIntoBitboards()) {
//store the node and the path to the node
pair <Bitboard, Bitboard> pairOfBoards =
{node, visited};
next.push_back(pairOfBoards);
}
}
}
//get next set of frontiers and visited paths
frontierVisited = next;
}
for (auto pairOfBoards : frontierVisited)
moves.unionWith(pairOfBoards.first);
}
void MoveGenerator::setGeneratingName(PieceName * pieceNameIn) {
generatingPieceName = pieceNameIn;
}
void MoveGenerator::setGeneratingPieceBoard(Bitboard * b) {
generatingPieceBoard = b;
}
void MoveGenerator::setUpperLevelPieces(Bitboard * in) {
upperLevelPieces = in;
}
void MoveGenerator::setPieceStacks(unordered_map <int , deque <pair < PieceColor , PieceName >>> * in) {
pieceStacks = in;
}
Bitboard MoveGenerator::getLegalClimbs( Bitboard& board){
Bitboard testNeighbors;
for (auto dir : hexagonalDirections) {
Bitboard neighbor = getLegalClimb(board, dir);
testNeighbors.unionWith(neighbor);
}
return testNeighbors;
}
Bitboard MoveGenerator::getLegalClimb( Bitboard& board, Direction dir) {
Bitboard test(board), gate, CWgate(board), CCWgate(board);
Direction CW = rotateClockWise(dir);
CWgate.shiftDirection(CW);
gate.unionWith(CWgate);
Direction CCW = rotateCounterClockWise(dir);
CCWgate.shiftDirection(CCW);
gate.unionWith(CCWgate);
gate.intersectionWith(*upperLevelPieces);
test.shiftDirection(dir);
if (gate.count() == 2) {
int maxPieceHeight = 1;
if (upperLevelPieces -> containsAny(test)) {
maxPieceHeight = pieceStacks->at(test.hash()).size();
}
if (upperLevelPieces -> containsAny(board) ) {
int max = pieceStacks ->at(board.hash()).size();
maxPieceHeight = (max > maxPieceHeight) ? max : maxPieceHeight;
}
int minGateHeight = 100;
int gateCWHeight = pieceStacks ->at(CWgate.hash() ).size();
int gateCCWHeight = pieceStacks ->at(CCWgate.hash() ).size();
minGateHeight = (gateCWHeight < gateCCWHeight) ? gateCWHeight : gateCCWHeight;
if (minGateHeight <= maxPieceHeight) {
//store extra information for post-processing
if (extraInfoOn) extraInfo.unionWith(gate);
return test;
}
}
return test;
}
Bitboard MoveGenerator::getLegalWalkPerimeter(Bitboard board) {
if (board.count() != 1) {
cout << "CANNOT GET LEGAL WALK PERIMETER OF THIS BOARD" << endl;
cout << board.count() << endl;
throw 2;
}
pair<int, unsigned long long> LSB = board.getLeastSignificantBit();
Direction correction;
Bitboard testPerimeter(board);
testPerimeter = testPerimeter.getPerimeter();
testPerimeter.intersectionWith(piecesExceptCurrent);
if (LSB.second & 0x7e7e7e7e7e7e00u) {
//middle bits
testPerimeter.setBoard(LSB.first, GATES[__builtin_ctzll(LSB.second)]
[testPerimeter[LSB.first]]);
return testPerimeter;
} else if (LSB.second & 0x1010101010100u) {
// rightmost bits
correction = Direction::W;
LSB.second <<= 1;
} else if (LSB.second & 0x7e00000000000000u) {
// upper bits
correction = Direction::N;
LSB.second >>= 16;
} else if (LSB.second & 0x7e) {
// lower bits
correction = Direction::S;
LSB.second <<= 16;
} else if (LSB.second & 0x80808080808000u) {
// leftmost bits
correction = Direction::E;
LSB.second >>= 1;
} else {
//use the slow method only for diagonal corner bits
Bitboard legalWalks = getLegalWalks(board, *allPieces);
legalWalks.notIntersectionWith(testPerimeter);
return legalWalks;
}
//since only one board is stored, shift the perimeter to occupy one board
//get the stored value, then correct
testPerimeter.shiftDirection(oppositeDirection[correction]);
if (correction == N || correction == S)
testPerimeter.shiftDirection(oppositeDirection[correction]);
testPerimeter.setBoard(LSB.first, GATES[__builtin_ctzll(LSB.second)][testPerimeter[LSB.first]]);
testPerimeter.shiftDirection(correction);
if (correction == N || correction == S)
testPerimeter.shiftDirection(correction);
return testPerimeter;
}