My Project
rating/feature.h
Go to the documentation of this file.
1/* feature.h
2 */
3#ifndef _FEATURE_H
4#define _FEATURE_H
5
8#include <string>
9
10namespace osl
11{
12 namespace rating
13 {
14 class Feature
15 {
16 std::string my_name;
17 public:
18 Feature(const std::string& name) : my_name(name)
19 {
20 }
21 virtual ~Feature();
22 virtual bool match(const NumEffectState& state, Move, const RatingEnv&) const =0;
23 virtual bool effectiveInCheck() const { return false; }
24 const std::string& name() const { return my_name; }
25 };
26
27 class TakeBack : public Feature
28 {
29 public:
30 TakeBack() : Feature("TakeBack")
31 {
32 }
33 bool match(const NumEffectState&, Move move, const RatingEnv& env) const
34 {
35 return env.history.hasLastMove() && move.to() == env.history.lastMove().to();
36 }
37 virtual bool effectiveInCheck() const { return true; }
38 };
39
40 class TakeBack2 : public Feature
41 {
42 public:
43 TakeBack2() : Feature("TakeBack2")
44 {
45 }
46 bool match(const NumEffectState&, Move move, const RatingEnv& env) const
47 {
48 return env.history.hasLastMove(2)
49 && move.to() == env.history.lastMove().to()
50 && move.to() == env.history.lastMove(2).to();
51 }
52 bool effectiveInCheck() const { return true; }
53 };
54
55
56 class Check : public Feature
57 {
59 public:
60 Check(int p);
61 static bool openLong(const NumEffectState& state, Move move)
62 {
63 if (move.isDrop())
64 return false;
65 return state.hasEffectByPtype<LANCE>(move.player(), move.from())
66 || state.hasEffectByPtype<BISHOP>(move.player(), move.from())
67 || state.hasEffectByPtype<ROOK>(move.player(), move.from());
68 }
69 bool match(const NumEffectState& state, Move move, const RatingEnv&) const;
71 bool effectiveInCheck() const { return true; }
72 };
73
74 class SendOff : public Feature
75 {
76 bool capture;
77 public:
78 SendOff(bool c) : Feature("SO"), capture(c) {}
79 bool match(const NumEffectState&, Move move, const RatingEnv& env) const
80 {
81 return env.sendoffs.isMember(move.to()) && (move.capturePtype() !=PTYPE_EMPTY) == capture;
82 }
83 };
84
85 class Block : public Feature
86 {
88 public:
89 static const std::string name(int self, int opponent);
90 Block(int s, int o) : Feature(name(s, o)), self(s), opponent(o) {}
91 static int count(const NumEffectState& state, Square position, Player player)
92 {
93 return (state.findAttackAt<LANCE>(player, position).ptype() == LANCE)
94 + state.hasEffectByPtype<BISHOP>(player, position)
95 + state.hasEffectByPtype<ROOK>(player, position);
96 }
97 bool match(const NumEffectState& state, Move move, const RatingEnv&) const
98 {
99 return count(state, move.to(), state.turn()) == self
100 && count(state, move.to(), alt(state.turn())) == opponent;
101 }
102 bool effectiveInCheck() const { return true; }
103 };
104
105 // { none, tate, naname, both }
107 {
108 static int index(const NumEffectState& state, Player player, Square from)
109 {
110 if (from.isPieceStand())
111 return -1;
112 const bool vertical = state.hasEffectByPtype<LANCE>(player, from)
113 || state.hasEffectByPtype<ROOK>(player, from);
114 const bool diagonal = state.hasEffectByPtype<BISHOP>(player, from);
115 return diagonal*2+vertical;
116 }
117 };
118 // { none, tate, naname, both } * { none, tate, naname, both }
119 class Open : public Feature
120 {
122 public:
123 Open(int p) : Feature(name(p)), property(p) {}
124 static int index(const NumEffectState& state, Move move)
125 {
126 if (move.isDrop())
127 return -1;
128 return CountOpen::index(state, move.player(), move.from())*4
129 + CountOpen::index(state, alt(move.player()), move.from());
130 }
131 bool match(const NumEffectState& state, Move move, const RatingEnv&) const
132 {
133 return index(state, move) == property;
134 }
135 static const std::string name(int property);
136 bool effectiveInCheck() const { return true; }
137 };
138
139 class Chase : public Feature
140 {
141 public:
143 private:
145 bool drop;
147 public:
148 Chase(Ptype s, Ptype t, bool d, OpponentType o)
149 : Feature(name(s,t,d,o)), self(s), target(t), drop(d), opponent_type(o) {}
150 bool match(const NumEffectState& state, Move move, const RatingEnv& env) const
151 {
152 const Move last_move = env.history.lastMove();
153 if (! last_move.isNormal())
154 return false;
155 if (! (move.ptype() == self && last_move.ptype() == target
156 && drop == move.isDrop()))
157 return false;
158 switch (opponent_type) {
159 case CAPTURE:
160 if (last_move.capturePtype() == PTYPE_EMPTY)
161 return false;
162 break;
163 case DROP:
164 if (! last_move.isDrop())
165 return false;
166 break;
167 case ESCAPE:
168 if (last_move.isDrop() || last_move.capturePtype() != PTYPE_EMPTY
169 || ! state.hasEffectAt(state.turn(), last_move.from()))
170 return false;
171 break;
172 case OTHER:
173 if (last_move.isDrop() || last_move.capturePtype() != PTYPE_EMPTY)
174 return false;
175 break;
176 }
177 return state.hasEffectIf
178(move.ptypeO(), move.to(), last_move.to());
179 }
180 static const std::string name(Ptype, Ptype, bool, OpponentType);
181 };
182
184 {
185 struct Test;
187 public:
189 bool match(const NumEffectState& state, Move move, const RatingEnv& env) const;
190 bool effectiveInCheck() const { return true; }
191 static int index(const NumEffectState& state, Move move, const RatingEnv& env);
192 };
193
194 class RookDefense : public Feature
195 {
196 public:
197 RookDefense() : Feature("RookDefense")
198 {
199 }
200 bool match(const NumEffectState& state, Move move, const RatingEnv& env) const
201 {
202 if (move.isDrop() || env.progress.value() > 8)
203 return false;
205 Piece rook2 = state.pieceOf(PtypeTraits<ROOK>::indexMin + 1);
206 if (move.from() == rook2.square())
207 std::swap(rook1, rook2);
208 if (move.from() != rook1.square()
209 || rook2.square().isPieceStand()
210 || rook2.owner() == move.player()
211 || rook2.square().x() != move.to().x())
212 return false;
213 return (move.to().y() - rook2.square().y())*sign(move.player()) > 0;
214 }
215 };
216
217 class BadLance : public Feature
218 {
220 public:
221 explicit BadLance(bool h) : Feature(h ? "StrongBadLance" : "WeakBadLance"), has_effect(h)
222 {
223 }
224 static bool basicMatch(const NumEffectState& state, Move move, Square front)
225 {
226 if (! (move.isDrop() && move.ptype() == LANCE))
227 return false;
228 return state.pieceOnBoard(front).isEmpty()
229 && state.hasPieceOnStand<PAWN>(alt(move.player()))
230 && !state.isPawnMaskSet(alt(move.player()), front.x());
231 }
232 bool match(const NumEffectState& state, Move move, const RatingEnv&) const
233 {
234 const Square front = Board_Table.nextSquare(move.player(), move.to(), U);
235 return basicMatch(state, move, front)
236 && ((!has_effect) ^ state.hasEffectAt(alt(move.player()), front));
237 }
238 };
239
240 class PawnAttack : public Feature
241 {
242 public:
244 {
245 }
246 bool match(const NumEffectState&, Move move, const RatingEnv& env) const
247 {
248 if (! (move.isDrop() && move.ptype() == PAWN))
249 return false;
250 const Move last_move = env.history.lastMove();
251 if (! last_move.isNormal() || last_move.capturePtype() == PTYPE_EMPTY)
252 return false;
253 return last_move.capturePtype() == PAWN && last_move.to().x() == move.to().x();
254 }
255 };
256
257 }
258}
259
260
261#endif /* _FEATURE_H */
262// ;;; Local Variables:
263// ;;; mode:c++
264// ;;; c-basic-offset:2
265// ;;; End:
const Square nextSquare(Player P, Square pos, Direction dr) const
next position from pos for player P.
Definition: boardTable.h:61
圧縮していない 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
Ptype capturePtype() const
Definition: basic_type.h:1180
bool isNormal() const
INVALID でも PASS でもない.
Definition: basic_type.h:1088
const Square to() const
Definition: basic_type.h:1132
const Square from() const
Definition: basic_type.h:1125
利きを持つ局面
bool hasEffectAt(Square target) const
対象とするマスにあるプレイヤーの利きがあるかどうか.
bool hasEffectIf(PtypeO ptypeo, Square attacker, Square target) const
attackerにptypeoの駒がいると仮定した場合にtargetに利きがあるかどうか を stateをupdateしないで確かめる.
const Piece findAttackAt(Player attack, Square target) const
return a piece s.t.
bool hasEffectByPtype(Player attack, Square target) const
target に ptype の利きがあるか? 成不成を区別しない
const Square square() const
Definition: basic_type.h:832
bool isEmpty() const
Definition: basic_type.h:913
Player owner() const
Definition: basic_type.h:963
const Piece pieceOnBoard(Square sq) const
Definition: simpleState.h:170
bool hasPieceOnStand(Player player, Ptype ptype) const
Definition: simpleState.h:191
Player turn() const
Definition: simpleState.h:220
const Piece pieceOf(int num) const
Definition: simpleState.h:76
bool isPawnMaskSet(Player player, int x) const
Definition: simpleState.h:146
bool isPieceStand() const
Definition: basic_type.h:576
int y() const
将棋としてのY座標を返す.
Definition: basic_type.h:567
int x() const
将棋としてのX座標を返す.
Definition: basic_type.h:563
bool hasLastMove(size_t last=1) const
Definition: moveStack.h:27
const Move lastMove(size_t last=1) const
Definition: moveStack.h:28
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
static bool basicMatch(const NumEffectState &state, Move move, Square front)
Block(int s, int o)
bool effectiveInCheck() const
static int count(const NumEffectState &state, Square position, Player player)
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
Chase(Ptype s, Ptype t, bool d, OpponentType o)
bool match(const NumEffectState &state, Move move, const RatingEnv &env) const
OpponentType opponent_type
static bool openLong(const NumEffectState &state, Move move)
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
Definition: feature.cc:16
bool effectiveInCheck() const
static const CArray< const char *, 4 > check_property
virtual ~Feature()
Definition: feature.cc:7
Feature(const std::string &name)
virtual bool effectiveInCheck() const
const std::string & name() const
virtual bool match(const NumEffectState &state, Move, const RatingEnv &) const =0
ImmediateAddSupport(Ptype self, Ptype attack)
Definition: feature.cc:63
static int index(const NumEffectState &state, Move move, const RatingEnv &env)
Definition: feature.cc:89
bool match(const NumEffectState &state, Move move, const RatingEnv &env) const
Definition: feature.cc:70
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
static int index(const NumEffectState &state, Move move)
bool effectiveInCheck() const
bool match(const NumEffectState &, Move move, const RatingEnv &env) const
Progress16 progress
Definition: ratingEnv.h:22
bool match(const NumEffectState &state, Move move, const RatingEnv &env) const
bool match(const NumEffectState &, Move move, const RatingEnv &env) const
bool match(const NumEffectState &, Move move, const RatingEnv &env) const
bool effectiveInCheck() const
bool match(const NumEffectState &, Move move, const RatingEnv &env) const
virtual bool effectiveInCheck() const
Ptype
駒の種類を4ビットでコード化する
Definition: basic_type.h:84
@ ROOK
Definition: basic_type.h:100
@ BISHOP
Definition: basic_type.h:99
@ PAWN
Definition: basic_type.h:95
@ PTYPE_EMPTY
Definition: basic_type.h:85
@ LANCE
Definition: basic_type.h:96
const BoardTable Board_Table
Definition: tables.cc:95
@ U
Definition: basic_type.h:314
constexpr int sign(Player player)
Definition: basic_type.h:23
Player
Definition: basic_type.h:8
constexpr Player alt(Player player)
Definition: basic_type.h:13
bool isMember(Square position) const
Definition: square8.h:22
static int index(const NumEffectState &state, Player player, Square from)