| 1 |
/* checkTableUtil.h |
| 2 |
*/ |
| 3 |
#ifndef _CHECKTABLEUTIL_H |
| 4 |
#define _CHECKTABLEUTIL_H |
| 5 |
|
| 6 |
#include "checkHashRecord.h" |
| 7 |
#include "osl/hash/hashKey.h" |
| 8 |
#include <cassert> |
| 9 |
namespace osl |
| 10 |
{ |
| 11 |
namespace hash |
| 12 |
{ |
| 13 |
class HashKey; |
| 14 |
} |
| 15 |
namespace checkmate |
| 16 |
{ |
| 17 |
struct CheckTableUtil |
| 18 |
{ |
| 19 |
template <class Table> |
| 20 |
static |
| 21 |
void allocate(CheckHashRecord*& record, Table& table, |
| 22 |
const HashKey& key, PieceStand white_stand, |
| 23 |
const PathEncoding& path, |
| 24 |
CheckHashRecord *parent) |
| 25 |
{ |
| 26 |
check_assert(record == 0); |
| 27 |
record = table.allocate(key, white_stand, path); |
| 28 |
if (! record->parent) |
| 29 |
{ |
| 30 |
record->parent = parent; |
| 31 |
record->distance = parent->distance+1; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* white_stand を作ってからrecordを確保する |
| 37 |
*/ |
| 38 |
template <class Table> |
| 39 |
static |
| 40 |
void allocate(Move last_move, CheckHashRecord*& record, Table& table, |
| 41 |
const HashKey& key, const PathEncoding& path, |
| 42 |
CheckHashRecord *parent) |
| 43 |
{ |
| 44 |
assert(parent); |
| 45 |
const PieceStand white_stand |
| 46 |
= parent->stand(WHITE).nextStand(WHITE, last_move); |
| 47 |
allocate(record, table, key, white_stand, path, parent); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* keyBefore と pathBefore に nextMove を指した後のものにしてから |
| 52 |
* record を確保する |
| 53 |
*/ |
| 54 |
template <class Table> |
| 55 |
static |
| 56 |
void allocateNext(Move next_move, |
| 57 |
CheckHashRecord*& record, Table& table, |
| 58 |
const HashKey& key_before, |
| 59 |
const PathEncoding& path_before, |
| 60 |
CheckHashRecord *parent) |
| 61 |
{ |
| 62 |
assert(parent); |
| 63 |
const PieceStand white_stand |
| 64 |
= parent->stand(WHITE).nextStand(WHITE, next_move); |
| 65 |
return allocate(record, table, key_before.newHashWithMove(next_move), |
| 66 |
white_stand, |
| 67 |
PathEncoding(path_before, next_move), parent); |
| 68 |
} |
| 69 |
/** defense node で move を指すと check_move でfixed_searher で詰みと記録 */ |
| 70 |
template <Player P, class Table> |
| 71 |
static void registerImmediateCheckmateInDefense |
| 72 |
(const HashKey& key, const PathEncoding& path, |
| 73 |
CheckHashRecord *record, CheckMove& move, |
| 74 |
ProofDisproof pdp, Move check_move, PieceStand proof_pieces, |
| 75 |
Table& table); |
| 76 |
}; |
| 77 |
} // namespace checkmate |
| 78 |
} // namespace osl |
| 79 |
|
| 80 |
template <osl::Player P, class Table> |
| 81 |
void osl::checkmate::CheckTableUtil:: |
| 82 |
registerImmediateCheckmateInDefense(const HashKey& key, const PathEncoding& path, |
| 83 |
CheckHashRecord *record, CheckMove& move, |
| 84 |
ProofDisproof pdp, |
| 85 |
Move check_move, PieceStand proof_pieces, |
| 86 |
Table& table) |
| 87 |
{ |
| 88 |
assert(pdp.isCheckmateSuccess()); |
| 89 |
const HashKey new_key = key.newHashWithMove(move.move); |
| 90 |
const PathEncoding new_path(path, move.move); |
| 91 |
if (! move.record) |
| 92 |
CheckTableUtil::allocate |
| 93 |
(move.move, move.record, table, new_key, new_path, record); |
| 94 |
if (! move.record->proofDisproof().isCheckmateSuccess()) |
| 95 |
{ |
| 96 |
if (move.record->moves.empty()) |
| 97 |
{ |
| 98 |
CheckMove best_move(check_move); |
| 99 |
best_move.flags.set(MoveFlags::ImmediateCheckmate); |
| 100 |
move.record->moves.setOne(best_move, table.listProvider()); |
| 101 |
move.record->bestMove = &*(move.record->moves.begin()); |
| 102 |
} |
| 103 |
else |
| 104 |
{ |
| 105 |
CheckMove *attack = move.record->moves.find(check_move); |
| 106 |
assert(attack); |
| 107 |
move.record->bestMove = attack; |
| 108 |
move.record->bestMove->flags.set(MoveFlags::ImmediateCheckmate); |
| 109 |
move.record->bestMove->record = 0; |
| 110 |
} |
| 111 |
move.record->setProofPieces(proof_pieces); |
| 112 |
move.record->propagateCheckmate<P>(ProofDisproof::Checkmate()); |
| 113 |
} |
| 114 |
record->addToSolvedInDefense(move, pdp); |
| 115 |
} |
| 116 |
|
| 117 |
#endif /* _CHECKTABLEUTIL_H */ |
| 118 |
// ;;; Local Variables: |
| 119 |
// ;;; mode:c++ |
| 120 |
// ;;; c-basic-offset:2 |
| 121 |
// ;;; End: |