| 1 |
/* oracleDisprover.tcc |
| 2 |
*/ |
| 3 |
#ifndef _ORACLEDISPROVER_TCC |
| 4 |
#define _ORACLEDISPROVER_TCC |
| 5 |
|
| 6 |
#include "oracleDisprover.h" |
| 7 |
#include "checkHashTable.h" |
| 8 |
#include "checkMoveGenerator.h" |
| 9 |
#include "checkTableUtil.h" |
| 10 |
#include "checkmateRecorder.h" |
| 11 |
#include "sameBoardList.h" |
| 12 |
#include "osl/checkmate/proofPieces.h" |
| 13 |
#include "osl/apply_move/applyMoveWithPath.h" |
| 14 |
#include <iostream> |
| 15 |
|
| 16 |
// 不変条件: (! record->needMoveGeneration()) の時は手生成が終了していること |
| 17 |
namespace osl |
| 18 |
{ |
| 19 |
namespace checkmate |
| 20 |
{ |
| 21 |
/** |
| 22 |
* OracleDisprover::defense を呼ぶhelper |
| 23 |
* @param P 攻撃側 |
| 24 |
*/ |
| 25 |
template <Player P, class Disprover> |
| 26 |
struct OracleDisproverDefense |
| 27 |
{ |
| 28 |
Disprover *prover; |
| 29 |
CheckHashRecord *record; |
| 30 |
const DisproofOracleDefense<P>& oracle; |
| 31 |
OracleDisproverDefense(Disprover *p, CheckHashRecord *r, |
| 32 |
const DisproofOracleDefense<P>& o) |
| 33 |
: prover(p), record(r), oracle(o) |
| 34 |
{ |
| 35 |
} |
| 36 |
void operator()(Square) |
| 37 |
{ |
| 38 |
assert(! record->isVisited); |
| 39 |
(*prover).template defense<P>(record, oracle); |
| 40 |
record->isVisited = false; |
| 41 |
} |
| 42 |
}; |
| 43 |
|
| 44 |
/** |
| 45 |
* OracleDisprover::attack を呼ぶhelper |
| 46 |
* @param P 攻撃側 |
| 47 |
*/ |
| 48 |
template <Player P, class Disprover> |
| 49 |
struct OracleDisproverAttack |
| 50 |
{ |
| 51 |
Disprover *prover; |
| 52 |
CheckHashRecord *record; |
| 53 |
const DisproofOracleAttack<P>& oracle; |
| 54 |
OracleDisproverAttack(Disprover *p, CheckHashRecord *r, |
| 55 |
const DisproofOracleAttack<P>& o) |
| 56 |
: prover(p), record(r), oracle(o) |
| 57 |
{ |
| 58 |
} |
| 59 |
void operator()(Square) |
| 60 |
{ |
| 61 |
assert(! record->isVisited); |
| 62 |
(*prover).template attack<P>(record, oracle); |
| 63 |
record->isVisited = false; |
| 64 |
} |
| 65 |
}; |
| 66 |
|
| 67 |
} // namespace checkmate |
| 68 |
} // namespace osl |
| 69 |
|
| 70 |
template <class Table> |
| 71 |
template <osl::Player P> |
| 72 |
bool osl::checkmate::OracleDisprover<Table>:: |
| 73 |
proofNoCheckmate(NumEffectState& state, |
| 74 |
const HashKey& key, const PathEncoding& root_path, |
| 75 |
const DisproofOracleAttack<P>& oracle) |
| 76 |
{ |
| 77 |
check_assert(oracle.isValid()); |
| 78 |
check_assert(state.turn() == P); |
| 79 |
path = root_path; |
| 80 |
this->state = &state; |
| 81 |
this->key = key; |
| 82 |
CheckmateRecorder::setState(&state); |
| 83 |
CheckHashRecord *record = table.find(key); |
| 84 |
if (! record) |
| 85 |
{ |
| 86 |
const PieceStand white_stand(WHITE, state); |
| 87 |
CheckHashRecord *root = table.root(); |
| 88 |
root->distance = path.getDepth()-1; |
| 89 |
CheckTableUtil::allocate(record, table, key, white_stand, path, root); |
| 90 |
} |
| 91 |
|
| 92 |
if ((! record->proofDisproof().isFinal()) |
| 93 |
&& (! record->findLoop(path, table.getTwinTable()))) |
| 94 |
{ |
| 95 |
const bool visitedBeforeAttack = record->isVisited; |
| 96 |
CheckmateRecorder::setNextMove(0); |
| 97 |
record->isVisited = false; |
| 98 |
attack<P>(record, oracle); |
| 99 |
record->isVisited = visitedBeforeAttack; |
| 100 |
} |
| 101 |
if (record->proofDisproof().isCheckmateFail() |
| 102 |
|| record->findLoop(path, table.getTwinTable())) |
| 103 |
return true; |
| 104 |
|
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
template <class Table> |
| 109 |
template <osl::Player P> |
| 110 |
bool osl::checkmate::OracleDisprover<Table>:: |
| 111 |
proofEscape(NumEffectState& state, |
| 112 |
const HashKey& key, const PathEncoding& root_path, |
| 113 |
const DisproofOracleDefense<P>& oracle, |
| 114 |
Move& best_move, Move last_move) |
| 115 |
{ |
| 116 |
check_assert(oracle.isValid()); |
| 117 |
check_assert(state.turn() == alt(P)); |
| 118 |
#ifdef CHECKMATE_DEBUG |
| 119 |
check_assert(oracle.guide->proofDisproof().isCheckmateFail() |
| 120 |
|| oracle.guide->findLoop(oracle.path, table.getTwinTable()) |
| 121 |
|| ((! oracle.guide->twins.empty()) |
| 122 |
&& oracle.guide->hasBestMove() |
| 123 |
&& oracle.guide->getBestMove()->findLoop(oracle.path, table.getTwinTable())) |
| 124 |
|| oracle.guide->proofDisproof() == ProofDisproof::NoEscape() |
| 125 |
|| (oracle.guide->dump(), 0)); |
| 126 |
#endif |
| 127 |
path = root_path; |
| 128 |
this->state = &state; |
| 129 |
this->key = key; |
| 130 |
CheckmateRecorder::setState(&state); |
| 131 |
CheckHashRecord *record = table.find(key); |
| 132 |
if (! record) |
| 133 |
{ |
| 134 |
const PieceStand white_stand(WHITE, state); |
| 135 |
CheckHashRecord *root = table.root(); |
| 136 |
root->distance = path.getDepth()-1; |
| 137 |
CheckTableUtil::allocate(record, table, key, white_stand, path, root); |
| 138 |
} |
| 139 |
|
| 140 |
if ((! record->proofDisproof().isFinal()) |
| 141 |
&& (! record->findLoop(path, table.getTwinTable()))) |
| 142 |
{ |
| 143 |
const bool visitedBeforeDefense = record->isVisited; |
| 144 |
CheckmateRecorder::setNextMove(0); |
| 145 |
record->isVisited = false; |
| 146 |
defense<P>(record, oracle); |
| 147 |
record->isVisited = visitedBeforeDefense; |
| 148 |
} |
| 149 |
if (record->proofDisproof().isCheckmateFail() |
| 150 |
|| record->findLoop(path, table.getTwinTable())) |
| 151 |
{ |
| 152 |
if (record->hasBestMove()) |
| 153 |
{ |
| 154 |
best_move = record->getBestMove()->move; |
| 155 |
check_assert(best_move.isValid()); |
| 156 |
check_assert(best_move.player() == alt(P)); |
| 157 |
} |
| 158 |
else |
| 159 |
{ |
| 160 |
check_assert(record->findLoop(path, table.getTwinTable())); |
| 161 |
} |
| 162 |
return true; |
| 163 |
} |
| 164 |
if (record->proofDisproof().isPawnDropFoul(last_move)) |
| 165 |
return true; |
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
template <class Table> |
| 170 |
template <osl::Player P> |
| 171 |
void osl::checkmate::OracleDisprover<Table>:: |
| 172 |
attack(CheckHashRecord *record, const DisproofOracleAttack<P>& oracle) |
| 173 |
{ |
| 174 |
++node_count; |
| 175 |
#ifndef NDEBUG |
| 176 |
CheckmateRecorder::Tracer tracer("disproof oracle attack ", record, |
| 177 |
key, path, 0, 0); |
| 178 |
#endif |
| 179 |
check_assert(oracle.isValid()); |
| 180 |
check_assert(state->turn() == P); |
| 181 |
check_assert(record); |
| 182 |
check_assert(! record->isVisited); |
| 183 |
check_assert(! record->findLoop(path, table.getTwinTable())); |
| 184 |
record->isVisited = true; |
| 185 |
check_assert(! record->proofDisproof().isFinal()); |
| 186 |
|
| 187 |
#ifndef NDEBUG |
| 188 |
const Square target_king_square |
| 189 |
=(*state).template kingSquare<PlayerTraits<P>::opponent>(); |
| 190 |
// 逃げる手になっていなかった場合 |
| 191 |
check_assert(! state->hasEffectAt(P,target_king_square)); |
| 192 |
#endif |
| 193 |
|
| 194 |
if (record->sameBoards) |
| 195 |
{ |
| 196 |
const CheckHashRecord *loop = |
| 197 |
(*record->sameBoards).template |
| 198 |
findIneffectiveDropLoop<P>(key.blackStand()); |
| 199 |
if (loop) |
| 200 |
{ |
| 201 |
record->setLoopDetection(path, loop); |
| 202 |
CheckmateRecorder::setLeaveReason("loop confirmation (drop)"); |
| 203 |
return; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
if (record->needMoveGeneration()) |
| 208 |
{ |
| 209 |
bool has_pawn_checkmate=false; |
| 210 |
CheckMoveGenerator<P>::generateAttack(*state, table.listProvider(), |
| 211 |
record->moves, has_pawn_checkmate); |
| 212 |
if (record->moves.empty()) |
| 213 |
{ |
| 214 |
record->setDisproofPieces(DisproofPieces::leaf(*state, alt(P), |
| 215 |
record->stand(alt(P)))); |
| 216 |
record->propagateNoCheckmate<P>(has_pawn_checkmate |
| 217 |
? ProofDisproof::NoCheckmate() |
| 218 |
: ProofDisproof::PawnCheckmate()); |
| 219 |
CheckmateRecorder::setLeaveReason("no attack moves"); |
| 220 |
return; |
| 221 |
} |
| 222 |
if (has_pawn_checkmate) |
| 223 |
record->updateBestResultInSolvedAttack(ProofDisproof::PawnCheckmate()); |
| 224 |
check_assert(record->moves.size()); |
| 225 |
record->setProofDisproof(1, record->moves.size()); |
| 226 |
} |
| 227 |
record->filter = MoveFilter(); // danger? |
| 228 |
ProofDisproof bestResultInSolved = record->bestResultInSolved; |
| 229 |
examine_moves: |
| 230 |
int disproved = 0; |
| 231 |
unsigned int sum_disproof = 0; // 証明失敗があったかどうかにも使う |
| 232 |
unsigned int min_proof = ProofDisproof::PROOF_LIMIT; |
| 233 |
|
| 234 |
HashKey new_key; |
| 235 |
PathEncoding new_path; |
| 236 |
|
| 237 |
for (CheckMoveList::iterator p=record->moves.begin(); |
| 238 |
p!=record->moves.end(); ++p) |
| 239 |
{ |
| 240 |
check_assert(p->move.player() == P); |
| 241 |
if (! record->filter.isTarget(p->flags)) // 不成など |
| 242 |
continue; |
| 243 |
new_key = key.newHashWithMove(p->move); |
| 244 |
new_path = PathEncoding(path, p->move); |
| 245 |
if (! p->record) |
| 246 |
{ |
| 247 |
CheckTableUtil::allocate(p->move, p->record, table, |
| 248 |
new_key, new_path, record); |
| 249 |
} |
| 250 |
|
| 251 |
if (const TwinEntry *loop |
| 252 |
= p->findLoop(path, table.getTwinTable())) |
| 253 |
{ |
| 254 |
#ifdef PAWN_CHECKMATE_SENSITIVE |
| 255 |
if ((record->bestResultInSolved != ProofDisproof::PawnCheckmate()) |
| 256 |
&& loop->move.record |
| 257 |
&& (loop->move.record->bestResultInSolved == ProofDisproof::PawnCheckmate())) |
| 258 |
{ |
| 259 |
record->bestResultInSolved |
| 260 |
= record->bestResultInSolved.betterForAttack(ProofDisproof::PawnCheckmate()); |
| 261 |
} |
| 262 |
#endif |
| 263 |
bestResultInSolved |
| 264 |
= bestResultInSolved.betterForAttack(ProofDisproof::LoopDetection()); |
| 265 |
continue; |
| 266 |
} |
| 267 |
if (p->record->isVisited) |
| 268 |
{ |
| 269 |
if (p->record->proofDisproof() == ProofDisproof::PawnCheckmate()) |
| 270 |
record->bestResultInSolved |
| 271 |
= record->bestResultInSolved.betterForAttack(ProofDisproof::PawnCheckmate()); |
| 272 |
bestResultInSolved |
| 273 |
= bestResultInSolved.betterForAttack(ProofDisproof::LoopDetection()); |
| 274 |
continue; |
| 275 |
} |
| 276 |
const ProofDisproof& pdp = p->record->proofDisproof(); |
| 277 |
if (! pdp.isFinal()) |
| 278 |
{ |
| 279 |
DisproofOracleDefense<P> new_oracle = oracle.expandOracle(p->move); |
| 280 |
if (! new_oracle.isValid()) |
| 281 |
{ |
| 282 |
CheckmateRecorder::setLeaveReason("invalid oracle"); |
| 283 |
#ifdef CHECKMATE_DEBUG |
| 284 |
CheckmateRecorder::DepthTracer::stream() << p->move << "\n"; |
| 285 |
#endif |
| 286 |
return; |
| 287 |
} |
| 288 |
if (! pdp.isFinal()) |
| 289 |
{ |
| 290 |
OracleDisproverDefense<P,OracleDisprover> |
| 291 |
oracle_disprover(this, p->record, new_oracle); |
| 292 |
CheckmateRecorder::setNextMove(&*p); |
| 293 |
|
| 294 |
std::swap(key, new_key); |
| 295 |
std::swap(path, new_path); |
| 296 |
ApplyMove<P>::doUndoMove(*state, p->move,oracle_disprover); |
| 297 |
key = new_key; |
| 298 |
path = new_path; |
| 299 |
} |
| 300 |
} |
| 301 |
if (pdp.isCheckmateFail()) |
| 302 |
{ |
| 303 |
check_assert(! pdp.isLoopDetection()); |
| 304 |
record->addToSolvedInAttack(*p, pdp); |
| 305 |
++disproved; |
| 306 |
continue; |
| 307 |
} |
| 308 |
if (pdp.isPawnDropFoul(p->move)) |
| 309 |
{ |
| 310 |
record->addToSolvedInAttack(*p, ProofDisproof::PawnCheckmate()); |
| 311 |
++disproved; |
| 312 |
continue; |
| 313 |
} |
| 314 |
if (p->findLoop(path, table.getTwinTable())) |
| 315 |
{ |
| 316 |
bestResultInSolved |
| 317 |
= bestResultInSolved.betterForAttack(ProofDisproof::LoopDetection()); |
| 318 |
++disproved; |
| 319 |
continue; |
| 320 |
} |
| 321 |
assert(! pdp.isCheckmateFail()); |
| 322 |
record->bestMove = &*p; |
| 323 |
if (pdp.isCheckmateSuccess()) |
| 324 |
{ |
| 325 |
CheckmateRecorder::setLeaveReason("checkmate move found in attack"); |
| 326 |
#ifdef CHECKMATE_DEBUG |
| 327 |
CheckmateRecorder::DepthTracer::stream() << p->move << "\n"; |
| 328 |
#endif |
| 329 |
if (! record->proofDisproof().isCheckmateSuccess()) |
| 330 |
{ |
| 331 |
record->setProofPiecesAttack(P); |
| 332 |
record->propagateCheckmate<P>(pdp); |
| 333 |
check_assert(record->isConsistent()); |
| 334 |
} |
| 335 |
return; |
| 336 |
} |
| 337 |
sum_disproof += pdp.disproof(); |
| 338 |
min_proof = std::min(min_proof, pdp.proof()); |
| 339 |
} |
| 340 |
if (sum_disproof && (disproved == 0)) |
| 341 |
{ |
| 342 |
CheckmateRecorder::setLeaveReason("no new nocheckmate found"); |
| 343 |
assert(min_proof < ProofDisproof::PROOF_LIMIT); |
| 344 |
assert(sum_disproof < ProofDisproof::DISPROOF_LIMIT); |
| 345 |
check_assert(! record->proofDisproof().isFinal()); |
| 346 |
record->setProofDisproof(std::max(record->proof(), min_proof), |
| 347 |
std::max(record->disproof(), sum_disproof)); |
| 348 |
return; |
| 349 |
} |
| 350 |
if (sum_disproof) |
| 351 |
goto examine_moves; |
| 352 |
#ifdef DELAY_SACRIFICE |
| 353 |
if (! record->filter.isTarget(MoveFlags::SacrificeAttack)) |
| 354 |
{ |
| 355 |
record->filter.addTarget(MoveFlags::SacrificeAttack); |
| 356 |
goto examine_moves; |
| 357 |
} |
| 358 |
#endif |
| 359 |
if ((record->bestResultInSolved == ProofDisproof::PawnCheckmate()) |
| 360 |
&& (! record->filter.isTarget(MoveFlags::NoPromote))) |
| 361 |
{ |
| 362 |
if (! oracle.guide->filter.isTarget(MoveFlags::NoPromote)) |
| 363 |
{ |
| 364 |
CheckmateRecorder::setLeaveReason("NoPromote in oracle"); |
| 365 |
return; |
| 366 |
} |
| 367 |
record->filter.addTarget(MoveFlags::NoPromote); |
| 368 |
goto examine_moves; |
| 369 |
} |
| 370 |
if (! record->filter.isTarget(MoveFlags::Upward)) |
| 371 |
{ |
| 372 |
record->filter.addTarget(MoveFlags::Upward); |
| 373 |
record->useMaxInsteadOfSum = true; |
| 374 |
goto examine_moves; |
| 375 |
} |
| 376 |
bestResultInSolved = |
| 377 |
bestResultInSolved.betterForAttack(record->bestResultInSolved); |
| 378 |
check_assert(bestResultInSolved.isCheckmateFail()); |
| 379 |
if (bestResultInSolved.isLoopDetection()) |
| 380 |
{ |
| 381 |
if (oracle.guide == record) |
| 382 |
{ |
| 383 |
// twin simulation |
| 384 |
assert(! record->twins.empty()); |
| 385 |
table.addLoopDetection(path); |
| 386 |
} |
| 387 |
else |
| 388 |
{ |
| 389 |
record->setLoopDetectionInAttack<P>(path); |
| 390 |
} |
| 391 |
} |
| 392 |
else |
| 393 |
{ |
| 394 |
record->setDisproofPieces(DisproofPieces::attack(record->moves, *state, |
| 395 |
record->stand(alt(P)))); |
| 396 |
record->propagateNoCheckmate<P>(record->bestResultInSolved); |
| 397 |
record->twins.clear(); |
| 398 |
} |
| 399 |
return; |
| 400 |
} |
| 401 |
|
| 402 |
namespace osl |
| 403 |
{ |
| 404 |
namespace checkmate |
| 405 |
{ |
| 406 |
template <class State, Player Attacker> |
| 407 |
struct ConfirmNoEscape |
| 408 |
{ |
| 409 |
State *state; |
| 410 |
bool *result; |
| 411 |
ConfirmNoEscape(State *s, bool *r) : state(s), result(r) |
| 412 |
{ |
| 413 |
} |
| 414 |
void operator()(Square) |
| 415 |
{ |
| 416 |
const Square defense_king_position |
| 417 |
= (*state).template kingSquare<PlayerTraits<Attacker>::opponent>(); |
| 418 |
*result = state->hasEffectAt(Attacker, defense_king_position); |
| 419 |
} |
| 420 |
}; |
| 421 |
} // namespace checkmate |
| 422 |
} // namespace osl |
| 423 |
|
| 424 |
template <class Table> |
| 425 |
template <osl::Player P> |
| 426 |
void osl::checkmate::OracleDisprover<Table>:: |
| 427 |
confirmNoEscape(CheckHashRecord *record) |
| 428 |
{ |
| 429 |
if (record->moves.empty()) |
| 430 |
CheckMoveGenerator<P>::generateEscape(*state, table.listProvider(), |
| 431 |
record->moves); |
| 432 |
if (record->moves.empty()) |
| 433 |
{ |
| 434 |
record->setProofPieces(ProofPieces::leaf(*state, P, record->stand(P))); |
| 435 |
record->propagateCheckmate<P>(ProofDisproof::NoEscape()); |
| 436 |
check_assert(record->isConsistent()); |
| 437 |
return; |
| 438 |
} |
| 439 |
// safe move ならここの処理は不要 |
| 440 |
} |
| 441 |
|
| 442 |
template <class Table> |
| 443 |
template <osl::Player P> |
| 444 |
void osl::checkmate::OracleDisprover<Table>:: |
| 445 |
defense(CheckHashRecord *record, const DisproofOracleDefense<P>& oracle) |
| 446 |
{ |
| 447 |
++node_count; |
| 448 |
#ifndef NDEBUG |
| 449 |
CheckmateRecorder::Tracer tracer("disproof oracle defense", record, |
| 450 |
key, path, 0, 0); |
| 451 |
#endif |
| 452 |
check_assert(oracle.isValid()); |
| 453 |
check_assert(record); |
| 454 |
check_assert(! record->isVisited); |
| 455 |
record->isVisited = true; |
| 456 |
check_assert(! record->proofDisproof().isFinal()); |
| 457 |
check_assert(! record->findLoop(path, table.getTwinTable())); |
| 458 |
#ifdef CHECKMATE_DEBUG |
| 459 |
const Square attack_king_position=(*state).template kingSquare<P>(); |
| 460 |
const Square defense_king_position=(*state).template kingSquare<PlayerTraits<P>::opponent>(); |
| 461 |
check_assert(attack_king_position.isPieceStand() |
| 462 |
|| ! state->hasEffectAt(alt(P),attack_king_position)); // 王手をかけたら自玉が取られる |
| 463 |
check_assert(state->hasEffectAt(P,defense_king_position)); // 王手になってない |
| 464 |
#endif |
| 465 |
if (oracle.guide->proofDisproof() == ProofDisproof::NoEscape()) |
| 466 |
{ |
| 467 |
confirmNoEscape<P>(record); |
| 468 |
return; |
| 469 |
} |
| 470 |
|
| 471 |
if (record->sameBoards) |
| 472 |
{ |
| 473 |
const CheckHashRecord *loop = |
| 474 |
(*record->sameBoards).template |
| 475 |
findIneffectiveDropLoop<P>(key.blackStand()); |
| 476 |
if (loop) |
| 477 |
{ |
| 478 |
if (oracle.guide == record) |
| 479 |
{ |
| 480 |
// twin simulation |
| 481 |
assert(! record->twins.empty()); |
| 482 |
table.addLoopDetection(path, loop); |
| 483 |
} |
| 484 |
else |
| 485 |
{ |
| 486 |
record->setLoopDetection(path, loop); |
| 487 |
} |
| 488 |
CheckmateRecorder::setLeaveReason("loop confirmation (drop)"); |
| 489 |
return; |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
if (oracle.best_move.isInvalid()) |
| 494 |
{ |
| 495 |
// TODO: この分岐は事前にとれそうな気がする |
| 496 |
CheckmateRecorder::setLeaveReason("empty defense oracle"); |
| 497 |
return; |
| 498 |
} |
| 499 |
assert(! oracle.best_move.isPass()); |
| 500 |
Move escape_move = oracle.best_move; |
| 501 |
check_assert(escape_move.player() == alt(P)); |
| 502 |
assert(escape_move.isValid()); |
| 503 |
if (! escape_move.isDrop()) |
| 504 |
{ |
| 505 |
const Piece existing = state->pieceOnBoard(escape_move.to()); |
| 506 |
if (existing.ptype() == KING) |
| 507 |
return; |
| 508 |
escape_move = escape_move.newCapture(existing); |
| 509 |
} |
| 510 |
if (! (*state).template isAlmostValidMove<false>(escape_move)) |
| 511 |
{ |
| 512 |
CheckmateRecorder::setLeaveReason("invalid defense oracle"); |
| 513 |
return; |
| 514 |
} |
| 515 |
|
| 516 |
if (record->moves.empty()) |
| 517 |
CheckMoveGenerator<P>::generateEscape(*state, table.listProvider(), |
| 518 |
record->moves); |
| 519 |
|
| 520 |
record->bestMove = 0; |
| 521 |
for (CheckMoveList::iterator p=record->moves.begin(); |
| 522 |
p!=record->moves.end(); ++p) |
| 523 |
{ |
| 524 |
if (p->move == escape_move) |
| 525 |
{ |
| 526 |
if (! p->flags.isSet(MoveFlags::Solved)) |
| 527 |
record->bestMove = &*p; |
| 528 |
break; |
| 529 |
} |
| 530 |
} |
| 531 |
unsigned int best_move_proof = 1; |
| 532 |
unsigned int best_move_disproof = 1; |
| 533 |
HashKey new_key = key.newHashWithMove(escape_move); |
| 534 |
PathEncoding new_path(path, escape_move); |
| 535 |
|
| 536 |
if (! record->hasBestMove()) // 合法な受手でなかった場合など |
| 537 |
goto failure_end; |
| 538 |
check_assert(record->hasBestMove() |
| 539 |
&& (record->getBestMove()->move == escape_move)); |
| 540 |
if (! record->getBestMove()->record) |
| 541 |
{ |
| 542 |
CheckTableUtil::allocate(escape_move, record->getBestMove()->record, |
| 543 |
table, new_key, new_path, record); |
| 544 |
if (record->proofDisproof().isFinal()) |
| 545 |
return; |
| 546 |
} |
| 547 |
|
| 548 |
if (record->getBestMove()->record->isVisited) |
| 549 |
{ |
| 550 |
if (oracle.guide == record) |
| 551 |
{ |
| 552 |
// twin simulation |
| 553 |
assert(! record->twins.empty()); |
| 554 |
table.addLoopDetection(path, |
| 555 |
*record->getBestMove(), record->getBestMove()->record); |
| 556 |
} |
| 557 |
else |
| 558 |
{ |
| 559 |
record->setLoopDetection(path, *record->getBestMove(), |
| 560 |
record->getBestMove()->record); |
| 561 |
} |
| 562 |
CheckmateRecorder::setLeaveReason("loop confirmation (v)"); |
| 563 |
return; |
| 564 |
} |
| 565 |
if (const TwinEntry *loop = record->getBestMove()->findLoop(path, table.getTwinTable())) |
| 566 |
{ |
| 567 |
if (oracle.guide == record) |
| 568 |
{ |
| 569 |
// twin simulation |
| 570 |
assert(! record->twins.empty()); |
| 571 |
table.addLoopDetection(path, *record->getBestMove(), loop->loopTo); |
| 572 |
} |
| 573 |
else |
| 574 |
{ |
| 575 |
record->setLoopDetectionTryMerge<P> |
| 576 |
(path, *record->getBestMove(), loop->loopTo); |
| 577 |
} |
| 578 |
CheckmateRecorder::setLeaveReason("loop confirmation (t)"); |
| 579 |
return; |
| 580 |
} |
| 581 |
|
| 582 |
{ |
| 583 |
const ProofDisproof& pdp = record->getBestMove()->record->proofDisproof(); |
| 584 |
if (! pdp.isFinal()) |
| 585 |
{ |
| 586 |
DisproofOracleAttack<P> new_oracle = oracle.expandOracle(); |
| 587 |
if (new_oracle.isValid()) |
| 588 |
{ |
| 589 |
OracleDisproverAttack<P,OracleDisprover> |
| 590 |
oracle_disprover(this, record->getBestMove()->record, new_oracle); |
| 591 |
CheckmateRecorder::setNextMove(&*record->getBestMove()); |
| 592 |
|
| 593 |
std::swap(key, new_key); |
| 594 |
std::swap(path, new_path); |
| 595 |
|
| 596 |
ApplyMove<PlayerTraits<P>::opponent>::doUndoMove |
| 597 |
(*state, escape_move,oracle_disprover); |
| 598 |
|
| 599 |
key = new_key; |
| 600 |
path = new_path; |
| 601 |
|
| 602 |
if (record->proofDisproof().isFinal()) |
| 603 |
return; |
| 604 |
} |
| 605 |
else |
| 606 |
{ |
| 607 |
#ifdef CHECKMATE_DEBUG |
| 608 |
CheckmateRecorder::DepthTracer::stream() << "oracle expand fail\n"; |
| 609 |
if (oracle.guide) |
| 610 |
oracle.guide->dump(CheckmateRecorder::DepthTracer::stream(),1); |
| 611 |
else |
| 612 |
CheckmateRecorder::DepthTracer::stream() << "no guide\n"; |
| 613 |
if (oracle.next_guide) |
| 614 |
oracle.next_guide->dump(CheckmateRecorder::DepthTracer::stream(),1); |
| 615 |
else |
| 616 |
CheckmateRecorder::DepthTracer::stream() << "no next guide\n"; |
| 617 |
#endif |
| 618 |
} |
| 619 |
} |
| 620 |
if (pdp.isCheckmateFail()) |
| 621 |
{ |
| 622 |
if (! record->proofDisproof().isCheckmateFail()) |
| 623 |
{ |
| 624 |
check_assert(! pdp.isLoopDetection()); |
| 625 |
record->setDisproofPiecesDefense(alt(P)); |
| 626 |
record->propagateNoCheckmate<P>(pdp); |
| 627 |
record->twins.clear(); |
| 628 |
} |
| 629 |
return; |
| 630 |
} |
| 631 |
if (const TwinEntry *loop |
| 632 |
= record->getBestMove()->findLoop(path, table.getTwinTable())) |
| 633 |
{ |
| 634 |
if (oracle.guide == record) |
| 635 |
{ |
| 636 |
// twin simulation |
| 637 |
assert(! record->twins.empty()); |
| 638 |
table.addLoopDetection(path, *record->getBestMove(), loop->loopTo); |
| 639 |
} |
| 640 |
else |
| 641 |
{ |
| 642 |
record-> |
| 643 |
setLoopDetectionTryMerge<P>(path, *record->getBestMove(), loop->loopTo); |
| 644 |
} |
| 645 |
CheckmateRecorder::setLeaveReason("loop confirmation after move"); |
| 646 |
return; |
| 647 |
} |
| 648 |
if (! pdp.isFinal()) |
| 649 |
{ |
| 650 |
best_move_proof = pdp.proof(); |
| 651 |
best_move_disproof = pdp.disproof(); // 本当は全てのminだが,代用 |
| 652 |
} |
| 653 |
} |
| 654 |
record->bestMove = 0; |
| 655 |
failure_end: |
| 656 |
CheckmateRecorder::setLeaveReason("disproof oracle not found"); |
| 657 |
#ifdef CHECKMATE_DEBUG |
| 658 |
CheckmateRecorder::DepthTracer::stream() << escape_move << " " << path << " " << oracle.path << "\n"; |
| 659 |
#endif |
| 660 |
if (record->proofDisproof().isFinal()) |
| 661 |
return; |
| 662 |
if (record->moves.empty()) |
| 663 |
{ |
| 664 |
record->setProofPieces(ProofPieces::leaf(*state, P, record->stand(P))); |
| 665 |
record->propagateCheckmate<P>(ProofDisproof::NoEscape()); |
| 666 |
check_assert(record->isConsistent()); |
| 667 |
} |
| 668 |
else |
| 669 |
{ |
| 670 |
check_assert(best_move_proof < ProofDisproof::PROOF_LIMIT); |
| 671 |
check_assert(best_move_disproof < ProofDisproof::DISPROOF_LIMIT); |
| 672 |
check_assert(! record->proofDisproof().isFinal()); |
| 673 |
record->setProofDisproof(std::max((size_t)record->proof(), |
| 674 |
best_move_proof + record->moves.size()), |
| 675 |
std::max(record->disproof(), |
| 676 |
best_move_disproof/4)); |
| 677 |
} |
| 678 |
|
| 679 |
return; |
| 680 |
} |
| 681 |
|
| 682 |
template <class Table> |
| 683 |
bool osl::checkmate::OracleDisprover<Table>:: |
| 684 |
proofNoCheckmate(NumEffectState& state, const PathEncoding& path, |
| 685 |
const CheckHashRecord *record, const PathEncoding& opath) |
| 686 |
{ |
| 687 |
const HashKey key(state); |
| 688 |
if (state.turn() == BLACK) |
| 689 |
{ |
| 690 |
DisproofOracleAttack<BLACK> oracle(record, opath); |
| 691 |
return proofNoCheckmate(state, key, path, oracle); |
| 692 |
} |
| 693 |
else |
| 694 |
{ |
| 695 |
DisproofOracleAttack<WHITE> oracle(record, opath); |
| 696 |
return proofNoCheckmate(state, key, path, oracle); |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
template <class Table> |
| 701 |
bool osl::checkmate::OracleDisprover<Table>:: |
| 702 |
proofEscape(NumEffectState& state, const PathEncoding& path, |
| 703 |
CheckHashRecord *record, const PathEncoding& opath, |
| 704 |
Move& best_move, Move last_move) |
| 705 |
{ |
| 706 |
const HashKey key(state); |
| 707 |
if (state.turn() == BLACK) |
| 708 |
{ |
| 709 |
DisproofOracleDefense<WHITE> oracle(record, opath); |
| 710 |
return proofEscape(state, key, path, oracle, best_move, last_move); |
| 711 |
} |
| 712 |
else |
| 713 |
{ |
| 714 |
DisproofOracleDefense<BLACK> oracle(record, opath); |
| 715 |
return proofEscape(state, key, path, oracle, best_move, last_move); |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
#endif /* _ORACLEDISPROVER_TCC */ |
| 720 |
// ;;; Local Variables: |
| 721 |
// ;;; mode:c++ |
| 722 |
// ;;; c-basic-offset:2 |
| 723 |
// ;;; End: |