pattern.h
Go to the documentation of this file.
1 /* patternLong2.h
2  */
3 #ifndef _PATTERNLONG2_H
4 #define _PATTERNLONG2_H
5 
6 #include "osl/rating/feature.h"
8 
9 namespace osl
10 {
11  namespace rating
12  {
13  class Pattern : public Feature, CountEffect2
14  {
15  public:
16  static const Direction INVALID = LONG_UL;
17  private:
19  Ptype self, target;
20  bool same;
21  static const std::string name(Direction d, Direction d2, Ptype self, Ptype target, bool same);
22  public:
23  Pattern(Direction d, Ptype s, Ptype t, bool ss, int attack, int defense)
24  : Feature(name(d, INVALID, s, t, ss)+CountEffect2::name(attack, defense)), CountEffect2(attack, defense),
25  direction(d), direction2(INVALID), self(s), target(t), same(ss)
26  {
27  }
28  Pattern(Direction d, Direction d2, Ptype s, Ptype t, bool ss, int attack, int defense)
29  : Feature(name(d, d2, s, t, ss)+CountEffect2::name(attack, defense)), CountEffect2(attack, defense),
30  direction(d), direction2(d2), self(s), target(t), same(ss)
31  {
32  }
33  static Square nextSquare(Player player, Square start, Direction direction, Direction direction2)
34  {
35  const Square p = Board_Table.nextSquare(player, start, direction);
36  if (direction2 == INVALID || ! p.isOnBoard())
37  return p;
38  return Board_Table.nextSquare(player, p, direction2);
39  }
40  static Square nextSquare(Move move, Direction direction, Direction direction2)
41  {
42  return nextSquare(move.player(), move.to(), direction, direction2);
43  }
44  bool match(const NumEffectState& state, Move move, const RatingEnv& env) const
45  {
46  if (move.ptype() != self)
47  return false;
48  const Square position = nextSquare(move, direction, direction2);
49  const Piece p = (position == move.from()) ? Piece::EMPTY() : state.pieceAt(position);
50  if (p.ptype() != target)
51  return false;
52  if (! CountEffect2::match(state, position, env))
53  return false;
54  if (!isPiece(target))
55  return true;
56  return same == (p.owner() == move.player());
57  }
58  };
59 
60  typedef std::pair<Piece,Square> PieceSquare;
62  {
64  bool promotable, same;
65  LongTarget(Ptype t, bool p, bool s, int attack, int defense)
66  : CountEffect2(attack, defense), target(t), promotable(p), same(s)
67  {
68  }
69  static bool isPromotable(Move move, Square position)
70  {
71  assert(position.isOnBoard());
72  return position.canPromote(move.player()) || (! move.isDrop() && move.to().canPromote(move.player()));
73  }
74  bool match(const NumEffectState& state, Move move, PieceSquare p, const RatingEnv& env) const
75  {
76  if (p.first.ptype() != target)
77  return false;
78  if (target == PTYPE_EDGE)
79  return true;
80  if (isPromotable(move, p.second) != promotable)
81  return false;
82  if (! CountEffect2::match(state, p.second, env))
83  return false;
84  if (! p.first.isPiece())
85  return true;
86  return same == (p.first.owner() == move.player());
87  }
88  bool matchOtherThanPromotable(const NumEffectState& state, Move move, PieceSquare p, const RatingEnv& env) const
89  {
90  if (p.first.ptype() != target)
91  return false;
92  if (target == PTYPE_EDGE)
93  return true;
94  if (! CountEffect2::match(state, p.second, env))
95  return false;
96  if (! p.first.isPiece())
97  return true;
98  return same == (p.first.owner() == move.player());
99  }
100  const std::string name() const;
101  };
102 
103  struct LongTarget2
104  {
106  bool same;
107  LongTarget2(Ptype t, bool s) : target(t), same(s)
108  {
109  }
110  bool match(const NumEffectState&, Move move, Piece p) const
111  {
112  if (p.ptype() != target)
113  return false;
114  assert(! p.isEmpty());
115  if (! isPiece(target))
116  return true;
117  return same == (p.owner() == move.player());
118  }
119  const std::string name() const;
120  };
121 
122  class PatternLong : public Feature
123  {
125  Ptype self;
127  static const std::string name(Direction d, Ptype self);
128  public:
137  static const PieceSquare nextPieceOrEnd(const SimpleState& state, Square start, Player player, Direction direction);
138  static const PieceSquare nextPieceOrEnd(const SimpleState& state, Square start, Offset);
139  static const PieceSquare find(const NumEffectState& state, Move move, Direction direction);
140  bool match(const NumEffectState& state, Move move, const RatingEnv& env) const
141  {
142  if (move.ptype() != self)
143  return false;
144  PieceSquare pp = find(state, move, direction);
145  return target.match(state, move, pp, env);
146  }
147  };
148 
149  class PatternLong2 : public Feature
150  {
152  Ptype self;
154  static const std::string name(Direction d, Ptype self);
155  public:
157  static const Piece find(const NumEffectState& state, Move move, Direction direction)
158  {
159  Piece p = state.nextPiece(move.to(), Board_Table.getOffset(move.player(), direction));
160  if (p.isPiece() && p.square() == move.from())
161  p = state.nextPiece(move.to(), Board_Table.getOffset(move.player(), direction));
162  return p;
163  }
164  bool match(const NumEffectState& state, Move move, const RatingEnv&) const
165  {
166  if (move.ptype() != self)
167  return false;
168  const Piece p = find(state, move, direction);
169  return target2.match(state, move, p);
170  }
171  };
172 
173  class PatternBlock : public Feature
174  {
175  Ptype self, attack;
177  public:
179  static const PieceSquare find(const NumEffectState& state, Move move, Ptype attacker_ptype);
180  bool match(const NumEffectState& state, Move move, const RatingEnv& env) const
181  {
182  if (move.ptype() != self)
183  return false;
184  PieceSquare pp = find(state, move, attack);
185  return ! pp.first.isEdge() && target.matchOtherThanPromotable(state, move, pp, env)
186  && pp.second.canPromote(pp.first.isPiece() ? alt(pp.first.owner()) : alt(move.player()))
187  == target.promotable;
188  }
189  };
190  }
191 }
192 
193 #endif /* _PATTERNLONG2_H */
194 // ;;; Local Variables:
195 // ;;; mode:c++
196 // ;;; c-basic-offset:2
197 // ;;; End:
bool match(const NumEffectState &, Move move, Piece p) const
Definition: pattern.h:110
static bool isPromotable(Move move, Square position)
Definition: pattern.h:69
bool match(const NumEffectState &state, Move move, const RatingEnv &env) const
Definition: pattern.h:180
constexpr Player alt(Player player)
Definition: basic_type.h:13
Pattern(Direction d, Ptype s, Ptype t, bool ss, int attack, int defense)
Definition: pattern.h:23
bool match(const NumEffectState &state, Move move, PieceSquare p, const RatingEnv &env) const
Definition: pattern.h:74
Ptype ptype() const
Definition: basic_type.h:821
座標の差分
Definition: basic_type.h:429
Direction direction2
Definition: pattern.h:18
bool matchOtherThanPromotable(const NumEffectState &state, Move move, PieceSquare p, const RatingEnv &env) const
Definition: pattern.h:88
bool isPiece() const
Definition: basic_type.h:953
const Piece pieceAt(Square sq) const
Definition: simpleState.h:167
static const Piece EMPTY()
Definition: basic_type.h:797
LongTarget(Ptype t, bool p, bool s, int attack, int defense)
Definition: pattern.h:65
bool match(const NumEffectState &state, Move move, const RatingEnv &env) const
Definition: pattern.h:44
static const Direction INVALID
Definition: pattern.h:16
Ptype
駒の種類を4ビットでコード化する
Definition: basic_type.h:83
Ptype ptype() const
Definition: basic_type.h:1155
bool isOnBoard() const
盤面上を表すかどうかの判定. 1<=x() && x()<=9 && 1<=y() && y()<=9 Squareの内部表現に依存する. ...
Definition: basic_type.h:583
const Square from() const
Definition: basic_type.h:1125
圧縮していない moveの表現 .
Definition: basic_type.h:1051
Player player() const
Definition: basic_type.h:1195
static const Piece find(const NumEffectState &state, Move move, Direction direction)
Definition: pattern.h:157
利きを持つ局面
const Square square() const
Definition: basic_type.h:832
bool isDrop() const
Definition: basic_type.h:1150
const Square to() const
Definition: basic_type.h:1132
Direction
Definition: basic_type.h:310
constexpr bool isPiece(Ptype ptype)
ptypeが空白やEDGEでないかのチェック
Definition: basic_type.h:120
std::pair< Piece, Square > PieceSquare
Definition: pattern.h:60
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
Definition: pattern.h:164
static Square nextSquare(Player player, Square start, Direction direction, Direction direction2)
Definition: pattern.h:33
bool isEmpty() const
Definition: basic_type.h:913
const Square nextSquare(Player P, Square pos, Direction dr) const
next position from pos for player P.
Definition: boardTable.h:61
Direction direction
Definition: pattern.h:18
Pattern(Direction d, Direction d2, Ptype s, Ptype t, bool ss, int attack, int defense)
Definition: pattern.h:28
static Square nextSquare(Move move, Direction direction, Direction direction2)
Definition: pattern.h:40
Player
Definition: basic_type.h:8
const std::string & name() const
const Offset getOffset(Direction dir) const
Definition: boardTable.h:47
bool match(const NumEffectState &state, Move move, const RatingEnv &env) const
Definition: pattern.h:140
bool match(const NumEffectState &state, Square position, const RatingEnv &env) const
Definition: countEffect2.h:37
Piece nextPiece(Square cur, Offset diff) const
diff方向にあるPiece を求める.
Definition: simpleState.h:208
LongTarget2(Ptype t, bool s)
Definition: pattern.h:107
bool canPromote() const
Definition: basic_type.h:659
const BoardTable Board_Table
Definition: tables.cc:95
Player owner() const
Definition: basic_type.h:963