pattern.h Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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"
7 #include "osl/rating/feature/countEffect2.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:
18 Direction direction, direction2;
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;
61 struct LongTarget : CountEffect2
62 {
63 Ptype target;
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 {
105 Ptype target;
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 {
124 Direction direction;
125 Ptype self;
126 LongTarget target;
127 static const std::string name(Direction d, Ptype self);
128 public:
129 PatternLong(Direction d, Ptype s, LongTarget t);
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 {
151 Direction direction;
152 Ptype self;
153 LongTarget2 target2;
154 static const std::string name(Direction d, Ptype self);
155 public:
156 PatternLong2(Direction d, Ptype s, LongTarget2 t2);
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;
176 LongTarget target;
177 public:
178 PatternBlock(Ptype s, Ptype a, LongTarget t);
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: