My Project
checkmate.cc
Go to the documentation of this file.
1 /* checkmate.cc
2  */
4 #include "osl/bits/king8Info.h"
6 
8 {
9  bool *result;
12  {
13  if (state->inCheck(state->turn())
14  || state->inCheck(alt(state->turn()))) {
15  *result = false;
16  return;
17  }
18  state->changeTurn();
20  state->changeTurn();
21  }
22 };
23 
25 Threatmate::knight2Step(const NumEffectState& state, Move move, Square king)
26 {
27  if (move.ptype() != KNIGHT)
28  return false;
29  const int y = king.y() + sign(state.turn())*4;
30  if (y != move.to().y())
31  return false;
32  const int x = move.to().x();
33  return (x == king.x() || abs(king.x() - x) == 2);
34 }
37 {
38  const Player defender = alt(state.turn());
39  const CArray<Square,2> knight_position = {{
40  Board_Table.nextSquare(defender, king, UUR),
41  Board_Table.nextSquare(defender, king, UUL)
42  }};
43  const Piece captured = state.pieceOnBoard(move.to());
44  assert(captured.isPiece());
45  for (int i=0; i<2; ++i) {
46  const Square kp = knight_position[i];
47  const Piece p = state.pieceAt(kp);
48  if (state.hasEffectNotBy(defender, captured, kp))
49  continue;
50  if (p.isEmpty()
51  && (unpromote(move.capturePtype()) == KNIGHT
52  || state.hasPieceOnStand<KNIGHT>(state.turn())))
53  return true;
54  if (p.canMoveOn(state.turn())
55  && state.hasEffectByPtypeStrict<KNIGHT>(state.turn(), kp))
56  return true;
57  }
58  return false;
59 }
60 
62 {
63  const Player defender = alt(state.turn());
64  const Square king = state.kingSquare(defender);
65  if (Neighboring8Direct::hasEffectOrAdditional(state, move.ptypeO(), move.to(), king)
66  || move.to().isNeighboring8(king)
67  || state.longEffectAt(move.to(), alt(state.turn())).any() // todo: refinement
68  || (! move.isDrop() && state.longEffectAt(move.from(), state.turn()).any()) // todo: refinement
69  )
70  return true;
71  if (move.capturePtype() != PTYPE_EMPTY
72  && Neighboring8Direct::hasEffectOrAdditional(state, move.capturePtypeO(), move.to(), king))
73  return true;
74 
75  const King8Info info(state.king8Info(defender));
76  if (move.capturePtype() != PTYPE_EMPTY
77  && (info.dropCandidate()
78  || (info.liberty() == 0 && captureForKnightCheck(state, move, king))))
79  return true;
80  if (state.inCheck()
81  && (info.dropCandidate() || info.moveCandidate2()
82  || /* only when hand knight or knight effect */info.liberty() == 0))
83  return true;
84  if (info.liberty() == 0
85  && (knight2Step(state, move, king)
86  || (! move.isDrop()
87  && ((state.hasPieceOnStand<KNIGHT>(state.turn())
88  && state.hasEffectIf(newPtypeO(state.turn(),KNIGHT), move.from(), king))
89  || state.hasEffectByPtypeStrict<KNIGHT>(state.turn(), move.from())))))
90  return true;
91  return false;
92 }
93 
95  const RatingEnv&) const
96 {
97  NumEffectState& state = const_cast<NumEffectState&>(cstate);
98  if (! isCandidate(cstate, move))
99  return false;
100  bool result = false;
101  Helper helper = { &result, &state };
102  state.makeUnmakeMove(move, helper);
103 #ifdef OSL_DEBUG
104  if (result && ! isCandidate(cstate, move))
105  std::cerr << cstate << move << "\n", assert(0);
106 #endif
107  return result;
108 }
109 
110 /* ------------------------------------------------------------------------- */
111 // ;;; Local Variables:
112 // ;;; mode:c++
113 // ;;; c-basic-offset:2
114 // ;;; End:
osl::SimpleState::pieceAt
const Piece pieceAt(Square sq) const
Definition: simpleState.h:167
osl::Square
Definition: basic_type.h:532
osl::checkmate::King8Info::liberty
unsigned int liberty() const
8-15 bit 目を 0-7bitにshiftして返す
Definition: king8Info.h:54
checkmate.h
osl::Piece::canMoveOn
bool canMoveOn() const
Player Pの駒が,thisの上に移動できるか? PIECE_EMPTY 0x00008000 BLACK_PIECE 0x000XxxYY X>=2, YY>0 PIECE_EDGE 0xfff1...
Definition: basic_type.h:980
osl::checkmate::ImmediateCheckmate::hasCheckmateMove
static bool hasCheckmateMove(NumEffectState const &state, Square target, King8Info mask, Move &bestMove)
osl::Board_Table
const BoardTable Board_Table
Definition: tables.cc:95
osl::NumEffectState::king8Info
const checkmate::King8Info king8Info(Player king) const
Definition: numEffectState.cc:44
osl::alt
constexpr Player alt(Player player)
Definition: basic_type.h:13
neighboring8Direct.h
osl::rating::Threatmate::isCandidate
static bool isCandidate(const NumEffectState &state, Move move)
Definition: checkmate.cc:61
osl::Move
圧縮していない moveの表現 .
Definition: basic_type.h:1052
osl::UUL
@ UUL
Definition: basic_type.h:322
osl::newPtypeO
PtypeO newPtypeO(Player player, Ptype ptype)
Definition: basic_type.h:211
osl::NumEffectState::hasEffectIf
bool hasEffectIf(PtypeO ptypeo, Square attacker, Square target) const
attackerにptypeoの駒がいると仮定した場合にtargetに利きがあるかどうか を stateをupdateしないで確かめる.
Definition: numEffectState.h:465
osl::UUR
@ UUR
Definition: basic_type.h:323
osl::rating::Threatmate::Helper::operator()
void operator()(Square)
Definition: checkmate.cc:11
osl::Piece
駒.
Definition: basic_type.h:788
osl::NumEffectState::inCheck
bool inCheck(Player P) const
Pの玉が王手状態
Definition: numEffectState.h:88
osl::checkmate::King8Info::dropCandidate
unsigned int dropCandidate() const
0-7 bit 目を返す
Definition: king8Info.h:49
osl::rating::RatingEnv
Definition: ratingEnv.h:16
osl::SimpleState::pieceOnBoard
const Piece pieceOnBoard(Square sq) const
Definition: simpleState.h:170
osl::NumEffectState::hasEffectByPtypeStrict
bool hasEffectByPtypeStrict(Player attack, Square target) const
target に ptype の利きがあるか? 成不成を区別
Definition: numEffectState.h:363
osl::Move::capturePtype
Ptype capturePtype() const
Definition: basic_type.h:1180
osl::NumEffectState::hasEffectNotBy
bool hasEffectNotBy(Player player, Piece piece, Square target) const
対象とするマスにあるプレイヤーの(ただしある駒以外)利きがあるかどうか.
Definition: numEffectState.h:409
osl::Move::isDrop
bool isDrop() const
Definition: basic_type.h:1150
osl::Move::ptypeO
PtypeO ptypeO() const
移動後のPtype, i.e., 成る手だった場合成った後
Definition: basic_type.h:1162
osl::rating::Threatmate::captureForKnightCheck
static bool captureForKnightCheck(const NumEffectState &state, Move move, Square king)
Definition: checkmate.cc:36
osl::rating::Threatmate::match
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
Definition: checkmate.cc:94
osl::Move::from
const Square from() const
Definition: basic_type.h:1125
osl::SimpleState::hasPieceOnStand
bool hasPieceOnStand(Player player, Ptype ptype) const
Definition: simpleState.h:191
osl::Square::x
int x() const
将棋としてのX座標を返す.
Definition: basic_type.h:563
osl::captured
PtypeO captured(PtypeO ptypeO)
unpromoteすると共に,ownerを反転する.
Definition: basic_type.h:264
osl::NumEffectState
利きを持つ局面
Definition: numEffectState.h:34
osl::SimpleState::changeTurn
void changeTurn()
手番を変更する
Definition: simpleState.h:226
osl::BoardTable::nextSquare
const Square nextSquare(Player P, Square pos, Direction dr) const
next position from pos for player P.
Definition: boardTable.h:61
king8Info.h
osl::SimpleState::kingSquare
Square kingSquare() const
Definition: simpleState.h:94
osl::rating::Threatmate::Helper
Definition: checkmate.cc:8
osl::Square::y
int y() const
将棋としてのY座標を返す.
Definition: basic_type.h:567
osl::sign
constexpr int sign(Player player)
Definition: basic_type.h:23
osl::checkmate::King8Info::moveCandidate2
unsigned int moveCandidate2() const
24-31 bit 目を 0-7bitにshiftして返す
Definition: king8Info.h:69
osl::Square::isNeighboring8
bool isNeighboring8(Square to) const
Definition: basic_type.cc:202
osl::Move::capturePtypeO
PtypeO capturePtypeO() const
Definition: basic_type.h:1185
osl::Piece::isEmpty
bool isEmpty() const
Definition: basic_type.h:913
osl::Move::ptype
Ptype ptype() const
Definition: basic_type.h:1155
osl::rating::Threatmate::knight2Step
static bool knight2Step(const NumEffectState &state, Move move, Square king)
Definition: checkmate.cc:25
osl::PTYPE_EMPTY
@ PTYPE_EMPTY
Definition: basic_type.h:85
osl::SimpleState::turn
Player turn() const
Definition: simpleState.h:220
osl::rating::Threatmate::Helper::result
bool * result
Definition: checkmate.cc:9
osl::Player
Player
Definition: basic_type.h:8
osl::effect_util::Neighboring8Direct::hasEffectOrAdditional
static bool hasEffectOrAdditional(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
ptypeo の駒がfromからtargetの8近傍に直接の利きを持つか そのような駒への追加/影利きになっている
Definition: neighboring8Direct.h:118
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::CArray
Definition: container.h:20
osl::Move::to
const Square to() const
Definition: basic_type.h:1132
osl::rating::Threatmate::Helper::state
NumEffectState * state
Definition: checkmate.cc:10
osl::checkmate::King8Info
敵玉の8近傍の状態を表す.
Definition: king8Info.h:29
osl::unpromote
Ptype unpromote(Ptype ptype)
ptypeがpromote後の型の時に,promote前の型を返す. promoteしていない型の時はそのまま返す
Definition: basic_type.h:157
osl::KNIGHT
@ KNIGHT
Definition: basic_type.h:97