| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
#include <iostream> |
|---|
| 24 |
#include <fstream> |
|---|
| 25 |
using namespace std; |
|---|
| 26 |
|
|---|
| 27 |
#include "algos.h" |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
string retrieve_datap0(PyObject* datapath) { |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
string retrieve_datap1(PyObject* datapath) { |
|---|
| 36 |
} |
|---|
| 37 |
|
|---|
| 38 |
Algorithm::Algorithm(int bsize) { |
|---|
| 39 |
boardsize = bsize; |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
Algo_finalpos::Algo_finalpos(int bsize) : Algorithm(bsize) { |
|---|
| 44 |
finalpos = 0; |
|---|
| 45 |
finalposIndex = -1; |
|---|
| 46 |
finalposSize = 0; |
|---|
| 47 |
fp = 0; |
|---|
| 48 |
fpIndex = -1; |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
Algo_finalpos::~Algo_finalpos() { |
|---|
| 52 |
delete [] finalpos; |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
void Algo_finalpos::initialize_process(int l) { |
|---|
| 57 |
finalposSize = l*100; |
|---|
| 58 |
finalpos = new char[finalposSize]; |
|---|
| 59 |
finalposIndex = 0; |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
void Algo_finalpos::newgame_process() { |
|---|
| 63 |
fp = new char[100]; |
|---|
| 64 |
for(int i=0; i<100; i++) fp[i] = 255; |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
void Algo_finalpos::AB_process(int x, int y) { |
|---|
| 68 |
fp[y/2 + 10*(x/2)] &= ~(1 << (2*(x%2 + 2*(y%2)))); |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
void Algo_finalpos::AW_process(int x, int y) { |
|---|
| 72 |
fp[y/2 + 10*(x/2)] &= ~(1 << (2*(x%2 + 2*(y%2))+1)); |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
void Algo_finalpos::AE_process(int x, int y, char removed) { |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
void Algo_finalpos::endOfNode_process() { |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
void Algo_finalpos::B_process(int x, int y, PyObject* cap) { |
|---|
| 82 |
fp[y/2 + 10*(x/2)] &= ~(1 << (2*(x%2 + 2*(y%2)))); |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
void Algo_finalpos::W_process(int x, int y, cap) { |
|---|
| 86 |
fp[y/2 + 10*(x/2)] &= ~(1 << (2*(x%2 + 2*(y%2))+1)); |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
void Algo_finalpos::branchpoint_process() { |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
void Algo_finalpos::endOfVariation_process() { |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
void Algo_finalpos::endgame_process() { |
|---|
| 96 |
if (finalposIndex > finalposSize-100) printf("OOPS\n"); |
|---|
| 97 |
for(int i=0; i<100; i++) finalpos[finalposIndex++] = fp[i]; |
|---|
| 98 |
delete [] fp; |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
void Algo_finalpos::finalize_process(PyObject* datap) { |
|---|
| 102 |
String fn = retrieve_datap0(datap) + "/finalpos" + retrieve_datap1(datap) + ".db"; |
|---|
| 103 |
ofstream file(fn, ios::out|ios::trunc|ios::binary) |
|---|
| 104 |
file.write(finalpos, finalposSize); |
|---|
| 105 |
file.close(); |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
int Algo_finalpos::duplicateCheck(sli, datapath) { |
|---|
| 110 |
if (!strcmp(datapath, "")) { |
|---|
| 111 |
for(int i=0; i<100; i++) |
|---|
| 112 |
if (finalpos[sli*100+i] != fp[i]) |
|---|
| 113 |
return 0; |
|---|
| 114 |
return 1; |
|---|
| 115 |
} |
|---|
| 116 |
return 0; |
|---|
| 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 |
int Algo_finalpos::retrieve_db(PyObject* datap) { |
|---|
| 143 |
String fn = retrieve_datap0(datap) + "/finalpos" + retrieve_datap1(datap) + ".db"; |
|---|
| 144 |
ifstream file (fn, ios::in|ios::binary|ios::ate); |
|---|
| 145 |
if (file.is_open()) { |
|---|
| 146 |
ifstream::pos_type size = file.tellg(); |
|---|
| 147 |
delete [] finalpos; |
|---|
| 148 |
finalpos = new char[size]; |
|---|
| 149 |
file.seekg (0, ios::beg); |
|---|
| 150 |
file.read (memblock, size); |
|---|
| 151 |
file.close(); |
|---|
| 152 |
return 1; |
|---|
| 153 |
} |
|---|
| 154 |
else cout << "Unable to open file"; |
|---|
| 155 |
return 0; |
|---|
| 156 |
} |
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
Algo_finalpos::search(patternList, options, db, progBar, progStart, progEnd) { |
|---|
| 160 |
|
|---|
| 161 |
int ctr = 0; |
|---|
| 162 |
|
|---|
| 163 |
possiblePatterns = range(patternList.size()); |
|---|
| 164 |
index = db.start(); |
|---|
| 165 |
int counter = 0; |
|---|
| 166 |
if (!self.retrieve_db(db.datapath)) { |
|---|
| 167 |
printf("error\n"); |
|---|
| 168 |
return; |
|---|
| 169 |
} |
|---|
| 170 |
|
|---|
| 171 |
while (index != -1) { |
|---|
| 172 |
counter++; |
|---|
| 173 |
if (progBar && !(counter % 10)) |
|---|
| 174 |
progBar.redraw((progEnd-progStart)*counter/len(db.current) + progStart); |
|---|
| 175 |
|
|---|
| 176 |
matchList = []; |
|---|
| 177 |
|
|---|
| 178 |
for(N in possiblePatterns) { |
|---|
| 179 |
|
|---|
| 180 |
Pattern pattern = patternList.get(N); |
|---|
| 181 |
for(int a0=pattern.left; a0 <= pattern.right; a0++) { |
|---|
| 182 |
for(int a1 = pattern.top; a1 <= pattern.bottom; a1++) { |
|---|
| 183 |
int matches = 1; |
|---|
| 184 |
|
|---|
| 185 |
int pIndex = 2*(a1%2) + (a0%2); |
|---|
| 186 |
char* patternBits = pattern.bits[pIndex]; |
|---|
| 187 |
int pbIndex = 0; |
|---|
| 188 |
int fpIndex = index*100 + a1/2 + (a0/2)*10; |
|---|
| 189 |
|
|---|
| 190 |
for(int x=0; x<patternBits[0]; x++) { |
|---|
| 191 |
char start = patternBits[pbIndex+1]; |
|---|
| 192 |
char length = patternBits[pbIndex+2]; |
|---|
| 193 |
fpIndex += start; |
|---|
| 194 |
pbIndex += 2; |
|---|
| 195 |
for(int y=0; y<length; y++) { |
|---|
| 196 |
pbIndex++; |
|---|
| 197 |
if (patternBits[pbIndex] & finalpos[fpIndex]) { |
|---|
| 198 |
matches = 0; |
|---|
| 199 |
break; |
|---|
| 200 |
} |
|---|
| 201 |
fpIndex++; |
|---|
| 202 |
} |
|---|
| 203 |
if (!matches) break; |
|---|
| 204 |
fpIndex += 10 - start - length; |
|---|
| 205 |
} |
|---|
| 206 |
if (matches) matchList.append((N,(a0,a1))); |
|---|
| 207 |
} |
|---|
| 208 |
} |
|---|
| 209 |
} |
|---|
| 210 |
|
|---|
| 211 |
if (matchList) db.makeCurrentCandidate(matchList); |
|---|
| 212 |
else db.discardCurrent(); |
|---|
| 213 |
|
|---|
| 214 |
index = db.next(); |
|---|
| 215 |
} |
|---|
| 216 |
} |
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
Algo_movelist::Algo_movelist(int bsize) : Algorithm(bsize) { |
|---|
| 224 |
} |
|---|
| 225 |
|
|---|
| 226 |
Algo_movelist::~Algo_movelist() { |
|---|
| 227 |
delete [] finalposC; |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
void Algo_movelist:: initialize_process(int l) { |
|---|
| 231 |
|
|---|
| 232 |
mainlistArr = vector<char>(); |
|---|
| 233 |
posTable = vector<long>(); |
|---|
| 234 |
|
|---|
| 235 |
finalposCSize = l*50; |
|---|
| 236 |
finalposC = new char[finalposCSize]; |
|---|
| 237 |
finalposCIndex = 0; |
|---|
| 238 |
} |
|---|
| 239 |
|
|---|
| 240 |
void Algo_movelist::newgame_process() { |
|---|
| 241 |
|
|---|
| 242 |
movelist = vector<char>; |
|---|
| 243 |
|
|---|
| 244 |
fpCSize = 50; |
|---|
| 245 |
fpC = new char[fpCSize]; |
|---|
| 246 |
fpCIndex = 0; |
|---|
| 247 |
for(int i=0; i<fpCSize; i++) fpC[i] = 0; |
|---|
| 248 |
} |
|---|
| 249 |
|
|---|
| 250 |
void Algo_movelist::AB_process(int x, int y) { |
|---|
| 251 |
|
|---|
| 252 |
movelist.push_back((char)x); |
|---|
| 253 |
movelist.push_back((char)(y | BLACK)); |
|---|
| 254 |
} |
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
void Algo_movelist::AW_process(int x, int y) { |
|---|
| 258 |
movelist.push_back((char)x); |
|---|
| 259 |
movelist.push_back((char)(y | WHITE)); |
|---|
| 260 |
} |
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
void Algo_movelist::AE_process(int x, int y, char removed) { |
|---|
| 264 |
movelist.push_back((char)x); |
|---|
| 265 |
if (removed == 'B') movelist.push_back((char)(y | REMOVE | BLACK)); |
|---|
| 266 |
else if (removed == 'W') movelist.push_back((char)(y | REMOVE | WHITE)); |
|---|
| 267 |
else printf("oops\n"); |
|---|
| 268 |
} |
|---|
| 269 |
|
|---|
| 270 |
void Algo_movelist::endOfNode_process() { |
|---|
| 271 |
if (movelist.size()>1) { |
|---|
| 272 |
if (movelist[movelist.size()-2] & ENDOFNODE) { |
|---|
| 273 |
movelist.push_back(ENDOFNODE); |
|---|
| 274 |
movelist.push_back(0); |
|---|
| 275 |
} |
|---|
| 276 |
else movelist[movelist.size()-2] |= ENDOFNODE; |
|---|
| 277 |
} |
|---|
| 278 |
} |
|---|
| 279 |
|
|---|
| 280 |
void Algo_movelist::B_process(int x, int y, cap) { |
|---|
| 281 |
if (!movelist.size()) { |
|---|
| 282 |
movelist.push_back(ENDOFNODE); |
|---|
| 283 |
self.movelist.push_back(0); |
|---|
| 284 |
} |
|---|
| 285 |
movelist.push_back(x); |
|---|
| 286 |
movelist.push_back(y | BLACK); |
|---|
| 287 |
|
|---|
| 288 |
for(x, y in cap) { |
|---|
| 289 |
movelist.push_back(x); |
|---|
| 290 |
movelist.push_back(y | REMOVE | WHITE); |
|---|
| 291 |
self.fpC[y/4 + 5*(x/2)] |= 1 << (x%2 + 2*(y%4)); |
|---|
| 292 |
} |
|---|
| 293 |
} |
|---|
| 294 |
|
|---|
| 295 |
void Algo_movelist::W_process(int x, int y, cap) { |
|---|
| 296 |
if (!movelist.size()) { |
|---|
| 297 |
movelist.push_back(ENDOFNODE); |
|---|
| 298 |
movelist.push_back(0); |
|---|
| 299 |
} |
|---|
| 300 |
movelist.push_back(x); |
|---|
| 301 |
movelist.push_back(y | WHITE); |
|---|
| 302 |
|
|---|
| 303 |
for(x, y in cap) { |
|---|
| 304 |
movelist.push_back(x); |
|---|
| 305 |
movelist.push_back(y | REMOVE | BLACK); |
|---|
| 306 |
self.fpC[y/4 + 5*(x/2)] |= 1 << (x%2 + 2*(y%4)); |
|---|
| 307 |
} |
|---|
| 308 |
} |
|---|
| 309 |
|
|---|
| 310 |
void Algo_movelist::branchpoint_process() { |
|---|
| 311 |
movelist.push_back(BRANCHPOINT); |
|---|
| 312 |
movelist.push_back(0); |
|---|
| 313 |
} |
|---|
| 314 |
|
|---|
| 315 |
void Algo_movelist::endOfVariation_process() { |
|---|
| 316 |
movelist.push_back(ENDOFVARIATION); |
|---|
| 317 |
movelist.push_back(0); |
|---|
| 318 |
} |
|---|
| 319 |
|
|---|
| 320 |
void Algo_movelist::endgame_process() { |
|---|
| 321 |
posTable.append(len(self.mainlistArr)); |
|---|
| 322 |
mainlistArr.push_back(255); |
|---|
| 323 |
mainlistArr.push_back(255); |
|---|
| 324 |
for(int i=0; i<movelist.size(); i++) |
|---|
| 325 |
mainlistArr.push_back(movelist[i]); |
|---|
| 326 |
|
|---|
| 327 |
for(int i=0; i<fpCSize; i++) |
|---|
| 328 |
finalposC[finalposCIndex++] = fpC[i]; |
|---|
| 329 |
|
|---|
| 330 |
delete [] fpC; |
|---|
| 331 |
fpCIndex = -1; |
|---|
| 332 |
fpCSize = 0; |
|---|
| 333 |
} |
|---|
| 334 |
|
|---|
| 335 |
void Algo_movelist::finalize_process(datap) { |
|---|
| 336 |
String fn = retrieve_datap0(datap) + "/posTable" + retrieve_datap1(datap) + ".db"; |
|---|
| 337 |
ofstream file(fn, ios::out|ios::trunc|ios::binary) |
|---|
| 338 |
file.write(posTable, posTableSize); |
|---|
| 339 |
file.close(); |
|---|
| 340 |
|
|---|
| 341 |
fn = datap0 + "/lists" + datap1 + ".db"; |
|---|
| 342 |
file(fn, ios::out|ios::trunc|ios::binary) |
|---|
| 343 |
file.write(mainlistArr, mainlistArrSize); |
|---|
| 344 |
file.close(); |
|---|
| 345 |
|
|---|
| 346 |
fn = datap0 + "/finalposC" + datap1 + ".db"; |
|---|
| 347 |
file(fn, ios::out|ios::trunc|ios::binary) |
|---|
| 348 |
file.write(finalposC, finalposCSize); |
|---|
| 349 |
file.close(); |
|---|
| 350 |
} |
|---|
| 351 |
|
|---|
| 352 |
void Algo_movelist::retrieve_db(datap) { |
|---|
| 353 |
|
|---|
| 354 |
String fn = retrieve_datap0(datap) + "/finalposC" + retrieve_datap1(datap) + ".db"; |
|---|
| 355 |
ifstream file (fn, ios::in|ios::binary|ios::ate); |
|---|
| 356 |
if (file.is_open()) { |
|---|
| 357 |
ifstream::pos_type size = file.tellg(); |
|---|
| 358 |
delete [] finalposC; |
|---|
| 359 |
finalposC = new char[size]; |
|---|
| 360 |
file.seekg (0, ios::beg); |
|---|
| 361 |
file.read (memblock, size); |
|---|
| 362 |
file.close(); |
|---|
| 363 |
} |
|---|
| 364 |
else { |
|---|
| 365 |
cout << "Unable to open file"; |
|---|
| 366 |
return 0; |
|---|
| 367 |
} |
|---|
| 368 |
fn = retrieve_datap0(datap) + "/lists" + retrieve_datap1(datap) + ".db"; |
|---|
| 369 |
ifstream file (fn, ios::in|ios::binary|ios::ate); |
|---|
| 370 |
if (file.is_open()) { |
|---|
| 371 |
ifstream::pos_type size = file.tellg(); |
|---|
| 372 |
delete [] mainlistArr; |
|---|
| 373 |
mainlistArr = new char[size]; |
|---|
| 374 |
file.seekg (0, ios::beg); |
|---|
| 375 |
file.read (memblock, size); |
|---|
| 376 |
file.close(); |
|---|
| 377 |
} |
|---|
| 378 |
else { |
|---|
| 379 |
cout << "Unable to open file"; |
|---|
| 380 |
return 0; |
|---|
| 381 |
} |
|---|
| 382 |
fn = retrieve_datap0(datap) + "/posTable" + retrieve_datap1(datap) + ".db"; |
|---|
| 383 |
ifstream file (fn, ios::in|ios::binary|ios::ate); |
|---|
| 384 |
if (file.is_open()) { |
|---|
| 385 |
ifstream::pos_type size = file.tellg(); |
|---|
| 386 |
delete [] posTable; |
|---|
| 387 |
posTable = new char[size]; |
|---|
| 388 |
file.seekg (0, ios::beg); |
|---|
| 389 |
file.read (memblock, size); |
|---|
| 390 |
file.close(); |
|---|
| 391 |
return 1; |
|---|
| 392 |
} |
|---|
| 393 |
else { |
|---|
| 394 |
cout << "Unable to open file"; |
|---|
| 395 |
return 0; |
|---|
| 396 |
} |
|---|
| 397 |
return 0; |
|---|
| 398 |
} |
|---|
| 399 |
|
|---|
| 400 |
|
|---|
| 401 |
|
|---|
| 402 |
void Algo_movelist::search(patternList, options, db, |
|---|
| 403 |
continuations, contLabelsIndex, contLabels, |
|---|
| 404 |
progBar, progStart, progEnd) { |
|---|
| 405 |
|
|---|
| 406 |
int numOfHits = 0; |
|---|
| 407 |
int self_numOfSwitched = 0; |
|---|
| 408 |
int Bwins = 0; |
|---|
| 409 |
int Wwins = 0; |
|---|
| 410 |
|
|---|
| 411 |
int movelimit = 1000; |
|---|
| 412 |
int showColorSwap = 1; |
|---|
| 413 |
|
|---|
| 414 |
if (options.has_key('movelimit')) movelimit = options['movelimit']; |
|---|
| 415 |
if (options.has_key('showcolorswap')) showColorSwap = options['showcolorswap']; |
|---|
| 416 |
|
|---|
| 417 |
if (!retrieve_db(db.datapath)) { |
|---|
| 418 |
printf("error\n"); |
|---|
| 419 |
return; |
|---|
| 420 |
} |
|---|
| 421 |
|
|---|
| 422 |
int index = db.start(); |
|---|
| 423 |
int gameCounter = 0; |
|---|
| 424 |
|
|---|
| 425 |
while (index != -1) { |
|---|
| 426 |
|
|---|
| 427 |
gameCounter++; |
|---|
| 428 |
if (progBar && !(gameCounter % 10)) |
|---|
| 429 |
progBar.redraw((progEnd-progStart)*gameCounter/len(db.current) + progStart); |
|---|
| 430 |
|
|---|
| 431 |
result = []; |
|---|
| 432 |
numOfSwitched = 0; |
|---|
| 433 |
branchpoints = []; |
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
int movelistIndex = posTable[index] + 2; |
|---|
| 437 |
|
|---|
| 438 |
int endMovelist = movelistIndex; |
|---|
| 439 |
while (endMovelist < mainlistArr.size() && mainlistArr[endMovelist] != 255) |
|---|
| 440 |
endMovelist += 2; |
|---|
| 441 |
|
|---|
| 442 |
dicts = {}; |
|---|
| 443 |
contList = {}; |
|---|
| 444 |
contListIndex = {}; |
|---|
| 445 |
dictsNO = {}; |
|---|
| 446 |
Xinterv = {}; |
|---|
| 447 |
Yinterv = {}; |
|---|
| 448 |
|
|---|
| 449 |
for (m in db.getCurrentMatchlist()) { |
|---|
| 450 |
d = {}; |
|---|
| 451 |
dNO = 0; |
|---|
| 452 |
Pattern p = patternList.get(m[0]); |
|---|
| 453 |
for(int i=0; i<p.sizeX; i++) { |
|---|
| 454 |
for(int j=0; j<p.sizeY; j++) { |
|---|
| 455 |
char p_ij = p.getInitial(i,j); |
|---|
| 456 |
if (p_ij != '.') d[(i+m[1][0],j+m[1][1])] = p_ij; |
|---|
| 457 |
if (p_ij == 'X' or p_ij == 'O') dNO++; |
|---|
| 458 |
} |
|---|
| 459 |
} |
|---|
| 460 |
|
|---|
| 461 |
dicts[m] = d; |
|---|
| 462 |
dictsNO[m] = dNO; |
|---|
| 463 |
|
|---|
| 464 |
contList[m] = []; |
|---|
| 465 |
for (x, y, color in p.contList) |
|---|
| 466 |
contList[m].append((x+m[1][0], y+m[1][1], color)); |
|---|
| 467 |
|
|---|
| 468 |
contListIndex[m] = 0; |
|---|
| 469 |
Xinterv[m] = (m[1][0], m[1][0] + p.sizeX); |
|---|
| 470 |
Yinterv[m] = (m[1][1], m[1][1] + p.sizeY); |
|---|
| 471 |
} |
|---|
| 472 |
|
|---|
| 473 |
int counter = 0; |
|---|
| 474 |
|
|---|
| 475 |
while (movelistIndex < endMovelist && counter <= self.movelimit) { |
|---|
| 476 |
|
|---|
| 477 |
if (mainlistArr[movelistIndex] & BRANCHPOINT) { |
|---|
| 478 |
self.branchpoints.append((deepcopy(dicts), deepcopy(dictsNO), |
|---|
| 479 |
deepcopy(contList), deepcopy(contListIndex), counter)); |
|---|
| 480 |
movelistIndex += 2; |
|---|
| 481 |
continue; |
|---|
| 482 |
} |
|---|
| 483 |
else if (mainlistArr[movelistIndex] & ENDOFVARIATION) { |
|---|
| 484 |
if (!patternList.nextMove) { |
|---|
| 485 |
for(m in dicts.keys()) |
|---|
| 486 |
if (dicts[m].has_key('found')) { |
|---|
| 487 |
Pattern p = patternList.get(m[0]); |
|---|
| 488 |
if (p.colorSwitch) numOfSwitched++; |
|---|
| 489 |
if (p.colorSwitch && showColorSwap) |
|---|
| 490 |
result.append((dicts[m]['found'], '-')); |
|---|
| 491 |
else result.append((dicts[m]['found'], '')); |
|---|
| 492 |
} |
|---|
| 493 |
} |
|---|
| 494 |
dicts, dictsNO, contList, contListIndex, counter = self.branchpoints.pop(); |
|---|
| 495 |
movelistIndex += 2; |
|---|
| 496 |
continue; |
|---|
| 497 |
} |
|---|
| 498 |
|
|---|
| 499 |
int endOfNode = mainlistArr[movelistIndex] & ENDOFNODE; |
|---|
| 500 |
|
|---|
| 501 |
int x = mainlistArr[movelistIndex] & 31; |
|---|
| 502 |
int y = mainlistArr[movelistIndex+1] & 31; |
|---|
| 503 |
|
|---|
| 504 |
char co; |
|---|
| 505 |
char invco; |
|---|
| 506 |
|
|---|
| 507 |
if (!(mainlistArr[movelistIndex+1] & REMOVE) && (mainlistArr[movelistIndex+1] & (BLACK | WHITE))) { |
|---|
| 508 |
if (mainlistArr[movelistIndex+1] & BLACK) { |
|---|
| 509 |
co = 'X'; |
|---|
| 510 |
invco = 'O'; |
|---|
| 511 |
} |
|---|
| 512 |
else { |
|---|
| 513 |
co = 'O'; |
|---|
| 514 |
invco = 'X'; |
|---|
| 515 |
} |
|---|
| 516 |
|
|---|
| 517 |
for(m in dicts.keys()) { |
|---|
| 518 |
if (Xinterv[m][0] <= x && x < Xinterv[m][1] && Yinterv[m][0] <= y && y < Yinterv[m][1]) { |
|---|
| 519 |
if (dicts[m].has_key('found')) { |
|---|
| 520 |
hit, label, contLabelsIndex, switched = patternList.updateContinuations(m[0], x, y, invco, |
|---|
| 521 |
Xinterv[m], Yinterv[m], |
|---|
| 522 |
dicts[m]['found'], counter, |
|---|
| 523 |
continuations, contLabels, |
|---|
| 524 |
contLabelsIndex, |
|---|
| 525 |
db.getCurrentWinner()); |
|---|
| 526 |
if (!hit) { |
|---|
| 527 |
del dicts[m]; |
|---|
| 528 |
del dictsNO[m]; |
|---|
| 529 |
continue; |
|---|
| 530 |
} |
|---|
| 531 |
|
|---|
| 532 |
numOfSwitched += switched; |
|---|
| 533 |
|
|---|
| 534 |
if (switched && showColorSwap) |
|---|
| 535 |
|
|---|
| 536 |
result.append((dicts[m]['found'], label + '-')); |
|---|
| 537 |
else result.append((dicts[m]['found'], label)); |
|---|
| 538 |
del dicts[m]; |
|---|
| 539 |
del dictsNO[m]; |
|---|
| 540 |
continue; |
|---|
| 541 |
} |
|---|
| 542 |
else if (dicts[m].has_key('foundInitial')) { |
|---|
| 543 |
if ((x, y, co) == contList[m][contListIndex[m]]) { |
|---|
| 544 |
contListIndex[m]++; |
|---|
| 545 |
if (contListIndex[m] == len(contList[m])) { |
|---|
| 546 |
dicts[m]['found'] = counter; |
|---|
| 547 |
del dicts[m]['foundInitial']; |
|---|
| 548 |
} |
|---|
| 549 |
} |
|---|
| 550 |
else { |
|---|
| 551 |
if (dicts[m].has_key('dontRestore')) { |
|---|
| 552 |
del dicts[m]; |
|---|
| 553 |
continue; |
|---|
| 554 |
} |
|---|
| 555 |
else { |
|---|
| 556 |
contListIndex[m] = 0; |
|---|
| 557 |
del dicts[m]['foundInitial']; |
|---|
| 558 |
} |
|---|
| 559 |
} |
|---|
| 560 |
} |
|---|
| 561 |
|
|---|
| 562 |
if (!dicts[m].has_key((x,y))) { |
|---|
| 563 |
if (!(self.finalposC[index*50 + y/4 + 5*(x/2)] & (1 << (x%2 + 2*(y%4))))) { |
|---|
| 564 |
|
|---|
| 565 |
if (!contListIndex[m]) { |
|---|
| 566 |
del dicts[m]; |
|---|
| 567 |
continue; |
|---|
| 568 |
} |
|---|
| 569 |
else dicts[m]['dontRestore'] = 1; |
|---|
| 570 |
} |
|---|
| 571 |
else { |
|---|
| 572 |
dicts[m][(x,y)] = '.'; |
|---|
| 573 |
dictsNO[m]++; |
|---|
| 574 |
} |
|---|
| 575 |
} |
|---|
| 576 |
else if (dicts[m][(x,y)] == lower(invco)) { |
|---|
| 577 |
if (!(self.finalposC[index*50 + y/4 + 5*(x/2)] & (1 << (x%2 + 2*(y%4))))) { |
|---|
| 578 |
if (!contListIndex[m]) { |
|---|
| 579 |
del dicts[m]; |
|---|
| 580 |
continue; |
|---|
| 581 |
} |
|---|
| 582 |
else dicts[m]['dontRestore'] = 1; |
|---|
| 583 |
} |
|---|
| 584 |
else dictsNO[m]++; |
|---|
| 585 |
} |
|---|
| 586 |
else if (dicts[m][(x,y)] == co) { |
|---|
| 587 |
del dicts[m][(x,y)]; |
|---|
| 588 |
dictsNO[m]--; |
|---|
| 589 |
} |
|---|
| 590 |
} |
|---|
| 591 |
} |
|---|
| 592 |
} |
|---|
| 593 |
else if (mainlistArr[movelistIndex+1] & REMOVE) { |
|---|
| 594 |
if (mainlistArr[movelistIndex+1] & BLACK) { |
|---|
| 595 |
co = 'X'; |
|---|
| 596 |
invco = 'O'; |
|---|
| 597 |
} |
|---|
| 598 |
else if mainlistArr[movelistIndex+1] & WHITE { |
|---|
| 599 |
co = 'O'; |
|---|
| 600 |
invco = 'X'; |
|---|
| 601 |
} |
|---|
| 602 |
else printf("oops\n"); |
|---|
| 603 |
|
|---|
| 604 |
for (m in dicts.keys()) { |
|---|
| 605 |
if (!dicts[m].has_key('found')) { |
|---|
| 606 |
if (Xinterv[m][0] <= x && x < Xinterv[m][1] && Yinterv[m][0] <= y && y < Yinterv[m][1]) { |
|---|
| 607 |
if (dicts[m].has_key('foundInitial')) { |
|---|
| 608 |
int ii = contListIndex[m]; |
|---|
| 609 |
while (ii < len(contList[m]) && contList[m][ii][2] == '-' && |
|---|
| 610 |
(x != contList[m][ii][0] || y != contList[m][ii][1])) |
|---|
| 611 |
ii++; |
|---|
| 612 |
if (ii < len(contList[m]) && contList[m][ii][2] == '-') { |
|---|
| 613 |
help = contList[m][ii]; |
|---|
| 614 |
contList[m][ii] = contList[m][contListIndex[m]]; |
|---|
| 615 |
contlist[contListIndex[m]] = help; |
|---|
| 616 |
|
|---|
| 617 |
contListIndex[m]++; |
|---|
| 618 |
} |
|---|
| 619 |
else { |
|---|
| 620 |
if (dicts[m].has_key('dontRestore')) { |
|---|
| 621 |
del dicts[m]; |
|---|
| 622 |
continue; |
|---|
| 623 |
} |
|---|
| 624 |
else { |
|---|
| 625 |
contListIndex[m] = 0; |
|---|
| 626 |
del dicts[m]['foundInitial']; |
|---|
| 627 |
} |
|---|
| 628 |
} |
|---|
| 629 |
} |
|---|
| 630 |
if (!dicts[m].has_key((x,y))) { |
|---|
| 631 |
dicts[m][(x,y)] = co; |
|---|
| 632 |
dictsNO[m]++; |
|---|
| 633 |
} |
|---|
| 634 |
else if (dicts[m][(x,y)] == lower(invco)) { |
|---|
| 635 |
dictsNO[m]--; |
|---|
| 636 |
} |
|---|
| 637 |
else if (dicts[m][(x,y)] == '.') { |
|---|
| 638 |
del dicts[m][(x,y)]; |
|---|
| 639 |
dictsNO[m]--; |
|---|
| 640 |
} |
|---|
| 641 |
} |
|---|
| 642 |
} |
|---|
| 643 |
} |
|---|
| 644 |
} |
|---|
| 645 |
|
|---|
| 646 |
if (endOfNode) { |
|---|
| 647 |
for (m in dicts.keys()) { |
|---|
| 648 |
if (!dictsNO[m] && !dicts[m].has_key('found') && !dicts[m].has_key('foundInitial')) |
|---|
| 649 |
if (contListIndex[m] == len(contList[m])) dicts[m]['found'] = counter; |
|---|
| 650 |
else dicts[m]['foundInitial'] = counter; |
|---|
| 651 |
} |
|---|
| 652 |
|
|---|
| 653 |
counter++; |
|---|
| 654 |
} |
|---|
| 655 |
|
|---|
| 656 |
if (dicts=={} && !self.branchpoints) break; |
|---|
| 657 |
movelistIndex += 2; |
|---|
| 658 |
} |
|---|
| 659 |
|
|---|
| 660 |
if (!patternList.nextMove) { |
|---|
| 661 |
for (m in dicts.keys()) { |
|---|
| 662 |
Pattern p = patternList.get(m[0]); |
|---|
| 663 |
if (dicts[m].has_key('found')) { |
|---|
| 664 |
if (p.colorSwitch) numOfSwitched++; |
|---|
| 665 |
if (p.colorSwitch && showColorSwap) result.append((dicts[m]['found'], '-')); |
|---|
| 666 |
else result.append((dicts[m]['found'], '')); |
|---|
| 667 |
} |
|---|
| 668 |
} |
|---|
| 669 |
} |
|---|
| 670 |
|
|---|
| 671 |
if (result) { |
|---|
| 672 |
numOfHits += len(result); |
|---|
| 673 |
self_numOfSwitched += numOfSwitched; |
|---|
| 674 |
|
|---|
| 675 |
if (db.getCurrentWinner() == 'B') { |
|---|
| 676 |
Bwins += len(result) - numOfSwitched; |
|---|
| 677 |
Wwins += Wwins + numOfSwitched; |
|---|
| 678 |
} |
|---|
| 679 |
else if (db.getCurrentWinner() == 'W') { |
|---|
| 680 |
Bwins += numOfSwitched; |
|---|
| 681 |
Wwins += len(result) - numOfSwitched; |
|---|
| 682 |
} |
|---|
| 683 |
result.sort(); |
|---|
| 684 |
db.makeCurrentHit(join([str(x[0])+x[1] for x in result], ', ')); |
|---|
| 685 |
} |
|---|
| 686 |
else db.discardCurrent(); |
|---|
| 687 |
|
|---|
| 688 |
index = db.next(); |
|---|
| 689 |
} |
|---|
| 690 |
return numOfHits, Bwins, Wwins, self.numOfSwitched; |
|---|
| 691 |
} |
|---|
| 692 |
|
|---|
| 693 |
|
|---|
| 694 |
|
|---|
| 695 |
|
|---|
| 696 |
|
|---|
| 697 |
|
|---|
| 698 |
|
|---|
| 699 |
|
|---|
| 700 |
|
|---|
| 701 |
|
|---|
| 702 |
|
|---|
| 703 |
|
|---|
| 704 |
|
|---|
| 705 |
|
|---|
| 706 |
|
|---|
| 707 |
|
|---|
| 708 |
|
|---|
| 709 |
|
|---|
| 710 |
|
|---|
| 711 |
|
|---|
| 712 |
|
|---|
| 713 |
|
|---|
| 714 |
|
|---|
| 715 |
|
|---|
| 716 |
|
|---|
| 717 |
|
|---|
| 718 |
|
|---|
| 719 |
|
|---|
| 720 |
|
|---|
| 721 |
|
|---|
| 722 |
|
|---|
| 723 |
|
|---|
| 724 |
|
|---|
| 725 |
|
|---|
| 726 |
|
|---|
| 727 |
|
|---|
| 728 |
|
|---|
| 729 |
|
|---|
| 730 |
|
|---|
| 731 |
|
|---|
| 732 |
|
|---|
| 733 |
|
|---|
| 734 |
|
|---|
| 735 |
|
|---|
| 736 |
|
|---|
| 737 |
|
|---|
| 738 |
|
|---|
| 739 |
|
|---|
| 740 |
|
|---|
| 741 |
|
|---|
| 742 |
|
|---|
| 743 |
|
|---|
| 744 |
|
|---|
| 745 |
|
|---|
| 746 |
|
|---|
| 747 |
|
|---|
| 748 |
|
|---|
| 749 |
|
|---|
| 750 |
|
|---|
| 751 |
|
|---|
| 752 |
|
|---|
| 753 |
|
|---|
| 754 |
|
|---|
| 755 |
|
|---|
| 756 |
|
|---|
| 757 |
|
|---|
| 758 |
|
|---|
| 759 |
|
|---|
| 760 |
|
|---|
| 761 |
|
|---|
| 762 |
|
|---|
| 763 |
|
|---|
| 764 |
|
|---|
| 765 |
|
|---|
| 766 |
|
|---|
| 767 |
|
|---|
| 768 |
|
|---|
| 769 |
|
|---|
| 770 |
|
|---|
| 771 |
|
|---|
| 772 |
|
|---|
| 773 |
|
|---|
| 774 |
|
|---|
| 775 |
|
|---|
| 776 |
|
|---|
| 777 |
|
|---|
| 778 |
|
|---|
| 779 |
|
|---|
| 780 |
|
|---|
| 781 |
|
|---|
| 782 |
|
|---|
| 783 |
|
|---|
| 784 |
|
|---|
| 785 |
|
|---|
| 786 |
|
|---|
| 787 |
|
|---|
| 788 |
|
|---|
| 789 |
|
|---|
| 790 |
|
|---|
| 791 |
|
|---|
| 792 |
|
|---|
| 793 |
|
|---|
| 794 |
|
|---|
| 795 |
|
|---|
| 796 |
|
|---|
| 797 |
|
|---|
| 798 |
|
|---|
| 799 |
|
|---|
| 800 |
|
|---|
| 801 |
|
|---|
| 802 |
|
|---|
| 803 |
|
|---|
| 804 |
|
|---|
| 805 |
|
|---|
| 806 |
|
|---|
| 807 |
|
|---|
| 808 |
|
|---|
| 809 |
|
|---|
| 810 |
|
|---|
| 811 |
|
|---|
| 812 |
|
|---|
| 813 |
|
|---|
| 814 |
|
|---|
| 815 |
|
|---|
| 816 |
|
|---|
| 817 |
|
|---|
| 818 |
|
|---|
| 819 |
|
|---|
| 820 |
|
|---|
| 821 |
|
|---|
| 822 |
|
|---|
| 823 |
|
|---|
| 824 |
|
|---|
| 825 |
|
|---|
| 826 |
|
|---|
| 827 |
|
|---|
| 828 |
|
|---|
| 829 |
|
|---|
| 830 |
|
|---|
| 831 |
|
|---|
| 832 |
|
|---|
| 833 |
|
|---|
| 834 |
|
|---|
| 835 |
|
|---|
| 836 |
|
|---|
| 837 |
|
|---|
| 838 |
|
|---|
| 839 |
|
|---|
| 840 |
|
|---|
| 841 |
|
|---|
| 842 |
|
|---|
| 843 |
|
|---|
| 844 |
|
|---|
| 845 |
|
|---|
| 846 |
|
|---|
| 847 |
|
|---|
| 848 |
|
|---|
| 849 |
|
|---|
| 850 |
|
|---|
| 851 |
|
|---|
| 852 |
|
|---|
| 853 |
|
|---|
| 854 |
|
|---|
| 855 |
|
|---|
| 856 |
|
|---|
| 857 |
|
|---|
| 858 |
|
|---|
| 859 |
|
|---|
| 860 |
|
|---|
| 861 |
|
|---|
| 862 |
|
|---|
| 863 |
|
|---|
| 864 |
|
|---|
| 865 |
|
|---|
| 866 |
|
|---|
| 867 |
|
|---|
| 868 |
|
|---|
| 869 |
|
|---|
| 870 |
|
|---|
| 871 |
|
|---|
| 872 |
|
|---|
| 873 |
|
|---|
| 874 |
|
|---|
| 875 |
|
|---|
| 876 |
|
|---|
| 877 |
|
|---|
| 878 |
|
|---|
| 879 |
|
|---|
| 880 |
|
|---|
| 881 |
|
|---|
| 882 |
|
|---|
| 883 |
|
|---|
| 884 |
|
|---|
| 885 |
|
|---|
| 886 |
|
|---|
| 887 |
|
|---|
| 888 |
|
|---|
| 889 |
|
|---|
| 890 |
|
|---|
| 891 |
|
|---|
| 892 |
|
|---|
| 893 |
|
|---|
| 894 |
|
|---|
| 895 |
|
|---|
| 896 |
|
|---|
| 897 |
|
|---|
| 898 |
|
|---|
| 899 |
|
|---|
| 900 |
|
|---|
| 901 |
|
|---|
| 902 |
|
|---|
| 903 |
|
|---|
|
|---|