All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 #include "osl/neighboring8.h"
10 
11 namespace osl
12 {
13  namespace rating
14  {
16  {
17  Ptype self, target;
18  bool same;
19  static const std::string name(Ptype self, Ptype target, bool same);
20  public:
21  AttackKing8(Ptype s, Ptype t, bool ss, int attack, int defense)
22  : Feature(name(s, t, ss)+CountEffect2::name(attack, defense)), CountEffect2(attack, defense),
23  self(s), target(t), same(ss)
24  {
25  }
26  bool match(const NumEffectState& state, Move move, const RatingEnv& env) const
27  {
28  if (move.ptype() != self)
29  return false;
30  const Square position
31  = Neighboring8Direct::findNearest(state, move.ptypeO(), move.to(), state.kingSquare(alt(state.turn())));
32  if (position.isPieceStand() || position == move.from())
33  return false;
34  if (! move.isDrop() && state.hasEffectByPiece(state.pieceOnBoard(move.from()), position))
35  return false;
36  const Piece p = state.pieceAt(position);
37  if (p.ptype() != target)
38  return false;
39  if (! CountEffect2::match(state, position, env))
40  return false;
41  if (!isPiece(target))
42  return true;
43  return same == (p.owner() == move.player());
44  }
45  };
46 
47  class DefenseKing8 : public Feature
48  {
49  Ptype self;
50  bool drop;
51  int danger;
52  public:
53  static const std::string name(Ptype self, bool drop, int danger);
54  DefenseKing8(Ptype s, bool d, int dan)
55  : Feature(name(s,d,dan)), self(s), drop(d), danger(dan)
56  {
57  }
58  static int count(const NumEffectState& state)
59  {
60  const Player attack = alt(state.turn());
61  const Square king = state.kingSquare(alt(attack));
62  int count = 0;
63  for (int dx=-1; dx<=1; ++dx) {
64  for (int dy=-1; dy<=1; ++dy) {
65  if (dx == 0 && dy ==0)
66  continue;
67  Square p = king + Offset(dx,dy);
68  if (! state.pieceAt(p).isEdge()
69  && state.hasEffectAt(attack, p))
70  ++count;
71  }
72  }
73  if (king.x() == 1 || king.x() == 9)
74  ++count;
75  return std::min(3, count);
76  }
77  static bool blocking(const NumEffectState& state, Square king, Square to)
78  {
79  const Player attacker = alt(state.turn());
80  Piece attack = state.findAttackAt<BISHOP>(attacker, to);
81  if (attack.isPiece()
82  && Neighboring8Direct::hasEffect(state, newPtypeO(attacker, BISHOP), attack.square(), king))
83  return true;
84  attack = state.findAttackAt<ROOK>(attacker, to);
85  if (attack.isPiece()
86  && Neighboring8Direct::hasEffect(state, newPtypeO(attacker, ROOK), attack.square(), king))
87  return true;
88  attack = state.findAttackAt<LANCE>(attacker, to);
89  return attack.isPiece() && attack.ptype() == LANCE
90  && Neighboring8Direct::hasEffect(state, newPtypeO(attacker, LANCE), attack.square(), king);
91  }
92  static bool matchDrop(const NumEffectState& state, Move move)
93  {
94  if (! move.isDrop())
95  return false;
96  const Square king = state.kingSquare(state.turn());
97  if (Neighboring8::isNeighboring8(king, move.to())
98  || Neighboring8Direct::hasEffect(state, move.ptypeO(), move.to(), king))
99  return true;
100  return blocking(state, king, move.to());
101  }
102  static bool matchMove(const NumEffectState& state, Move move)
103  {
104  if (move.isDrop())
105  return false;
106  if (move.ptype() == KING)
107  return true;
108  const Square king = state.kingSquare(state.turn());
109  if (Neighboring8::isNeighboring8(king, move.to())
110  || Neighboring8::isNeighboring8(king, move.from())
111  || Neighboring8Direct::hasEffect(state, move.ptypeO(), move.to(), king))
112  return true;
113  return blocking(state, king, move.to());
114  }
115  bool match(const NumEffectState& state, Move move, const RatingEnv&) const
116  {
117  if (move.ptype() != self)
118  return false;
119  if (count(state) != danger)
120  return false;
121  if (drop)
122  return matchDrop(state, move);
123  return matchMove(state, move);
124  }
125  };
126  }
127 }
128 
129 #endif /* RATING_KING8_H */
130 // ;;; Local Variables:
131 // ;;; mode:c++
132 // ;;; c-basic-offset:2
133 // ;;; End: