My Project
checkmate.cc
Go to the documentation of this file.
1/* checkmate.cc
2 */
6
8{
9 bool *result;
12 {
13 if (state->inCheck(state->turn())
14 || state->inCheck(alt(state->turn()))) {
15 *result = false;
16 return;
17 }
21 }
22};
23
25Threatmate::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:
const Square nextSquare(Player P, Square pos, Direction dr) const
next position from pos for player P.
Definition boardTable.h:61
圧縮していない moveの表現 .
PtypeO ptypeO() const
移動後のPtype, i.e., 成る手だった場合成った後
Ptype ptype() const
bool isDrop() const
Ptype capturePtype() const
PtypeO capturePtypeO() const
const Square to() const
const Square from() const
利きを持つ局面
const checkmate::King8Info king8Info(Player king) const
bool hasEffectNotBy(Player player, Piece piece, Square target) const
対象とするマスにあるプレイヤーの(ただしある駒以外)利きがあるかどうか.
bool hasEffectIf(PtypeO ptypeo, Square attacker, Square target) const
attackerにptypeoの駒がいると仮定した場合にtargetに利きがあるかどうか を stateをupdateしないで確かめる.
bool inCheck(Player P) const
Pの玉が王手状態
const mask_t longEffectAt(Square target) const
void makeUnmakeMove(Move move, Function &f)
bool hasEffectByPtypeStrict(Player attack, Square target) const
target に ptype の利きがあるか? 成不成を区別
bool isEmpty() const
Definition basic_type.h:913
bool canMoveOn() const
Player Pの駒が,thisの上に移動できるか? PIECE_EMPTY 0x00008000 BLACK_PIECE 0x000XxxYY X>=2, YY>0 PIECE_EDGE 0xfff1...
Definition basic_type.h:980
const Piece pieceOnBoard(Square sq) const
bool hasPieceOnStand(Player player, Ptype ptype) const
Player turn() const
void changeTurn()
手番を変更する
Square kingSquare() const
Definition simpleState.h:94
const Piece pieceAt(Square sq) const
int y() const
将棋としてのY座標を返す.
Definition basic_type.h:567
bool isNeighboring8(Square to) const
int x() const
将棋としてのX座標を返す.
Definition basic_type.h:563
static bool hasCheckmateMove(NumEffectState const &state, Square target, King8Info mask, Move &bestMove)
敵玉の8近傍の状態を表す.
Definition king8Info.h:29
unsigned int liberty() const
8-15 bit 目を 0-7bitにshiftして返す
Definition king8Info.h:54
unsigned int moveCandidate2() const
24-31 bit 目を 0-7bitにshiftして返す
Definition king8Info.h:69
unsigned int dropCandidate() const
0-7 bit 目を返す
Definition king8Info.h:49
static bool hasEffectOrAdditional(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
ptypeo の駒がfromからtargetの8近傍に直接の利きを持つか そのような駒への追加/影利きになっている
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
Definition checkmate.cc:94
static bool isCandidate(const NumEffectState &state, Move move)
Definition checkmate.cc:61
static bool knight2Step(const NumEffectState &state, Move move, Square king)
Definition checkmate.cc:25
static bool captureForKnightCheck(const NumEffectState &state, Move move, Square king)
Definition checkmate.cc:36
@ KNIGHT
Definition basic_type.h:97
@ PTYPE_EMPTY
Definition basic_type.h:85
Ptype unpromote(Ptype ptype)
ptypeがpromote後の型の時に,promote前の型を返す. promoteしていない型の時はそのまま返す
Definition basic_type.h:157
const BoardTable Board_Table
Definition tables.cc:95
@ UUR
Definition basic_type.h:323
@ UUL
Definition basic_type.h:322
constexpr int sign(Player player)
Definition basic_type.h:23
Player
Definition basic_type.h:8
constexpr Player alt(Player player)
Definition basic_type.h:13
PtypeO newPtypeO(Player player, Ptype ptype)
Definition basic_type.h:211
PtypeO captured(PtypeO ptypeO)
unpromoteすると共に,ownerを反転する.
Definition basic_type.h:264