checkmateIfCapture.cc
Go to the documentation of this file.
/* checkmateIfCapture.cc
*/
#include "osl/checkmate/checkmateIfCapture.h"
#include "osl/checkmate/fixedDepthSearcher.h"
#include "osl/checkmate/immediateCheckmate.h"
#include "osl/move_generator/capture_.h"
#include "osl/effect_util/neighboring8Direct.h"
struct osl::checkmate::CheckmateIfCapture::CallDefense
{
NumEffectState *state;
int depth;
bool result;
void operator()(Square last_to)
{
result = cannotCapture(*state, last_to, depth);
}
};
bool osl::checkmate::
CheckmateIfCapture::effectiveAttackCandidate0(const NumEffectState& state, Move move)
{
using namespace move_classifier;
// depth 0 専用の枝刈
const Player attacker = state.turn();
const Player defender = alt(attacker);
const Square king = state.kingSquare(defender);
PieceMask pieces = state.effectSetAt(move.to())
& state.piecesOnBoard(defender);
if (pieces.none())
return false;
if (move.to().isNeighboring8(king))
return true;
const Piece captured = state.pieceOnBoard(move.to());
if (move.isCapture()) {
if (Neighboring8Direct::hasEffect(state, captured.ptypeO(),
move.to(), king))
return true;
}
if (! move.isDrop()
&& (state.longEffectAt(move.from(), attacker).any() // todo: refinement 開き王手pinとか8近傍くらい?
|| (move.from().isNeighboring8(king)
&& state.hasEffectAt(attacker, move.from()))))
return true;
const King8Info info = state.king8Info(defender);
const CArray<Square,2> knight_position = {{
Board_Table.nextSquare(defender, king, UUR),
Board_Table.nextSquare(defender, king, UUL)
}};
if (state.inCheck()
&& (info.dropCandidate() || info.moveCandidate2()
|| /* only when has knight or knight effect */info.liberty() == 0))
return true;
if (move.isCapture()) {
if (info.dropCandidate())
return true;
if (info.liberty() == 0) {
for (int i=0; i<2; ++i) {
const Square kp = knight_position[i];
const Piece kpp = state.pieceAt(kp);
if (kpp.isEdge() || state.hasEffectNotBy(defender, captured, kp))
continue;
if (kpp.isEmpty()
&& unpromote(move.capturePtype()) == KNIGHT)
return true;
if (state.hasEffectByPiece(captured, kp)
&& (unpromote(move.capturePtype()) == KNIGHT
|| state.hasPieceOnStand<KNIGHT>(attacker)
|| state.hasEffectByPtypeStrict<KNIGHT>(attacker, kp)))
return true;
}
}
} else if (info.liberty() == 0 && state.hasPieceOnStand<KNIGHT>(attacker)) {
for (int i=0; i<2; ++i) {
const Square kp = knight_position[i];
const Piece kpp = state.pieceAt(kp);
if (! kpp.isOnBoardByOwner(defender))
continue;
if (state.hasEffectByPiece(kpp, move.to()))
return true;
}
}
// テストでは出てこないが焦点もあるか?
while (pieces.any())
{
const Piece p=state.pieceOf(pieces.takeOneBit());
if (Neighboring8Direct::hasEffectOrAdditional(state, p.ptypeO(), p.square(), king)
|| p.square().isNeighboring8(king))
continue; // i.e., need analyses
if (state.longEffectAt(p.square(), attacker).any()) // todo: refinement
continue;
if (info.liberty() == 0) {
int i=0;
for (; i<2; ++i) {
const Square kp = knight_position[i];
const Piece kpp = state.pieceAt(kp);
if (kpp.isEdge() || state.hasEffectNotBy(defender, p, kp))
continue;
if (p.square() == kp
&& state.hasPieceOnStand<KNIGHT>(attacker))
break;
if (state.countEffect(defender, kp) == 1)
if ((kpp.canMoveOn(attacker)
&& state.hasEffectByPtypeStrict<KNIGHT>(attacker, kp))
|| (kpp.isEmpty()
&& state.hasPieceOnStand<KNIGHT>(attacker)))
break;
}
if (i<2)
continue;
}
// now we have safe takeback
return false;
}
return true;
}
bool osl::checkmate::
CheckmateIfCapture::effectiveAttack(NumEffectState& state, Move move, int depth)
{
assert(move.player() == state.turn());
CallDefense defense = { &state, depth, false };
state.makeUnmakeMove(move, defense);
#ifdef OSL_DEBUG
if (defense.result && ! effectiveAttackCandidate0(state, move))
std::cerr << state << move << "\n", assert(0);
#endif
return defense.result;
}
bool osl::checkmate::
CheckmateIfCapture::cannotCapture(NumEffectState& state,
Square last_to, int depth)
{
if (state.inCheck(alt(state.turn())))
return false; // 前の手が自殺
using namespace move_generator;
using namespace move_action;
MoveVector moves; // may contain unsafe move
GenerateCapture::generate(state, last_to, moves);
if (moves.empty())
return false;
FixedDepthSearcher searcher(state);
const Square king = state.kingSquare(state.turn());
for (MoveVector::const_iterator p=moves.begin(); p!=moves.end(); ++p)
{
if (state.inCheck()) {
if (state.countEffect(alt(state.turn()), king) > 1
|| ! state.hasEffectByPiece(state.pieceOnBoard(last_to), king))
if (p->ptype() != KING)
continue;
}
const bool checkmate
= searcher.hasEscapeByMoveOfTurn(*p, depth).isCheckmateSuccess();
if (! checkmate)
return false;
}
return true;
}
/* ------------------------------------------------------------------------- */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
capture_.h
checkmateIfCapture.h
osl::CArray
Definition container.h:20
osl::FixedCapacityVector::end
iterator end()
Definition container.h:199
osl::FixedCapacityVector::empty
bool empty() const
Definition container.h:244
osl::FixedCapacityVector::begin
iterator begin()
Definition container.h:198
osl::FixedCapacityVector< Move, Move::MaxUniqMoves >::const_iterator
array_t::const_iterator const_iterator
Definition container.h:157
osl::MoveVector
Definition container.h:293
osl::Move
圧縮していない moveの表現 .
Definition basic_type.h:1052
osl::Move::player
Player player() const
Definition basic_type.h:1195
osl::Move::isDrop
bool isDrop() const
Definition basic_type.h:1150
osl::Move::capturePtype
Ptype capturePtype() const
Definition basic_type.h:1180
osl::Move::isCapture
bool isCapture() const
Definition basic_type.h:1148
osl::Move::to
const Square to() const
Definition basic_type.h:1132
osl::Move::from
const Square from() const
Definition basic_type.h:1125
osl::Neighboring8Direct::hasEffectOrAdditional
static bool hasEffectOrAdditional(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
ptypeo の駒がfromからtargetの8近傍に直接の利きを持つか そのような駒への追加/影利きになっている
Definition neighboring8Direct.h:118
osl::Neighboring8Direct::hasEffect
static bool hasEffect(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
ptypeo の駒がfromからtargetの8近傍に直接の利きを持つか
Definition neighboring8Direct.h:108
osl::NumEffectState
利きを持つ局面
Definition numEffectState.h:34
osl::NumEffectState::effectSetAt
const NumBitmapEffect effectSetAt(Square sq) const
Definition numEffectState.h:258
osl::NumEffectState::king8Info
const checkmate::King8Info king8Info(Player king) const
Definition numEffectState.cc:44
osl::NumEffectState::hasEffectNotBy
bool hasEffectNotBy(Player player, Piece piece, Square target) const
対象とするマスにあるプレイヤーの(ただしある駒以外)利きがあるかどうか.
Definition numEffectState.h:409
osl::NumEffectState::countEffect
int countEffect(Player player, Square target) const
利きの数を数える.
Definition numEffectState.h:266
osl::NumEffectState::hasEffectByPiece
bool hasEffectByPiece(Piece attack, Square target) const
駒attack が target に利きを持つか (旧hasEffectToと統合)
Definition numEffectState.h:450
osl::NumEffectState::hasEffectAt
bool hasEffectAt(Square target) const
対象とするマスにあるプレイヤーの利きがあるかどうか.
Definition numEffectState.h:324
osl::NumEffectState::inCheck
bool inCheck(Player P) const
Pの玉が王手状態
Definition numEffectState.h:88
osl::NumEffectState::longEffectAt
const mask_t longEffectAt(Square target) const
Definition numEffectState.h:298
osl::NumEffectState::makeUnmakeMove
void makeUnmakeMove(Move move, Function &f)
Definition numEffectState.h:683
osl::NumEffectState::piecesOnBoard
const PieceMask & piecesOnBoard(Player p) const
Definition numEffectState.h:63
osl::NumEffectState::hasEffectByPtypeStrict
bool hasEffectByPtypeStrict(Player attack, Square target) const
target に ptype の利きがあるか?
Definition numEffectState.h:363
osl::PieceMask
駒番号のビットセット.
Definition pieceMask.h:21
osl::PieceMask::any
bool any() const
Definition pieceMask.h:57
osl::Piece
駒.
Definition basic_type.h:788
osl::Piece::ptypeO
PtypeO ptypeO() const
Definition basic_type.h:824
osl::Piece::square
const Square square() const
Definition basic_type.h:832
osl::Piece::isEmpty
bool isEmpty() const
Definition basic_type.h:913
osl::Piece::isEdge
bool isEdge() const
Definition basic_type.h:919
osl::Piece::canMoveOn
bool canMoveOn() const
Player Pの駒が，thisの上に移動できるか?
Definition basic_type.h:980
osl::Piece::isOnBoardByOwner
bool isOnBoardByOwner() const
piece がプレイヤーPの持ち物でかつボード上にある駒の場合は true.
Definition basic_type.h:852
osl::SimpleState::pieceOnBoard
const Piece pieceOnBoard(Square sq) const
Definition simpleState.h:170
osl::SimpleState::hasPieceOnStand
bool hasPieceOnStand(Player player, Ptype ptype) const
Definition simpleState.h:191
osl::SimpleState::turn
Player turn() const
Definition simpleState.h:220
osl::SimpleState::pieceOf
const Piece pieceOf(int num) const
Definition simpleState.h:76
osl::SimpleState::kingSquare
Square kingSquare() const
Definition simpleState.h:94
osl::SimpleState::pieceAt
const Piece pieceAt(Square sq) const
Definition simpleState.h:167
osl::Square
Definition basic_type.h:532
osl::Square::isNeighboring8
bool isNeighboring8(Square to) const
Definition basic_type.cc:202
osl::checkmate::FixedDepthSearcher
深さ固定で，その深さまで depth first searchで読む詰将棋.
Definition fixedDepthSearcher.h:41
osl::checkmate::FixedDepthSearcher::hasEscapeByMoveOfTurn
const ProofDisproof hasEscapeByMoveOfTurn(Move next_move, int depth)
Definition fixedDepthSearcher.cc:30
osl::checkmate::King8Info
敵玉の8近傍の状態を表す.
Definition king8Info.h:29
osl::checkmate::King8Info::liberty
unsigned int liberty() const
8-15 bit 目を 0-7bitにshiftして返す
Definition king8Info.h:54
osl::checkmate::King8Info::moveCandidate2
unsigned int moveCandidate2() const
24-31 bit 目を 0-7bitにshiftして返す
Definition king8Info.h:69
osl::checkmate::King8Info::dropCandidate
unsigned int dropCandidate() const
0-7 bit 目を返す
Definition king8Info.h:49
osl::checkmate::ProofDisproof::isCheckmateSuccess
bool isCheckmateSuccess() const
Definition proofDisproof.h:86
osl::container::PieceMask64::none
bool none() const
Definition pieceMask64.h:57
osl::container::PieceMask64::takeOneBit
int takeOneBit()
Definition pieceMask64.h:82
osl::misc::GeneralMask::any
bool any() const
Definition mask.h:298
fixedDepthSearcher.h
immediateCheckmate.h
osl::checkmate
Definition king8Info.h:12
osl::move_action
Definition move_action.h:11
osl::move_classifier
Definition check_.h:10
osl::move_generator
Definition addEffectWithEffect.cc:10
osl::KING
@ KING
Definition basic_type.h:93
osl::KNIGHT
@ KNIGHT
Definition basic_type.h:97
osl::unpromote
Ptype unpromote(Ptype ptype)
ptypeがpromote後の型の時に，promote前の型を返す． promoteしていない型の時はそのまま返す
Definition basic_type.h:157
osl::Board_Table
const BoardTable Board_Table
Definition tables.cc:95
osl::UUR
@ UUR
Definition basic_type.h:323
osl::UUL
@ UUL
Definition basic_type.h:322
osl::Player
Player
Definition basic_type.h:8
osl::alt
constexpr Player alt(Player player)
Definition basic_type.h:13
osl::captured
PtypeO captured(PtypeO ptypeO)
unpromoteすると共に，ownerを反転する．
Definition basic_type.h:264
neighboring8Direct.h
osl::GenerateCapture::generate
static void generate(Player p, const NumEffectState &state, Square target, Action &action)
Definition capture_.h:49
osl::checkmate::CheckmateIfCapture::CallDefense
Definition checkmateIfCapture.cc:10
osl::checkmate::CheckmateIfCapture::CallDefense::result
bool result
Definition checkmateIfCapture.cc:13
osl::checkmate::CheckmateIfCapture::CallDefense::state
NumEffectState * state
Definition checkmateIfCapture.cc:11
osl::checkmate::CheckmateIfCapture::CallDefense::depth
int depth
Definition checkmateIfCapture.cc:12
osl::checkmate::CheckmateIfCapture::CallDefense::operator()
void operator()(Square last_to)
Definition checkmateIfCapture.cc:14
osl::checkmate::CheckmateIfCapture::cannotCapture
static bool cannotCapture(NumEffectState &state, Square last_to, int depth)
手番の側がSquare の駒を取っても詰みがないか．
Definition checkmateIfCapture.cc:133
osl::checkmate::CheckmateIfCapture::effectiveAttackCandidate0
static bool effectiveAttackCandidate0(const NumEffectState &state, Move move)
depth==0でeffectiveAttackになる可能性がなければfalse
Definition checkmateIfCapture.cc:21
osl::checkmate::CheckmateIfCapture::effectiveAttack
static bool effectiveAttack(NumEffectState &state, Move move, int depth)
move を指した後，alt(move.player())が取ると詰みかどうか.
Definition checkmateIfCapture.cc:120