All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
rating/feature.cc
Go to the documentation of this file.
1 /* feature.cc
2  */
3 #include "osl/rating/feature.h"
5 #include <sstream>
6 
8 {
9 }
10 
11 const osl::CArray<const char*,4> osl::rating::Check::check_property = {{ "Di", "DO", "OC", "Bo" }};
12 osl::rating::Check::Check(int p) : Feature(check_property[p]), property(p)
13 {
14 }
15 
16 bool osl::rating::Check::match(const NumEffectState& state, Move move, const RatingEnv&) const
17 {
18  using namespace osl::move_classifier;
19  if (property == 0 || property == 1) {
22  return false;
23  return property == openLong(state, move);
24  }
25  if (property == 2)
30 }
31 
32 const std::string osl::rating::
33 Block::name(int self, int opponent)
34 {
35  std::ostringstream os;
36  os << "B" << self << opponent;
37  return os.str();
38 }
39 
40 const std::string osl::rating::Open::name(int property)
41 {
42  std::ostringstream os;
43  os << "Open" << property / 4 << property % 4;
44  return os.str();
45 }
46 
48 {
49  bool *result;
50  const NumEffectState& state;
52  Test(bool *r, const NumEffectState& s, Move m)
53  : result(r), state(s), move(m)
54  {
55  }
56  template <Player P>
57  void doAction(Piece /*piece*/, Square last_attack)
58  {
59  const Piece attacked = state.pieceAt(last_attack);
60  if (attacked.isOnBoardByOwner(state.turn())
61  && state.hasEffectIf(move.ptypeO(), move.to(), last_attack))
62  *result = true;
63  }
64 };
65 
68  : Feature(std::string(Ptype_Table.getCsaName(s))+":"+Ptype_Table.getCsaName(a)),
69  self(s), attack(a)
70 {
71 }
72 
73 bool osl::rating::
74 ImmediateAddSupport::match(const NumEffectState& state, Move move, const RatingEnv& env) const
75 {
76  if (move.ptype() != self)
77  return false;
78  const Move last_move=env.history.lastMove();
79  if (! last_move.isNormal())
80  return false;
81  if (last_move.ptype() != attack)
82  return false;
83  const Square last_to = last_move.to();
84  if (last_to==move.to())
85  return false; // TakeBack は除く
86  bool result = false;
87  Test action(&result, state, move);
88  state.forEachEffectOfPiece(state.pieceOnBoard(last_to), action);
89  return result;
90 }
91 
92 int osl::rating::
93 ImmediateAddSupport::index(const NumEffectState& state, Move move, const RatingEnv& env)
94 {
95  const Move last_move=env.history.lastMove();
96  if (! last_move.isNormal())
97  return -1;
98  const Square last_to = last_move.to();
99  if (last_to==move.to())
100  return -1; // TakeBack は除く
101  bool result = false;
102  const Piece last_piece = state.pieceOnBoard(last_to);
103  // BoardMask state.changedEffects(alt(Turn))?
104  if (! Ptype_Table.hasLongMove(last_piece.ptype()))
105  {
106  Test action(&result, state, move);
107  state.forEachEffectOfPiece(last_piece, action);
108  }
109  else
110  {
111  const Player Turn = state.turn();
112  PieceMask pieces = state.piecesOnBoard(Turn) & state.effectedMask(alt(Turn));
113  mask_t m = pieces.getMask(0);
114  while (m.any()) {
115  const Piece p = state.pieceOf(m.takeOneBit());
116  if (state.hasEffectByPiece(last_piece, p.square())
117  && state.hasEffectIf(move.ptypeO(), move.to(), p.square())) {
118  result = true;
119  break;
120  }
121  }
122 #if OSL_WORDSIZE == 32
123  if (! result) {
124  m = pieces.getMask(1);
125  while (m.any()) {
126  const Piece p = state.pieceOf(m.takeOneBit()+32);
127  if (state.hasEffectByPiece(last_piece, p.square())
128  && state.hasEffectIf(move.ptypeO(), move.to(), p.square())) {
129  result = true;
130  break;
131  }
132  }
133  }
134 #endif
135  }
136  if (! result)
137  return -1;
138  return (move.ptype() - PTYPE_PIECE_MIN) * (PTYPE_MAX+1 - PTYPE_PIECE_MIN)
139  + last_move.ptype() - PTYPE_PIECE_MIN;
140 }
141 
142 const std::string osl::rating::Chase::name(Ptype self, Ptype target, bool drop, OpponentType opponent_type)
143 {
144  return std::string(Ptype_Table.getCsaName(self))
145  +(drop ? "d" : "m")+">"+Ptype_Table.getCsaName(target)
146  +(opponent_type == CAPTURE ? "c" : (opponent_type == DROP ? "d" : "e"));
147 }
148 
149 /* ------------------------------------------------------------------------- */
150 // ;;; Local Variables:
151 // ;;; mode:c++
152 // ;;; c-basic-offset:2
153 // ;;; End: