All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
rating/feature/square.h
Go to the documentation of this file.
1 /* square.h
2  */
3 #ifndef FEATURE_POSITION_H
4 #define FEATURE_POSITION_H
5 
6 #include "osl/rating/feature.h"
7 #include "osl/move.h"
8 #include "osl/ptypeTable.h"
9 #include <string>
10 
11 namespace osl
12 {
13  namespace rating
14  {
15  struct DropPtype
16  {
18  bool drop;
19  DropPtype(Ptype p, bool d) : ptype(p), drop(d)
20  {
21  }
22  bool match(Move m) const
23  {
24  return (m.ptype() == ptype) && (m.isDrop() == drop);
25  }
26  static std::string name(Ptype ptype, bool drop)
27  {
28  return std::string(Ptype_Table.getCsaName(ptype)) + (drop ? "d" : "m");
29  }
31  static int index(Move move)
32  {
33  const Ptype ptype = move.ptype();
34  int index;
35  if (! isBasic(ptype) || ptype == KING)
36  index = ptype - PTYPE_PIECE_MIN;
37  else
38  index = (PTYPE_BASIC_MIN+1-PTYPE_PIECE_MIN) + (ptype - (PTYPE_BASIC_MIN+1))*2 + move.isDrop();
39  return index;
40  }
41  };
42 
43  class RelativeKingX : public Feature
44  {
45  int x, old_x;
47  bool attack;
48  public:
49  static const std::string name(int x, int old_x, bool /*attack*/, Ptype);
50  RelativeKingX(int ix, int iox, bool a, Ptype p)
51  : Feature(name(ix,iox,a,p)), x(ix), old_x(iox), ptype(p), attack(a) {}
52  // [0,8]
53  static int makeX(bool attack, const NumEffectState& state, Move move)
54  {
55  const Square king = state.kingSquare(attack ? alt(move.player()) : move.player());
56  return abs(move.to().x() - king.x());
57  }
58  // [0,9]
59  static int makeOldX(bool attack, const NumEffectState& state, Move move)
60  {
61  if (move.isDrop())
62  return 9;
63  const Square king = state.kingSquare(attack ? alt(move.player()) : move.player());
64  return abs(move.from().x() - king.x());
65  }
66  bool match(const NumEffectState& state, Move move, const RatingEnv&) const
67  {
68  if (ptype != move.ptype())
69  return false;
70  return makeX(attack, state, move) == x && makeOldX(attack, state, move) == old_x;
71  }
72  static int index(bool attack, const NumEffectState& state, Move move)
73  {
74  const int x = makeX(attack, state, move), old_x = makeOldX(attack, state, move);
75  const int ptype = move.ptype() - PTYPE_PIECE_MIN;
76  return (x*10+old_x)*(PTYPE_MAX+1 - PTYPE_PIECE_MIN) + ptype;
77  }
78  };
79 
80  class RelativeKingY : public Feature
81  {
82  int y, old_y;
84  bool attack;
85  public:
86  static const std::string name(int y, int old_y, bool /*attack*/, Ptype ptype);
87  RelativeKingY(int iy, int ioy, bool a, Ptype p)
88  : Feature(name(iy,ioy,a,p)), y(iy), old_y(ioy), ptype(p), attack(a) {}
89  // [-8,8]
90  static int makeY(bool attack, const NumEffectState& state, Move move)
91  {
92  const Square king = state.kingSquare(attack ? alt(move.player()) : move.player());
93  int diff = move.to().y() - king.y();
94  if (move.player() == WHITE)
95  diff = -diff;
96  return diff;
97  }
98  // [-8,9]
99  static int makeOldY(bool attack, const NumEffectState& state, Move move)
100  {
101  if (move.isDrop())
102  return 9;
103  const Square king = state.kingSquare(attack ? alt(move.player()) : move.player());
104  int diff = move.from().y() - king.y();
105  if (move.player() == WHITE)
106  diff = -diff;
107  return diff;
108  }
109  bool match(const NumEffectState& state, Move move, const RatingEnv&) const
110  {
111  if (ptype != move.ptype())
112  return false;
113  return makeY(attack, state, move) == y && makeOldY(attack, state, move) == old_y;
114  }
115  static int index(bool attack, const NumEffectState& state, Move move)
116  {
117  const int y = makeY(attack, state, move) + 8;
118  const int old_y = makeOldY(attack, state, move) + 8;
119  const int ptype = move.ptype() - PTYPE_PIECE_MIN;
120  return (y*18+old_y)*(PTYPE_MAX+1 - PTYPE_PIECE_MIN) + ptype;
121  }
122  };
123 
124  class SquareX : public Feature, DropPtype
125  {
126  int x;
127  static const std::string name(int x);
128  public:
129  SquareX(int ix, Ptype ptype, bool drop)
130  : Feature(name(ix)+DropPtype::name(ptype, drop)), DropPtype(ptype, drop), x(ix) {}
131  static int makeX(Move move)
132  {
133  int mx = move.to().x();
134  if (mx > 5)
135  mx = 10-mx;
136  return mx;
137  }
138  bool match(const NumEffectState& , Move move, const RatingEnv&) const
139  {
140  if (! DropPtype::match(move))
141  return false;
142  return x == makeX(move);
143  }
144  };
145 
146  class SquareY : public Feature, DropPtype
147  {
148  int y;
149  static const std::string name(int y);
150  public:
151  SquareY(int iy, Ptype ptype, bool drop)
152  : Feature(name(iy)+DropPtype::name(ptype, drop)), DropPtype(ptype, drop), y(iy) {}
153  static int makeY(Move move)
154  {
155  int my = move.to().y();
156  if (move.player() == WHITE)
157  my = 10 - my;
158  return my;
159  }
160  bool match(const NumEffectState&, Move move, const RatingEnv&) const
161  {
162  if (! DropPtype::match(move))
163  return false;
164  return y == makeY(move);
165  }
166  };
167  }
168 }
169 
170 #endif /* FEATURE_POSITION_H */
171 // ;;; Local Variables:
172 // ;;; mode:c++
173 // ;;; c-basic-offset:2
174 // ;;; End: