| 1 |
/* checkMove.h |
| 2 |
*/ |
| 3 |
#ifndef _CHECKMOVE_H |
| 4 |
#define _CHECKMOVE_H |
| 5 |
|
| 6 |
#include "osl/checkmate/proofDisproof.h" |
| 7 |
#include "osl/move.h" |
| 8 |
#include "osl/pathEncoding.h" |
| 9 |
#include <iosfwd> |
| 10 |
namespace osl |
| 11 |
{ |
| 12 |
namespace checkmate |
| 13 |
{ |
| 14 |
class CheckHashRecord; |
| 15 |
class TwinEntry; |
| 16 |
class TwinTable; |
| 17 |
class MoveFlags |
| 18 |
{ |
| 19 |
unsigned char flags; |
| 20 |
public: |
| 21 |
enum Constant { |
| 22 |
/** 手番に*否定的*な結果が判明した指手 */ |
| 23 |
Solved=1, |
| 24 |
/** 歩以外の中合いリスト. 守備側で詰があった時だけ使う, */ |
| 25 |
BlockingBySacrifice=2, |
| 26 |
/** 歩飛角の不成の指手. 攻撃側で打歩詰があったときだけ使う. */ |
| 27 |
NoPromote=4, |
| 28 |
/** 捨駒の攻撃 最初は読まない */ |
| 29 |
SacrificeAttack=8, |
| 30 |
/** 深さの浅い子供へのリンク. |
| 31 |
* ループなどのもと.他の指手が全て詰/不詰の時だけ使用 */ |
| 32 |
Upward=16, |
| 33 |
/** この手で一手詰. この先にrecordがあるとは限らない */ |
| 34 |
ImmediateCheckmate=32, |
| 35 |
}; |
| 36 |
MoveFlags() : flags(0) {} |
| 37 |
MoveFlags& operator=(Constant flag) |
| 38 |
{ |
| 39 |
flags = static_cast<unsigned char>(flag); |
| 40 |
return *this; |
| 41 |
} |
| 42 |
void set(Constant flag) |
| 43 |
{ |
| 44 |
assert((flags & flag) == 0); |
| 45 |
flags |= flag; |
| 46 |
} |
| 47 |
void unset(Constant flag) |
| 48 |
{ |
| 49 |
assert(flags & flag); |
| 50 |
flags ^= flag; |
| 51 |
} |
| 52 |
int isSet(Constant flag) const |
| 53 |
{ |
| 54 |
return flags & flag; |
| 55 |
} |
| 56 |
int getFlags() const { return flags; } |
| 57 |
}; |
| 58 |
class MoveFilter |
| 59 |
{ |
| 60 |
unsigned char mask; |
| 61 |
public: |
| 62 |
MoveFilter() : mask(31) |
| 63 |
{ |
| 64 |
} |
| 65 |
bool isTarget(MoveFlags flags) const |
| 66 |
{ |
| 67 |
return (mask & flags.getFlags()) == 0; |
| 68 |
} |
| 69 |
void addTarget(MoveFlags::Constant flag) |
| 70 |
{ |
| 71 |
assert(! isTarget(flag)); |
| 72 |
mask ^= flag; |
| 73 |
} |
| 74 |
bool isTarget(MoveFlags::Constant flag) const |
| 75 |
{ |
| 76 |
return (mask & flag) == 0; |
| 77 |
} |
| 78 |
int getMask() const { return mask; } |
| 79 |
}; |
| 80 |
std::ostream& operator<<(std::ostream& os, MoveFlags); |
| 81 |
std::ostream& operator<<(std::ostream& os, MoveFilter); |
| 82 |
/** |
| 83 |
* 詰将棋で使う指手 |
| 84 |
*/ |
| 85 |
struct CheckMoveCore |
| 86 |
{ |
| 87 |
/** move を指した後のrecord */ |
| 88 |
CheckHashRecord *record; // 8 byte |
| 89 |
Move move; // 4 byte |
| 90 |
explicit CheckMoveCore(Move m=Move::INVALID(), CheckHashRecord *r=0) |
| 91 |
: record(r), move(m) |
| 92 |
{ |
| 93 |
} |
| 94 |
}; |
| 95 |
/** |
| 96 |
* 詰将棋で使う指手 |
| 97 |
*/ |
| 98 |
struct CheckMove : public CheckMoveCore |
| 99 |
{ |
| 100 |
MoveFlags flags; // 1 byte |
| 101 |
/** cost */ |
| 102 |
signed char cost_proof, cost_disproof; // 2 byte |
| 103 |
/** H. record == 0 の時だけ使う */ |
| 104 |
unsigned short h_proof, h_disproof; // 4 byte |
| 105 |
explicit CheckMove(Move m=Move::INVALID(), CheckHashRecord *r=0) |
| 106 |
: CheckMoveCore(m, r), |
| 107 |
cost_proof(0), cost_disproof(0), h_proof(1), h_disproof(1) |
| 108 |
{ |
| 109 |
} |
| 110 |
void addCost(unsigned& proof, unsigned int& disproof) const |
| 111 |
{ |
| 112 |
assert(proof && disproof); |
| 113 |
assert(cost_proof >= 0); |
| 114 |
assert(cost_disproof >= 0); |
| 115 |
proof += cost_proof; |
| 116 |
disproof += cost_disproof; |
| 117 |
assert(proof < ProofDisproof::PROOF_LIMIT); |
| 118 |
assert(disproof < ProofDisproof::DISPROOF_LIMIT); |
| 119 |
} |
| 120 |
/** |
| 121 |
* path の後に this を指した後に LoopDetection かどうか |
| 122 |
* CheckHashRecord.h で定義 |
| 123 |
* @return 見つからなければ0 |
| 124 |
*/ |
| 125 |
inline const TwinEntry* findLoop(const PathEncoding& path, |
| 126 |
const TwinTable& table) const; |
| 127 |
inline const TwinEntry* findLoopInList(const PathEncoding& path) const; |
| 128 |
}; |
| 129 |
inline bool operator==(const CheckMove& l, const CheckMove& r) |
| 130 |
{ |
| 131 |
if (l.move == r.move) |
| 132 |
{ |
| 133 |
assert(l.record == r.record); |
| 134 |
return true; |
| 135 |
} |
| 136 |
assert(l.record != r.record || (l.record == 0 && r.record == 0)); |
| 137 |
return false; |
| 138 |
} |
| 139 |
} // namespace checkmate |
| 140 |
} // namespace osl |
| 141 |
|
| 142 |
|
| 143 |
#endif /* _CHECKMOVE_H */ |
| 144 |
// ;;; Local Variables: |
| 145 |
// ;;; mode:c++ |
| 146 |
// ;;; c-basic-offset:2 |
| 147 |
// ;;; End: |