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 
6 #include "osl/rating/feature.h"
9 
10 namespace osl
11 {
12  namespace rating
13  {
15  {
16  Ptype self, target;
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)
21  : Feature(name(s, t, ss)+CountEffect2::name(attack, defense)), CountEffect2(attack, 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  {
48  Ptype self;
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:
bool hasEffectAt(Square target) const
対象とするマスにあるプレイヤーの利きがあるかどうか.
constexpr Player alt(Player player)
Definition: basic_type.h:13
const Piece findAttackAt(Player attack, Square target) const
return a piece s.t.
Ptype ptype() const
Definition: basic_type.h:821
int min(Player p, int v1, int v2)
Definition: evalTraits.h:92
座標の差分
Definition: basic_type.h:429
static int count(const NumEffectState &state)
const Piece pieceAt(Square sq) const
Definition: simpleState.h:167
const Piece pieceOnBoard(Square sq) const
Definition: simpleState.h:170
bool hasEffectByPiece(Piece attack, Square target) const
駒attack が target に利きを持つか (旧hasEffectToと統合)
PtypeO ptypeO() const
移動後のPtype, i.e., 成る手だった場合成った後
Definition: basic_type.h:1162
PtypeO newPtypeO(Player player, Ptype ptype)
Definition: basic_type.h:211
static bool matchMove(const NumEffectState &state, Move move)
Ptype
駒の種類を4ビットでコード化する
Definition: basic_type.h:83
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
Square kingSquare() const
Definition: simpleState.h:94
Ptype ptype() const
Definition: basic_type.h:1155
bool match(const NumEffectState &state, Move move, const RatingEnv &env) 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
static Square findNearest(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
Player player() const
Definition: basic_type.h:1195
Player turn() const
Definition: simpleState.h:220
static bool hasEffect(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
ptypeo の駒がfromからtargetの8近傍に直接の利きを持つか
bool isEdge() const
Definition: basic_type.h:919
static bool matchDrop(const NumEffectState &state, Move move)
利きを持つ局面
DefenseKing8(Ptype s, bool d, int dan)
bool isDrop() const
Definition: basic_type.h:1150
const Square to() const
Definition: basic_type.h:1132
constexpr bool isPiece(Ptype ptype)
ptypeが空白やEDGEでないかのチェック
Definition: basic_type.h:120
static bool blocking(const NumEffectState &state, Square king, Square to)
Player
Definition: basic_type.h:8
const std::string & name() const
AttackKing8(Ptype s, Ptype t, bool ss, int attack, int defense)
bool isPieceStand() const
Definition: basic_type.h:576
bool match(const NumEffectState &state, Square position, const RatingEnv &env) const
Definition: countEffect2.h:37
Player owner() const
Definition: basic_type.h:963
static std::pair< int, int > count(const NumEffectState &state, Square position, const RatingEnv &env)
Definition: countEffect2.h:21