| 1 |
/* disproofOracle.h |
| 2 |
*/ |
| 3 |
#ifndef _DISPROOFORACLE_H |
| 4 |
#define _DISPROOFORACLE_H |
| 5 |
|
| 6 |
#include "checkHashRecord.h" |
| 7 |
#include "checkAssert.h" |
| 8 |
#include "osl/pathEncoding.h" |
| 9 |
|
| 10 |
#include <cstddef> |
| 11 |
namespace osl |
| 12 |
{ |
| 13 |
namespace checkmate |
| 14 |
{ |
| 15 |
template <Player Attacker> |
| 16 |
struct DisproofOracleAttack; |
| 17 |
|
| 18 |
/** |
| 19 |
* 詰まない類似局面 (受方). |
| 20 |
* OracleDisrover が使用 |
| 21 |
*/ |
| 22 |
template <Player Attacker> |
| 23 |
struct DisproofOracleDefense |
| 24 |
{ |
| 25 |
const CheckHashRecord *guide; |
| 26 |
const CheckHashRecord *next_guide; |
| 27 |
PathEncoding path; |
| 28 |
Move best_move; |
| 29 |
DisproofOracleDefense(CheckHashRecord *g, const PathEncoding& p) |
| 30 |
: guide(g), next_guide(0), path(p), best_move(Move::INVALID()) |
| 31 |
{ |
| 32 |
if (guide) |
| 33 |
{ |
| 34 |
if (guide->hasBestMove() |
| 35 |
&& guide->getBestMove()->record |
| 36 |
&& guide->getBestMove()->record->proofDisproof().isCheckmateFail()) |
| 37 |
{ |
| 38 |
next_guide = guide->getBestMove()->record; |
| 39 |
best_move = guide->getBestMove()->move; |
| 40 |
if (! guide->proofDisproof().isCheckmateFail()) |
| 41 |
{ |
| 42 |
g->setDisproofPiecesDefense(alt(Attacker)); |
| 43 |
g->propagateNoCheckmate<Attacker>(g->getBestMove()->record->proofDisproof()); |
| 44 |
} |
| 45 |
} |
| 46 |
else |
| 47 |
{ |
| 48 |
if (const TwinEntry *loop = guide->findLoopInList(path)) // LoopDetection |
| 49 |
{ |
| 50 |
next_guide = loop->move.record; |
| 51 |
best_move = loop->move.move; |
| 52 |
} |
| 53 |
else |
| 54 |
{ |
| 55 |
setValidTwins(); |
| 56 |
} |
| 57 |
} |
| 58 |
check_assert((guide->proofDisproof().isCheckmateFail() |
| 59 |
&& guide->hasBestMove() |
| 60 |
&& (guide->getBestMove()->move.player()==alt(Attacker))) |
| 61 |
|| (guide->findLoopInList(path)) |
| 62 |
|| (guide->proofDisproof() == ProofDisproof::NoEscape()) |
| 63 |
|| (guide->dump(), 0)); |
| 64 |
} |
| 65 |
} |
| 66 |
explicit DisproofOracleDefense(CheckHashRecord *g) |
| 67 |
: guide(g), next_guide(0), path(alt(Attacker)), best_move(Move::INVALID()) |
| 68 |
{ |
| 69 |
if (guide) |
| 70 |
{ |
| 71 |
if (guide->hasBestMove() && guide->getBestMove()->record) |
| 72 |
{ |
| 73 |
if (guide->getBestMove()->record->proofDisproof().isCheckmateFail()) |
| 74 |
{ |
| 75 |
next_guide = guide->getBestMove()->record; |
| 76 |
best_move = guide->getBestMove()->move; |
| 77 |
if (! guide->proofDisproof().isCheckmateFail()) |
| 78 |
{ |
| 79 |
g->setDisproofPiecesDefense(alt(Attacker)); |
| 80 |
g->propagateNoCheckmate<Attacker>(g->getBestMove()->record->proofDisproof()); |
| 81 |
} |
| 82 |
} |
| 83 |
else if (! guide->bestMove->record->twins.empty()) |
| 84 |
{ |
| 85 |
next_guide = guide->getBestMove()->record; |
| 86 |
best_move = guide->getBestMove()->move; |
| 87 |
path = guide->bestMove->record->twins.begin()->path; |
| 88 |
path.popMove(guide->bestMove->move); |
| 89 |
} |
| 90 |
} |
| 91 |
if ((! next_guide) && (! guide->twins.empty())) |
| 92 |
{ |
| 93 |
setValidTwins(); |
| 94 |
} |
| 95 |
check_assert((! guide->hasBestMove()) |
| 96 |
|| guide->getBestMove()->move.player()==alt(Attacker)); |
| 97 |
check_assert((guide->hasBestMove() |
| 98 |
&& (guide->getBestMove()->record->proofDisproof().isCheckmateFail() |
| 99 |
|| (guide->getBestMove()->findLoopInList(path)))) |
| 100 |
|| (guide->findLoopInList(path)) |
| 101 |
|| (guide->proofDisproof() == ProofDisproof::NoEscape()) |
| 102 |
|| (guide->dump(), 0)); |
| 103 |
} |
| 104 |
} |
| 105 |
private: |
| 106 |
void setValidTwins() |
| 107 |
{ |
| 108 |
for (TwinList::const_iterator p=guide->twins.begin(); |
| 109 |
p!=guide->twins.end(); ++p) |
| 110 |
{ |
| 111 |
if (best_move.isInvalid()) |
| 112 |
{ |
| 113 |
assert(! best_move.isPass()); |
| 114 |
path = p->path; |
| 115 |
best_move = p->move.move; |
| 116 |
next_guide = p->move.record; |
| 117 |
if (next_guide) |
| 118 |
break; |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
public: |
| 123 |
Move oracle() |
| 124 |
{ |
| 125 |
check_assert(guide && next_guide); |
| 126 |
return best_move; |
| 127 |
} |
| 128 |
inline DisproofOracleAttack<Attacker> expandOracle() const; |
| 129 |
bool isValid() const { return guide; } |
| 130 |
}; |
| 131 |
|
| 132 |
/** |
| 133 |
* 詰まない類似局面 (攻方). |
| 134 |
* OracleDisprover が使用 |
| 135 |
*/ |
| 136 |
template <Player Attacker> |
| 137 |
struct DisproofOracleAttack |
| 138 |
{ |
| 139 |
const CheckHashRecord *guide; |
| 140 |
PathEncoding path; |
| 141 |
DisproofOracleAttack(const CheckHashRecord *g, const PathEncoding& p) |
| 142 |
: guide(g), path(p) |
| 143 |
{ |
| 144 |
if (guide && (! guide->proofDisproof().isCheckmateFail()) |
| 145 |
&& (! guide->findLoopInList(path))) |
| 146 |
{ |
| 147 |
if (! guide->twins.empty()) |
| 148 |
path = guide->twins.begin()->path; |
| 149 |
else |
| 150 |
guide = 0; |
| 151 |
// TODO: ここに来るのは LoopDetection に失敗した時だけなので |
| 152 |
// 事前に効率的に判定したい |
| 153 |
} |
| 154 |
check_assert((!guide) |
| 155 |
|| guide->proofDisproof().isCheckmateFail() |
| 156 |
|| guide->findLoopInList(path) |
| 157 |
|| (g->dump(), 0)); |
| 158 |
} |
| 159 |
DisproofOracleAttack(const CheckHashRecord *g) |
| 160 |
: guide(g), path(Attacker) |
| 161 |
{ |
| 162 |
if (guide && (! guide->proofDisproof().isCheckmateFail()) |
| 163 |
&& (! guide->twins.empty())) |
| 164 |
{ |
| 165 |
path = guide->twins.begin()->path; |
| 166 |
} |
| 167 |
check_assert((!guide) |
| 168 |
|| guide->proofDisproof().isCheckmateFail() |
| 169 |
|| guide->findLoopInList(path) |
| 170 |
|| (g->dump(), 0)); |
| 171 |
} |
| 172 |
bool invalidNextOracle(const CheckMove& move) const |
| 173 |
{ |
| 174 |
if (! move.record) |
| 175 |
return true; |
| 176 |
const ProofDisproof& pdp = move.record->proofDisproof(); |
| 177 |
return (! pdp.isCheckmateFail()) |
| 178 |
&& (! (guide->bestResultInSolved == ProofDisproof::PawnCheckmate() |
| 179 |
&& (pdp == ProofDisproof::NoEscape()))) |
| 180 |
&& move.record->twins.empty(); |
| 181 |
} |
| 182 |
DisproofOracleDefense<Attacker> makeOracle(const CheckMove& move) const |
| 183 |
{ |
| 184 |
check_assert(move.record); |
| 185 |
#ifndef NDEBUG |
| 186 |
const ProofDisproof& pdp = move.record->proofDisproof(); |
| 187 |
#endif |
| 188 |
const PathEncoding new_path(path, move.move); |
| 189 |
check_assert((! move.record->twins.empty()) |
| 190 |
|| (pdp.isCheckmateFail() && move.record->hasBestMove()) |
| 191 |
|| (move.record->proofDisproof() == ProofDisproof::NoEscape()) |
| 192 |
|| (guide->dump(), 0)); |
| 193 |
return DisproofOracleDefense<Attacker>(move.record, new_path); |
| 194 |
} |
| 195 |
|
| 196 |
/** 攻方が attack をしたときのoracle */ |
| 197 |
DisproofOracleDefense<Attacker> expandOracle(Move attack) const |
| 198 |
{ |
| 199 |
check_assert(guide); |
| 200 |
check_assert(guide->proofDisproof().isCheckmateFail() |
| 201 |
|| guide->findLoopInList(path)); |
| 202 |
check_assert(attack.player() == Attacker); |
| 203 |
const CheckMoveList& moves |
| 204 |
= (guide->finalByDominance() |
| 205 |
? guide->finalByDominance()->moves |
| 206 |
: guide->moves); |
| 207 |
for (CheckMoveList::const_iterator p=moves.begin(); |
| 208 |
p!=moves.end(); ++p) |
| 209 |
{ |
| 210 |
if (p->move == attack) |
| 211 |
{ |
| 212 |
if (invalidNextOracle(*p)) |
| 213 |
goto not_found; |
| 214 |
return makeOracle(*p); |
| 215 |
} |
| 216 |
} |
| 217 |
{ |
| 218 |
// 似た手を探す |
| 219 |
const Square from = attack.from(); |
| 220 |
const Square to = attack.to(); |
| 221 |
const Ptype ptype = attack.ptype(); |
| 222 |
if (from.isPieceStand()) |
| 223 |
{ |
| 224 |
// 打った手 |
| 225 |
for (CheckMoveList::const_iterator p=moves.begin(); |
| 226 |
p!=moves.end(); ++p) |
| 227 |
{ |
| 228 |
if (! p->move.isDrop()) |
| 229 |
continue; |
| 230 |
if (p->move.to() == to) // ptype が違う drop |
| 231 |
{ |
| 232 |
if (! invalidNextOracle(*p)) |
| 233 |
return makeOracle(*p); |
| 234 |
} |
| 235 |
else if (p->move.ptype() == ptype) // ptype が同じで,場所が違うdrop |
| 236 |
{ |
| 237 |
// 大駒を遠くから打ったような場合 |
| 238 |
// TODO: 方向が同じことを確認,桂馬も含む? |
| 239 |
if (! invalidNextOracle(*p)) |
| 240 |
return makeOracle(*p); |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
else |
| 245 |
{ |
| 246 |
// 動いた手 |
| 247 |
const CheckMove *ignorePtype = 0; |
| 248 |
for (CheckMoveList::const_iterator p=moves.begin(); |
| 249 |
p!=moves.end(); ++p) |
| 250 |
{ |
| 251 |
if ((p->move.to() != to) || invalidNextOracle(*p)) |
| 252 |
continue; |
| 253 |
if (unpromote(p->move.ptype()) == unpromote(ptype)) |
| 254 |
return makeOracle(*p); |
| 255 |
ignorePtype = &*p; // 最後の手段 |
| 256 |
} |
| 257 |
if (ignorePtype) |
| 258 |
return makeOracle(*ignorePtype); |
| 259 |
} |
| 260 |
} |
| 261 |
not_found: |
| 262 |
return DisproofOracleDefense<Attacker>(0,PathEncoding()); |
| 263 |
} |
| 264 |
bool isValid() const { return guide; } |
| 265 |
}; |
| 266 |
|
| 267 |
template <Player Attacker> |
| 268 |
inline DisproofOracleAttack<Attacker> |
| 269 |
DisproofOracleDefense<Attacker>::expandOracle() const |
| 270 |
{ |
| 271 |
PathEncoding new_path = path; |
| 272 |
new_path.pushMove(best_move); |
| 273 |
return DisproofOracleAttack<Attacker>(next_guide, new_path); |
| 274 |
} |
| 275 |
} // namespace checkmate |
| 276 |
} // namespace osl |
| 277 |
|
| 278 |
#endif /* _DISPROOFORACLE_H */ |
| 279 |
// ;;; Local Variables: |
| 280 |
// ;;; mode:c++ |
| 281 |
// ;;; c-basic-offset:2 |
| 282 |
// ;;; End: |