/* quiescenceRecord.h */ #ifndef _QUIESCENCERECORD_H #define _QUIESCENCERECORD_H #include "osl/search/dualThreatmateState.h" #ifdef OSL_SMP # include "osl/misc/lightMutex.h" #endif #include #include namespace osl { namespace search { struct QSearchTraits { enum { /** 通常探索の最大 */ MaxDepth = 8, /** 即詰の深さ */ CheckmateSpecialDepth = 127, /** 駒損ループの深さ */ HistorySpecialDepth = 126, }; enum { FirstThreat = 6, SecondThreat = 2 }; enum MoveType { UNKNOWN, KING_ESCAPE, CAPTURE, PROMOTE, CHECK, ESCAPE, ATTACK, OTHER, }; }; /** * QuiescenceSearch でパスした場合の相手の有力な指手 */ struct QuiescenceThreat { int value; Move move; explicit QuiescenceThreat(int v=0, Move m=Move::INVALID()) : value(v), move(m) { } }; struct BestMoves : public CArray { BestMoves() { // automaticallyfilled by invalid data by constructor } size_t capacity() const { return size(); } size_t sizeFilled() const { for (size_t i=0; i copy = *this; clear(); size_t size = 0; if (copy[0].isNormal()) operator[](size++) = copy[0]; for (size_t i=0; i const Square8 sendOffSquare(const NumEffectState& state) const { #ifdef OSL_SMP SCOPED_LOCK_CHAR(lk,mutex); #endif assert(Turn == state.turn()); Square8 ret; const Square king_position = state.kingSquare(alt(Turn)); if (threatmate.sendoffs == SendOffSquare::invalidData()) threatmate.sendoffs = SendOffSquare::find(state, king_position, ret); else SendOffSquare::unpack(threatmate.sendoffs, king_position, ret); return ret; } const Square8 sendOffSquare(Player turn, const NumEffectState& state) const { if (turn == BLACK) return sendOffSquare(state); else return sendOffSquare(state); } /** * @param max このrecordで使って良いノード数 * @return 詰将棋に使えるノード数を返す */ int checkmateNodesLeft(int max) { #ifdef OSL_SMP SCOPED_LOCK_CHAR(lk,mutex); #endif if (max > checkmate_nodes) { const int left = max - checkmate_nodes; checkmate_nodes = max; return left; } return 0; } /** * @param max このrecordで使って良いノード数 * @return 詰めろ確認の詰将棋に使えるノード数を返す */ int threatmateNodesLeft(int max) { #ifdef OSL_SMP SCOPED_LOCK_CHAR(lk,mutex); #endif if (max > threatmate_nodes) { const int left = max - threatmate_nodes; threatmate_nodes = max; return left; } return 0; } /** 今までに詰将棋で探したノード数 */ int checkmateNodes() const { return checkmate_nodes; } int threatmateNodes() const { return threatmate_nodes; } void clear() { #ifdef OSL_SMP SCOPED_LOCK_CHAR(lk,mutex); #endif best_moves.clear(); upper_depth = lower_depth = static_value_depth = InitialDepth; } void setStaticValue(StaticValueType type, int value, int depth, const QuiescenceThreat& t1=QuiescenceThreat(), const QuiescenceThreat& t2=QuiescenceThreat()) { #ifdef OSL_SMP SCOPED_LOCK_CHAR(lk,mutex); #endif assert((depth <= QSearchTraits::MaxDepth) || (depth == QSearchTraits::CheckmateSpecialDepth)); assert(value % 2 == 0); static_value = value; threat1 = t1; threat2 = t2; static_value_depth = depth; threatmate.flags.static_value_type = type; } public: void setLowerBound(int depth, int bound, Move best_move) { #ifdef OSL_SMP SCOPED_LOCK_CHAR(lk,mutex); #endif assert((depth <= QSearchTraits::MaxDepth) || (depth == QSearchTraits::CheckmateSpecialDepth)); if (depth >= lower_depth) { best_moves.add(best_move); lower_bound = bound; lower_depth = depth; } } void setUpperBound(int depth, int bound) { #ifdef OSL_SMP SCOPED_LOCK_CHAR(lk,mutex); #endif assert((depth <= QSearchTraits::MaxDepth) || (depth == QSearchTraits::CheckmateSpecialDepth)); if (depth >= upper_depth) { upper_bound = bound; upper_depth = depth; } } void setHistoryValue(int value) { lower_bound = upper_bound = value; lower_depth = upper_depth = QSearchTraits::HistorySpecialDepth; } void setHistoryValue(Move best_move, int value) { #ifdef OSL_SMP SCOPED_LOCK_CHAR(lk,mutex); #endif best_moves.add(best_move); setHistoryValue(value); } public: void addKillerMoves(const MoveVector& new_moves) { #ifdef OSL_SMP SCOPED_LOCK_CHAR(lk,mutex); #endif best_moves.addSecondary(new_moves); } StaticValueType staticValueType() const { return static_cast(threatmate.flags.static_value_type); } bool hasStaticValue() const { return staticValueType() != UNKNOWN; } bool hasStaticValue(int& value, int& depth, StaticValueType& type) const { type = staticValueType(); if (type == UNKNOWN) return false; #ifdef OSL_SMP SCOPED_LOCK_CHAR(lk,mutex); #endif type = staticValueType(); value = static_value; depth = static_value_depth; return type != UNKNOWN; } int staticValue() const { assert(hasStaticValue()); return static_value; } int staticValueDepth() const { return static_value_depth; } int upperDepth() const { return upper_depth; } int lowerDepth() const { return lower_depth; } int upperBound() const { return upper_bound; } int lowerBound() const { return lower_bound; } const Move bestMove() const { return best_moves[0]; } int movesEmpty() const { return ! best_moves[1].isNormal(); } int movesSizeLessThan(size_t n) const { return best_moves.capacity() < n || ! best_moves[n-1].isNormal(); } int moves_size() const { return std::max(0, (int)best_moves.sizeFilled()-1); } void loadMoves(MoveVector& dst) const{ #ifdef OSL_SMP SCOPED_LOCK_CHAR(lk,mutex); #endif dst.clear(); for (size_t i=1; i