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();
19  *result = ImmediateCheckmate::hasCheckmateMove(state->turn(), *state);
20  state->changeTurn();
21  }
22 };
23 
24 bool osl::rating::
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 }
35 bool osl::rating::
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:
Ptype unpromote(Ptype ptype)
ptypeがpromote後の型の時に,promote前の型を返す. promoteしていない型の時はそのまま返す ...
Definition: basic_type.h:157
bool hasEffectIf(PtypeO ptypeo, Square attacker, Square target) const
attackerにptypeoの駒がいると仮定した場合にtargetに利きがあるかどうか を stateをupdateしないで確かめる...
bool canMoveOn() const
Player Pの駒が,thisの上に移動できるか? PIECE_EMPTY 0x00008000 BLACK_PIECE 0x000XxxYY X>=2...
Definition: basic_type.h:980
constexpr Player alt(Player player)
Definition: basic_type.h:13
PtypeO capturePtypeO() const
Definition: basic_type.h:1185
void makeUnmakeMove(Move move, Function &f)
bool isPiece() const
Definition: basic_type.h:953
const Piece pieceAt(Square sq) const
Definition: simpleState.h:167
int y() const
将棋としてのY座標を返す.
Definition: basic_type.h:567
const Piece pieceOnBoard(Square sq) const
Definition: simpleState.h:170
void changeTurn()
手番を変更する
Definition: simpleState.h:226
PtypeO ptypeO() const
移動後のPtype, i.e., 成る手だった場合成った後
Definition: basic_type.h:1162
static bool captureForKnightCheck(const NumEffectState &state, Move move, Square king)
Definition: checkmate.cc:36
int x() const
将棋としてのX座標を返す.
Definition: basic_type.h:563
PtypeO newPtypeO(Player player, Ptype ptype)
Definition: basic_type.h:211
Square kingSquare() const
Definition: simpleState.h:94
Ptype ptype() const
Definition: basic_type.h:1155
const mask_t longEffectAt(Square target) const
const Square from() const
Definition: basic_type.h:1125
bool isNeighboring8(Square to) const
Definition: basic_type.cc:202
圧縮していない moveの表現 .
Definition: basic_type.h:1051
bool hasPieceOnStand(Player player, Ptype ptype) const
Definition: simpleState.h:191
敵玉の8近傍の状態を表す.
Definition: king8Info.h:28
static bool knight2Step(const NumEffectState &state, Move move, Square king)
Definition: checkmate.cc:25
static bool isCandidate(const NumEffectState &state, Move move)
Definition: checkmate.cc:61
Player turn() const
Definition: simpleState.h:220
constexpr int sign(Player player)
Definition: basic_type.h:23
利きを持つ局面
const checkmate::King8Info king8Info(Player king) const
bool inCheck(Player P) const
Pの玉が王手状態
Ptype capturePtype() const
Definition: basic_type.h:1180
bool isDrop() const
Definition: basic_type.h:1150
const Square to() const
Definition: basic_type.h:1132
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
Definition: checkmate.cc:94
bool hasEffectByPtypeStrict(Player attack, Square target) const
target に ptype の利きがあるか? 成不成を区別
bool isEmpty() const
Definition: basic_type.h:913
PtypeO captured(PtypeO ptypeO)
unpromoteすると共に,ownerを反転する.
Definition: basic_type.h:264
const Square nextSquare(Player P, Square pos, Direction dr) const
next position from pos for player P.
Definition: boardTable.h:61
static bool hasCheckmateMove(NumEffectState const &state, Square target, King8Info mask, Move &bestMove)
bool hasEffectNotBy(Player player, Piece piece, Square target) const
対象とするマスにあるプレイヤーの(ただしある駒以外)利きがあるかどうか.
Player
Definition: basic_type.h:8
static bool hasEffectOrAdditional(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
ptypeo の駒がfromからtargetの8近傍に直接の利きを持つか そのような駒への追加/影利きになっている ...
const BoardTable Board_Table
Definition: tables.cc:95