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 
6 #include "osl/rating/ratingEnv.h"
7 #include "osl/numEffectState.h"
8 #include <string>
9 
10 namespace 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  {
58  int property;
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  {
87  int self, opponent;
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 }
106  struct CountOpen
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  {
121  int property;
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:
144  Ptype self, target;
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;
186  Ptype self, attack;
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:
243  PawnAttack() : Feature("PA")
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:
ratingEnv.h
osl::rating::ImmediateAddSupport::index
static int index(const NumEffectState &state, Move move, const RatingEnv &env)
Definition: feature.cc:89
osl::rating::Open::match
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
Definition: rating/feature.h:131
osl::Square
Definition: basic_type.h:532
osl::rating::SendOff::SendOff
SendOff(bool c)
Definition: rating/feature.h:78
osl::rating::Feature::my_name
std::string my_name
Definition: rating/feature.h:16
osl::rating::TakeBack2::TakeBack2
TakeBack2()
Definition: rating/feature.h:43
osl::rating::Chase::opponent_type
OpponentType opponent_type
Definition: rating/feature.h:146
osl::rating::CountOpen::index
static int index(const NumEffectState &state, Player player, Square from)
Definition: rating/feature.h:108
osl::rating::Block::self
int self
Definition: rating/feature.h:87
osl::Board_Table
const BoardTable Board_Table
Definition: tables.cc:95
osl::rating::BadLance
Definition: rating/feature.h:218
osl::alt
constexpr Player alt(Player player)
Definition: basic_type.h:13
osl::rating::Check
Definition: rating/feature.h:57
osl::rating::Check::Check
Check(int p)
Definition: feature.cc:12
osl::rating::Block::match
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
Definition: rating/feature.h:97
osl::rating::Chase::Chase
Chase(Ptype s, Ptype t, bool d, OpponentType o)
Definition: rating/feature.h:148
osl::Move
圧縮していない moveの表現 .
Definition: basic_type.h:1052
osl::rating::SendOff::capture
bool capture
Definition: rating/feature.h:76
osl::rating::Feature::~Feature
virtual ~Feature()
Definition: feature.cc:7
osl::rating::Open::Open
Open(int p)
Definition: rating/feature.h:123
osl::container::MoveStack::lastMove
const Move lastMove(size_t last=1) const
Definition: moveStack.h:28
osl::PtypeTraits
Definition: ptypeTraits.h:12
osl::rating::Feature::name
const std::string & name() const
Definition: rating/feature.h:24
osl::rating::RatingEnv::progress
Progress16 progress
Definition: ratingEnv.h:22
osl::rating::Block::opponent
int opponent
Definition: rating/feature.h:87
osl::rating::Block::effectiveInCheck
bool effectiveInCheck() const
Definition: rating/feature.h:102
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition: basic_type.h:84
osl::rating::CountOpen
Definition: rating/feature.h:107
osl::LANCE
@ LANCE
Definition: basic_type.h:96
osl::NumEffectState::hasEffectIf
bool hasEffectIf(PtypeO ptypeo, Square attacker, Square target) const
attackerにptypeoの駒がいると仮定した場合にtargetに利きがあるかどうか を stateをupdateしないで確かめる.
Definition: numEffectState.h:465
osl::rating::Chase
Definition: rating/feature.h:140
osl::rating::Open
Definition: rating/feature.h:120
osl::NumEffectState::hasEffectAt
bool hasEffectAt(Square target) const
対象とするマスにあるプレイヤーの利きがあるかどうか.
Definition: numEffectState.h:324
osl::Piece
駒.
Definition: basic_type.h:788
osl::rating::Feature
Definition: rating/feature.h:15
osl::SimpleState::pieceOf
const Piece pieceOf(int num) const
Definition: simpleState.h:76
osl::NumEffectState::findAttackAt
const Piece findAttackAt(Player attack, Square target) const
return a piece s.t.
Definition: numEffectState.h:503
osl::rating::TakeBack2::effectiveInCheck
bool effectiveInCheck() const
Definition: rating/feature.h:52
osl::rating::RatingEnv
Definition: ratingEnv.h:16
osl::SimpleState::pieceOnBoard
const Piece pieceOnBoard(Square sq) const
Definition: simpleState.h:170
osl::Square::isPieceStand
bool isPieceStand() const
Definition: basic_type.h:576
osl::rating::TakeBack::effectiveInCheck
virtual bool effectiveInCheck() const
Definition: rating/feature.h:37
osl::rating::Check::effectiveInCheck
bool effectiveInCheck() const
Definition: rating/feature.h:71
osl::rating::Feature::effectiveInCheck
virtual bool effectiveInCheck() const
Definition: rating/feature.h:23
osl::rating::Check::property
int property
Definition: rating/feature.h:58
osl::rating::TakeBack
Definition: rating/feature.h:28
osl::BISHOP
@ BISHOP
Definition: basic_type.h:99
osl::Move::capturePtype
Ptype capturePtype() const
Definition: basic_type.h:1180
osl::rating::Block::count
static int count(const NumEffectState &state, Square position, Player player)
Definition: rating/feature.h:91
osl::rating::RookDefense::RookDefense
RookDefense()
Definition: rating/feature.h:197
osl::rating::Block
Definition: rating/feature.h:86
osl::Move::isDrop
bool isDrop() const
Definition: basic_type.h:1150
osl::Move::ptypeO
PtypeO ptypeO() const
移動後のPtype, i.e., 成る手だった場合成った後
Definition: basic_type.h:1162
osl::PAWN
@ PAWN
Definition: basic_type.h:95
osl::rating::RatingEnv::sendoffs
Square8 sendoffs
Definition: ratingEnv.h:19
osl::container::Square8::isMember
bool isMember(Square position) const
Definition: square8.h:22
osl::rating::Check::openLong
static bool openLong(const NumEffectState &state, Move move)
Definition: rating/feature.h:61
osl::rating::RatingEnv::history
MoveStack history
Definition: ratingEnv.h:18
osl::rating::SendOff::match
bool match(const NumEffectState &, Move move, const RatingEnv &env) const
Definition: rating/feature.h:79
osl::rating::Chase::self
Ptype self
Definition: rating/feature.h:144
osl::rating::Block::Block
Block(int s, int o)
Definition: rating/feature.h:90
osl::rating::BadLance::match
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
Definition: rating/feature.h:232
osl::rating::Chase::target
Ptype target
Definition: rating/feature.h:144
osl::Move::isNormal
bool isNormal() const
INVALID でも PASS でもない.
Definition: basic_type.h:1088
osl::Piece::owner
Player owner() const
Definition: basic_type.h:963
osl::Move::from
const Square from() const
Definition: basic_type.h:1125
osl::rating::Feature::match
virtual bool match(const NumEffectState &state, Move, const RatingEnv &) const =0
osl::SimpleState::hasPieceOnStand
bool hasPieceOnStand(Player player, Ptype ptype) const
Definition: simpleState.h:191
osl::Square::x
int x() const
将棋としてのX座標を返す.
Definition: basic_type.h:563
osl::NumEffectState
利きを持つ局面
Definition: numEffectState.h:34
osl::rating::Feature::Feature
Feature(const std::string &name)
Definition: rating/feature.h:18
osl::rating::PawnAttack::PawnAttack
PawnAttack()
Definition: rating/feature.h:243
osl::ROOK
@ ROOK
Definition: basic_type.h:100
osl::BoardTable::nextSquare
const Square nextSquare(Player P, Square pos, Direction dr) const
next position from pos for player P.
Definition: boardTable.h:61
osl::rating::Chase::OpponentType
OpponentType
Definition: rating/feature.h:142
osl::rating::BadLance::basicMatch
static bool basicMatch(const NumEffectState &state, Move move, Square front)
Definition: rating/feature.h:224
osl::Square::y
int y() const
将棋としてのY座標を返す.
Definition: basic_type.h:567
osl::sign
constexpr int sign(Player player)
Definition: basic_type.h:23
osl::rating::ImmediateAddSupport::Test
Definition: feature.cc:44
osl::rating::BadLance::BadLance
BadLance(bool h)
Definition: rating/feature.h:221
osl::Piece::square
const Square square() const
Definition: basic_type.h:832
osl::rating::BadLance::has_effect
bool has_effect
Definition: rating/feature.h:219
osl::rating::Chase::drop
bool drop
Definition: rating/feature.h:145
osl::Piece::isEmpty
bool isEmpty() const
Definition: basic_type.h:913
osl::Move::ptype
Ptype ptype() const
Definition: basic_type.h:1155
osl::rating::RookDefense
Definition: rating/feature.h:195
osl::rating::SendOff
Definition: rating/feature.h:75
osl::rating::ImmediateAddSupport::match
bool match(const NumEffectState &state, Move move, const RatingEnv &env) const
Definition: feature.cc:70
osl::rating::ImmediateAddSupport::effectiveInCheck
bool effectiveInCheck() const
Definition: rating/feature.h:190
osl::rating::TakeBack2
Definition: rating/feature.h:41
osl::rating::PawnAttack
Definition: rating/feature.h:241
osl::rating::Chase::OTHER
@ OTHER
Definition: rating/feature.h:142
osl::rating::Open::index
static int index(const NumEffectState &state, Move move)
Definition: rating/feature.h:124
osl::rating::TakeBack::match
bool match(const NumEffectState &, Move move, const RatingEnv &env) const
Definition: rating/feature.h:33
osl::rating::PawnAttack::match
bool match(const NumEffectState &, Move move, const RatingEnv &env) const
Definition: rating/feature.h:246
osl::rating::Chase::CAPTURE
@ CAPTURE
Definition: rating/feature.h:142
osl::rating::Check::check_property
static const CArray< const char *, 4 > check_property
Definition: rating/feature.h:70
osl::rating::Chase::DROP
@ DROP
Definition: rating/feature.h:142
osl::PTYPE_EMPTY
@ PTYPE_EMPTY
Definition: basic_type.h:85
osl::SimpleState::turn
Player turn() const
Definition: simpleState.h:220
osl::rating::ImmediateAddSupport
Definition: rating/feature.h:184
osl::rating::RookDefense::match
bool match(const NumEffectState &state, Move move, const RatingEnv &env) const
Definition: rating/feature.h:200
osl::Player
Player
Definition: basic_type.h:8
numEffectState.h
osl::NumEffectState::hasEffectByPtype
bool hasEffectByPtype(Player attack, Square target) const
target に ptype の利きがあるか? 成不成を区別しない
Definition: numEffectState.h:355
osl::rating::Chase::ESCAPE
@ ESCAPE
Definition: rating/feature.h:142
osl::rating::Open::property
int property
Definition: rating/feature.h:121
osl::rating::Open::effectiveInCheck
bool effectiveInCheck() const
Definition: rating/feature.h:136
osl::CArray< const char *, 4 >
osl::Move::to
const Square to() const
Definition: basic_type.h:1132
osl::container::MoveStack::hasLastMove
bool hasLastMove(size_t last=1) const
Definition: moveStack.h:27
osl::rating::ImmediateAddSupport::attack
Ptype attack
Definition: rating/feature.h:186
osl::rating::ImmediateAddSupport::ImmediateAddSupport
ImmediateAddSupport(Ptype self, Ptype attack)
Definition: feature.cc:63
osl::rating::TakeBack::TakeBack
TakeBack()
Definition: rating/feature.h:30
osl::U
@ U
Definition: basic_type.h:314
osl::rating::TakeBack2::match
bool match(const NumEffectState &, Move move, const RatingEnv &env) const
Definition: rating/feature.h:46
osl::rating::Chase::match
bool match(const NumEffectState &state, Move move, const RatingEnv &env) const
Definition: rating/feature.h:150
osl::SimpleState::isPawnMaskSet
bool isPawnMaskSet(Player player, int x) const
Definition: simpleState.h:146
osl::rating::Check::match
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
Definition: feature.cc:16
osl::Move::player
Player player() const
Definition: basic_type.h:1195
osl
Definition: additionalEffect.h:6