-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblemNode.cpp
261 lines (200 loc) · 7.09 KB
/
ProblemNode.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
#include <list>
#include <iterator>
#include <unordered_map>
#include "Bitboard.h"
#include "ProblemNode.h"
int numDirections = 6;
int lowerLeftGate = (BITBOARD_ROWS - 1)*BITBOARD_COLS;
//TODO: CHECK THESE IF ANY ERROR IN BEETLE MOVE
unordered_map <Direction, unsigned long long> gateInDirection = {
{Direction::SE, 524800u},
{Direction::SW, 132096u},
{Direction::W, 33554944u},
{Direction::NW, 67239936u},
{Direction::NE, 34078720u},
{Direction::E, 67109888u}
};
//gates are structures that create problemNodes
//they are centered around 2,2 at board index lowerLeftGate
Bitboard gates[] = {
Bitboard({{lowerLeftGate, 134479872u}}),
Bitboard({{lowerLeftGate, 264192u}}),
Bitboard({{lowerLeftGate, 262148u}}),
Bitboard({{lowerLeftGate, 262400u}}),
Bitboard({{lowerLeftGate, 17039360u}}),
Bitboard({{lowerLeftGate, 17180131328u}})
};
//problemNodes are places in the board that do
//not have the freedom to move along all edges
//they are centered around 2,2 at board index lowerLeftGate
Bitboard potentialProblemNodes[] = {
Bitboard({{lowerLeftGate,67633152u}}),
Bitboard({{lowerLeftGate,525312u}}),
Bitboard({{lowerLeftGate,1536u}}),
Bitboard({{lowerLeftGate,131584u}}),
Bitboard({{lowerLeftGate,33685504u}}),
Bitboard({{lowerLeftGate,100663296u}})
};
using namespace std;
ProblemNodeContainer::ProblemNodeContainer(Bitboard * pieces) {
allPieces = pieces;
}
void ProblemNodeContainer::insert(Bitboard& problemNodes) {
int problemNodeHash = problemNodes.hash();
if (problemNodeExists(problemNodes)) return;
problemNodeHashes.insert(problemNodeHash);
for (auto map: problemNodes.split()){
for (auto piece: map.second){
int hashInt = hash(map.first, piece);
if (problemNodes.count() != 2) {
cout << "Attempting to insert an incorrectly sized problemNode" << endl;
throw 19;
}
//assigned the hash to a map for O(1) access
locationHashTable[hashInt].push_front(problemNodes);
}
}
}
bool ProblemNodeContainer::problemNodeExists(Bitboard& problemNode) {
return problemNodeHashes.find(problemNode.hash()) != problemNodeHashes.end();
}
void ProblemNodeContainer::clear() {
locationHashTable.clear();
problemNodeHashes.clear();
}
int ProblemNodeContainer::hash(int boardIndex, unsigned long long piece) {
return boardIndex + (__builtin_clzll(piece) << 8);
// 0x04d7651f <- might be faster to use this ;-)
}
void ProblemNodeContainer::remove(Bitboard & problemNodes) {
problemNodeHashes.erase(problemNodes.hash());
}
void ProblemNodeContainer::removePiece( Bitboard & piece) {
if (piece.count() != 1) {
cout << "Attempting to remove incorrectly sized piece" << endl;
throw 14;
}
//TODO: make this unneccessary
piece.pruneCache();
int pieceHash = piece.hash();
Bitboard testUpdate;
if (locationHashTable.find(pieceHash) != locationHashTable.end()) {
for (auto board: locationHashTable[pieceHash]){
testUpdate.unionWith(board);
}
testUpdate.unionWith(piece);
}
int boardIndex = piece.getRandomBoardIndex();
Bitboard pieceRemoved(*allPieces);
pieceRemoved.notIntersectionWith(piece);
Bitboard * temp = allPieces;
list <Bitboard> problemNodesCollection = getProblemNodesAtLocation(boardIndex,
piece[boardIndex]);
for (auto problemNodes: problemNodesCollection) {
//remove from problemNodeHashes
remove(problemNodes);
//add locations to update list over every problem node location
testUpdate.unionWith(problemNodes);
}
allPieces = &pieceRemoved;
//update to see if there are more problem nodes at those locations
updateVisible(testUpdate);
allPieces = temp;
}
//call this only once! very slow and inefficient
//TODO: programatically enforce above rule
void ProblemNodeContainer::findAllProblemNodes() {
Bitboard testUpdate;
for (auto map: allPieces -> split() ) {
for (unsigned long long board: map.second) {
for (auto problemNodes: getProblemNodesAtLocation(map.first, board)){
insert(problemNodes);
testUpdate.unionWith(problemNodes);
}
}
}
updateVisible(testUpdate);
}
//TODO: test and remove pruneCache
void ProblemNodeContainer::insertPiece(Bitboard & piece) {
piece.pruneCache();
int boardIndex = piece.getRandomBoardIndex();
unsigned long long board = piece[boardIndex];
Bitboard testUpdate;
for (auto problemNodes : getProblemNodesAtLocation(boardIndex, board)){
insert(problemNodes);
testUpdate.unionWith(problemNodes);
}
updateVisible(testUpdate);
}
void ProblemNodeContainer::updateVisible(Bitboard& locations) {
//finds out whether the location on the bit board should be
//set in problemNodes
visibleProblemNodes.unionWith(locations);
visibleProblemNodes.xorWith(locations);
for (auto& location: locations.splitIntoBitboards()){
int hashInt = location.hash();
auto problemNodes = locationHashTable[hashInt].begin();
while (problemNodes != locationHashTable[hashInt].end()) {
Bitboard testProblemNodes(*problemNodes);
testProblemNodes.notIntersectionWith(*allPieces);
if (testProblemNodes.count() == 2) {
if (problemNodeExists(testProblemNodes)) {
visibleProblemNodes.unionWith(testProblemNodes);
break;
} else {
locationHashTable[hashInt].erase(problemNodes++);
}
} else
problemNodes++;
}
if (locationHashTable.size() == 0) locationHashTable.erase(hashInt);
}
}
list <Bitboard>
ProblemNodeContainer::getProblemNodesAtLocation(int boardIndex, unsigned long long
piece){
list <Bitboard> retList;
int leadingZeroesCount = __builtin_clzll(piece);
int xShift = ((63) - leadingZeroesCount )% BITBOARD_WIDTH - 2;
int yShift = ((63) - leadingZeroesCount )/ BITBOARD_HEIGHT - 2;
xShift += (BITBOARD_WIDTH * (boardIndex % BITBOARD_COLS));
yShift += BITBOARD_HEIGHT * (BITBOARD_ROWS - 1 - (boardIndex / BITBOARD_ROWS));
Bitboard testGate;
Bitboard testProblemNodes;
for( int i = 0; i < numDirections; i++){
//create and shift into place
//TODO: make a 2d shift function
testGate.initializeTo(gates[i]);
testGate.shiftDirection(Direction::N, yShift);
testGate.shiftDirection(Direction::E, xShift);
testGate.convertToHexRepresentation(Direction::NE,yShift);
//see if all the bits in gateBitboard are set
testGate.intersectionWith(*allPieces);
if (testGate.count() == 2){
//create and shift into place
testProblemNodes.initializeTo(potentialProblemNodes[i]);
testProblemNodes.shiftDirection(Direction::N, yShift);
testProblemNodes.shiftDirection(Direction::E, xShift);
testProblemNodes.convertToHexRepresentation(Direction::NE,yShift);
//Add nodes to allProblemNodes (disregarding visibility);
if (testProblemNodes.count() == 2 ) retList.push_front(testProblemNodes);
}
}
return retList;
}
Bitboard ProblemNodeContainer::getPerimeter(Bitboard& pieces) {
Bitboard perimeter;
//first assume every direction is in perimeter
perimeter = pieces.getPerimeter();
for (auto& piece: pieces.splitIntoBitboards()) {
for (auto restrictedNodes: locationHashTable[piece.hash()]) {
//remove every restriction found
perimeter.notIntersectionWith(restrictedNodes);
}
}
return perimeter;
}
bool ProblemNodeContainer::contains(Bitboard& piece){
return (locationHashTable.find(piece.hash()) != locationHashTable.end());
}