checkmate.cc
Go to the documentation of this file.
/* checkmate.cc
*/
#include "osl/rating/feature/checkmate.h"
#include "osl/bits/king8Info.h"
#include "osl/effect_util/neighboring8Direct.h"
struct osl::rating::Threatmate::Helper
{
bool *result;
NumEffectState *state;
void operator()(Square)
{
if (state->inCheck(state->turn())
|| state->inCheck(alt(state->turn()))) {
*result = false;
return;
}
state->changeTurn();
*result = ImmediateCheckmate::hasCheckmateMove(state->turn(), *state);
state->changeTurn();
}
};
bool osl::rating::
Threatmate::knight2Step(const NumEffectState& state, Move move, Square king)
{
if (move.ptype() != KNIGHT)
return false;
const int y = king.y() + sign(state.turn())*4;
if (y != move.to().y())
return false;
const int x = move.to().x();
return (x == king.x() || abs(king.x() - x) == 2);
}
bool osl::rating::
Threatmate::captureForKnightCheck(const NumEffectState& state, Move move, Square king)
{
const Player defender = alt(state.turn());
const CArray<Square,2> knight_position = {{
Board_Table.nextSquare(defender, king, UUR),
Board_Table.nextSquare(defender, king, UUL)
}};
const Piece captured = state.pieceOnBoard(move.to());
assert(captured.isPiece());
for (int i=0; i<2; ++i) {
const Square kp = knight_position[i];
const Piece p = state.pieceAt(kp);
if (state.hasEffectNotBy(defender, captured, kp))
continue;
if (p.isEmpty()
&& (unpromote(move.capturePtype()) == KNIGHT
|| state.hasPieceOnStand<KNIGHT>(state.turn())))
return true;
if (p.canMoveOn(state.turn())
&& state.hasEffectByPtypeStrict<KNIGHT>(state.turn(), kp))
return true;
}
return false;
}
bool osl::rating::Threatmate::isCandidate(const NumEffectState& state, Move move)
{
const Player defender = alt(state.turn());
const Square king = state.kingSquare(defender);
if (Neighboring8Direct::hasEffectOrAdditional(state, move.ptypeO(), move.to(), king)
|| move.to().isNeighboring8(king)
|| state.longEffectAt(move.to(), alt(state.turn())).any() // todo: refinement
|| (! move.isDrop() && state.longEffectAt(move.from(), state.turn()).any()) // todo: refinement
)
return true;
if (move.capturePtype() != PTYPE_EMPTY
&& Neighboring8Direct::hasEffectOrAdditional(state, move.capturePtypeO(), move.to(), king))
return true;
const King8Info info(state.king8Info(defender));
if (move.capturePtype() != PTYPE_EMPTY
&& (info.dropCandidate()
|| (info.liberty() == 0 && captureForKnightCheck(state, move, king))))
return true;
if (state.inCheck()
&& (info.dropCandidate() || info.moveCandidate2()
|| /* only when hand knight or knight effect */info.liberty() == 0))
return true;
if (info.liberty() == 0
&& (knight2Step(state, move, king)
|| (! move.isDrop()
&& ((state.hasPieceOnStand<KNIGHT>(state.turn())
&& state.hasEffectIf(newPtypeO(state.turn(),KNIGHT), move.from(), king))
|| state.hasEffectByPtypeStrict<KNIGHT>(state.turn(), move.from())))))
return true;
return false;
}
bool osl::rating::Threatmate::match(const NumEffectState& cstate, Move move,
const RatingEnv&) const
{
NumEffectState& state = const_cast<NumEffectState&>(cstate);
if (! isCandidate(cstate, move))
return false;
bool result = false;
Helper helper = { &result, &state };
state.makeUnmakeMove(move, helper);
#ifdef OSL_DEBUG
if (result && ! isCandidate(cstate, move))
std::cerr << cstate << move << "\n", assert(0);
#endif
return result;
}
/* ------------------------------------------------------------------------- */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
checkmate.h
osl::CArray
Definition container.h:20
osl::ImmediateCheckmate::hasCheckmateMove
static bool hasCheckmateMove(NumEffectState const &state, Square target, King8Info mask, Move &bestMove)
osl::Move
圧縮していない moveの表現 .
Definition basic_type.h:1052
osl::Move::ptypeO
PtypeO ptypeO() const
移動後のPtype, i.e., 成る手だった場合成った後
Definition basic_type.h:1162
osl::Move::ptype
Ptype ptype() const
Definition basic_type.h:1155
osl::Move::isDrop
bool isDrop() const
Definition basic_type.h:1150
osl::Move::capturePtype
Ptype capturePtype() const
Definition basic_type.h:1180
osl::Move::capturePtypeO
PtypeO capturePtypeO() const
Definition basic_type.h:1185
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::NumEffectState
利きを持つ局面
Definition numEffectState.h:34
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::hasEffectIf
bool hasEffectIf(PtypeO ptypeo, Square attacker, Square target) const
attackerにptypeoの駒がいると仮定した場合にtargetに利きがあるかどうか を stateをupdateしないで確かめる.
Definition numEffectState.h:465
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::hasEffectByPtypeStrict
bool hasEffectByPtypeStrict(Player attack, Square target) const
target に ptype の利きがあるか?
Definition numEffectState.h:363
osl::Piece
駒.
Definition basic_type.h:788
osl::Piece::isEmpty
bool isEmpty() const
Definition basic_type.h:913
osl::Piece::canMoveOn
bool canMoveOn() const
Player Pの駒が，thisの上に移動できるか?
Definition basic_type.h:980
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::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::y
int y() const
将棋としてのY座標を返す.
Definition basic_type.h:567
osl::Square::isNeighboring8
bool isNeighboring8(Square to) const
Definition basic_type.cc:202
osl::Square::x
int x() const
将棋としてのX座標を返す.
Definition basic_type.h:563
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::misc::GeneralMask::any
bool any() const
Definition mask.h:298
osl::rating::RatingEnv
Definition ratingEnv.h:16
osl::rating::Threatmate::match
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
Definition checkmate.cc:94
osl::rating::Threatmate::isCandidate
static bool isCandidate(const NumEffectState &state, Move move)
Definition checkmate.cc:61
osl::rating::Threatmate::knight2Step
static bool knight2Step(const NumEffectState &state, Move move, Square king)
Definition checkmate.cc:25
osl::rating::Threatmate::captureForKnightCheck
static bool captureForKnightCheck(const NumEffectState &state, Move move, Square king)
Definition checkmate.cc:36
king8Info.h
osl::KNIGHT
@ KNIGHT
Definition basic_type.h:97
osl::PTYPE_EMPTY
@ PTYPE_EMPTY
Definition basic_type.h:85
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::sign
constexpr int sign(Player player)
Definition basic_type.h:23
osl::Player
Player
Definition basic_type.h:8
osl::alt
constexpr Player alt(Player player)
Definition basic_type.h:13
osl::newPtypeO
PtypeO newPtypeO(Player player, Ptype ptype)
Definition basic_type.h:211
osl::captured
PtypeO captured(PtypeO ptypeO)
unpromoteすると共に，ownerを反転する．
Definition basic_type.h:264
neighboring8Direct.h
osl::rating::Threatmate::Helper
Definition checkmate.cc:8
osl::rating::Threatmate::Helper::operator()
void operator()(Square)
Definition checkmate.cc:11
osl::rating::Threatmate::Helper::state
NumEffectState * state
Definition checkmate.cc:10
osl::rating::Threatmate::Helper::result
bool * result
Definition checkmate.cc:9