My Project
std/osl/rating/feature/king8.h
Go to the documentation of this file.
1/* king8.h
2 */
3#ifndef RATING_KING8_H
4#define RATING_KING8_H
5
9
10namespace osl
11{
12 namespace rating
13 {
15 {
17 bool same;
18 static const std::string name(Ptype self, Ptype target, bool same);
19 public:
20 AttackKing8(Ptype s, Ptype t, bool ss, int attack, int defense)
22 self(s), target(t), same(ss)
23 {
24 }
25 bool match(const NumEffectState& state, Move move, const RatingEnv& env) const
26 {
27 if (move.ptype() != self)
28 return false;
29 const Square position
30 = Neighboring8Direct::findNearest(state, move.ptypeO(), move.to(), state.kingSquare(alt(state.turn())));
31 if (position.isPieceStand() || position == move.from())
32 return false;
33 if (! move.isDrop() && state.hasEffectByPiece(state.pieceOnBoard(move.from()), position))
34 return false;
35 const Piece p = state.pieceAt(position);
36 if (p.ptype() != target)
37 return false;
38 if (! CountEffect2::match(state, position, env))
39 return false;
40 if (!isPiece(target))
41 return true;
42 return same == (p.owner() == move.player());
43 }
44 };
45
46 class DefenseKing8 : public Feature
47 {
49 bool drop;
50 int danger;
51 public:
52 static const std::string name(Ptype self, bool drop, int danger);
53 DefenseKing8(Ptype s, bool d, int dan)
54 : Feature(name(s,d,dan)), self(s), drop(d), danger(dan)
55 {
56 }
57 static int count(const NumEffectState& state)
58 {
59 const Player attack = alt(state.turn());
60 const Square king = state.kingSquare(alt(attack));
61 int count = 0;
62 for (int dx=-1; dx<=1; ++dx) {
63 for (int dy=-1; dy<=1; ++dy) {
64 if (dx == 0 && dy ==0)
65 continue;
66 Square p = king + Offset(dx,dy);
67 if (! state.pieceAt(p).isEdge()
68 && state.hasEffectAt(attack, p))
69 ++count;
70 }
71 }
72 if (king.x() == 1 || king.x() == 9)
73 ++count;
74 return std::min(3, count);
75 }
76 static bool blocking(const NumEffectState& state, Square king, Square to)
77 {
78 const Player attacker = alt(state.turn());
79 Piece attack = state.findAttackAt<BISHOP>(attacker, to);
80 if (attack.isPiece()
81 && Neighboring8Direct::hasEffect(state, newPtypeO(attacker, BISHOP), attack.square(), king))
82 return true;
83 attack = state.findAttackAt<ROOK>(attacker, to);
84 if (attack.isPiece()
85 && Neighboring8Direct::hasEffect(state, newPtypeO(attacker, ROOK), attack.square(), king))
86 return true;
87 attack = state.findAttackAt<LANCE>(attacker, to);
88 return attack.isPiece() && attack.ptype() == LANCE
89 && Neighboring8Direct::hasEffect(state, newPtypeO(attacker, LANCE), attack.square(), king);
90 }
91 static bool matchDrop(const NumEffectState& state, Move move)
92 {
93 if (! move.isDrop())
94 return false;
95 const Square king = state.kingSquare(state.turn());
96 if (king.isNeighboring8(move.to())
97 || Neighboring8Direct::hasEffect(state, move.ptypeO(), move.to(), king))
98 return true;
99 return blocking(state, king, move.to());
100 }
101 static bool matchMove(const NumEffectState& state, Move move)
102 {
103 if (move.isDrop())
104 return false;
105 if (move.ptype() == KING)
106 return true;
107 const Square king = state.kingSquare(state.turn());
108 if (king.isNeighboring8(move.to())
109 || king.isNeighboring8(move.from())
110 || Neighboring8Direct::hasEffect(state, move.ptypeO(), move.to(), king))
111 return true;
112 return blocking(state, king, move.to());
113 }
114 bool match(const NumEffectState& state, Move move, const RatingEnv&) const
115 {
116 if (move.ptype() != self)
117 return false;
118 if (count(state) != danger)
119 return false;
120 if (drop)
121 return matchDrop(state, move);
122 return matchMove(state, move);
123 }
124 };
125 }
126}
127
128#endif /* RATING_KING8_H */
129// ;;; Local Variables:
130// ;;; mode:c++
131// ;;; c-basic-offset:2
132// ;;; End:
圧縮していない moveの表現 .
Definition: basic_type.h:1052
PtypeO ptypeO() const
移動後のPtype, i.e., 成る手だった場合成った後
Definition: basic_type.h:1162
Ptype ptype() const
Definition: basic_type.h:1155
Player player() const
Definition: basic_type.h:1195
bool isDrop() const
Definition: basic_type.h:1150
const Square to() const
Definition: basic_type.h:1132
const Square from() const
Definition: basic_type.h:1125
利きを持つ局面
bool hasEffectByPiece(Piece attack, Square target) const
駒attack が target に利きを持つか (旧hasEffectToと統合)
bool hasEffectAt(Square target) const
対象とするマスにあるプレイヤーの利きがあるかどうか.
const Piece findAttackAt(Player attack, Square target) const
return a piece s.t.
座標の差分
Definition: basic_type.h:430
Ptype ptype() const
Definition: basic_type.h:821
const Square square() const
Definition: basic_type.h:832
bool isEdge() const
Definition: basic_type.h:919
Player owner() const
Definition: basic_type.h:963
bool isPiece() const
Definition: basic_type.h:953
const Piece pieceOnBoard(Square sq) const
Definition: simpleState.h:170
Player turn() const
Definition: simpleState.h:220
Square kingSquare() const
Definition: simpleState.h:94
const Piece pieceAt(Square sq) const
Definition: simpleState.h:167
bool isPieceStand() const
Definition: basic_type.h:576
bool isNeighboring8(Square to) const
Definition: basic_type.cc:202
int x() const
将棋としてのX座標を返す.
Definition: basic_type.h:563
static Square findNearest(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
static bool hasEffect(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
ptypeo の駒がfromからtargetの8近傍に直接の利きを持つか
bool match(const NumEffectState &state, Move move, const RatingEnv &env) const
AttackKing8(Ptype s, Ptype t, bool ss, int attack, int defense)
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
static bool matchDrop(const NumEffectState &state, Move move)
static bool blocking(const NumEffectState &state, Square king, Square to)
DefenseKing8(Ptype s, bool d, int dan)
static bool matchMove(const NumEffectState &state, Move move)
static int count(const NumEffectState &state)
const std::string & name() const
int min(Player p, int v1, int v2)
Definition: evalTraits.h:92
Ptype
駒の種類を4ビットでコード化する
Definition: basic_type.h:84
@ ROOK
Definition: basic_type.h:100
@ BISHOP
Definition: basic_type.h:99
@ KING
Definition: basic_type.h:93
@ LANCE
Definition: basic_type.h:96
Player
Definition: basic_type.h:8
constexpr bool isPiece(Ptype ptype)
ptypeが空白やEDGEでないかのチェック
Definition: basic_type.h:120
constexpr Player alt(Player player)
Definition: basic_type.h:13
PtypeO newPtypeO(Player player, Ptype ptype)
Definition: basic_type.h:211
bool match(const NumEffectState &state, Square position, const RatingEnv &env) const
Definition: countEffect2.h:37