bigramAttack.h Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
bigramAttack.h
Go to the documentation of this file.
1 /* bigramAttack.h
2 */
3 #ifndef _BIGRAMATTACK_H
4 #define _BIGRAMATTACK_H
5
6 #include "osl/rating/feature.h"
7
8 namespace osl
9 {
10 namespace rating
11 {
12 class BigramAttack : public Feature
13 {
14 int property;
15 bool same, focus_x;
16 public:
17 static const std::string name(int x1, int y1, int x2, int y2, int king_index, bool s, bool f);
18 BigramAttack(int x1, int y1, int x2, int y2, int king_index, bool s, bool f)
19 : Feature(name(x1,y1,x2,y2,king_index,s,f)), property((((x1+2)*5+y1+2)*25 + (x2+2)*5+y2+2)*5 + king_index), same(s), focus_x(f)
20 {
21 }
22 // [0,4]
23 static int indexKing(Player attack, Square king, bool focus_x)
24 {
25 int x = focus_x ? king.x() : king.y();
26 if (! focus_x && attack == WHITE)
27 x = 10 - x;
28 if (x <= 3)
29 return x-1;
30 if (x >= 7)
31 return x-5;
32 return 2;
33 }
34 static int indexOfMove(Square king, Move move)
35 {
36 int x_diff = move.to().x() - king.x();
37 if (abs(x_diff) > 2)
38 return -1;
39 x_diff += 2;
40 assert(x_diff >= 0 && x_diff <= 4);
41 int y_diff = move.to().y() - king.y();
42 if (abs(y_diff) > 2)
43 return -1;
44 if (move.player() == WHITE)
45 y_diff = -y_diff;
46 y_diff += 2;
47 assert(y_diff >= 0 && y_diff <= 4);
48 return x_diff * 5 + y_diff;
49 }
50 static int index(const NumEffectState& state, Move move, const RatingEnv& env, bool same, bool focus_x)
51 {
52 if (! env.history.hasLastMove(same+1))
53 return -1;
54 const Move prev = env.history.lastMove(same+1);
55 if (! prev.isNormal())
56 return -1;
57 const Square king = state.kingSquare(alt(state.turn()));
58 const int index1 = indexOfMove(king, prev);
59 if (index1 < 0)
60 return -1;
61 const int index2 = indexOfMove(king, move);
62 if (index2 < 0)
63 return -1;
64 return (index1 * 25 + index2) * 5 + indexKing(move.player(), king, focus_x);
65 }
66 bool match(const NumEffectState& state, Move move, const RatingEnv& env) const
67 {
68 int index = this->index(state, move, env, same, focus_x);
69 return index == property;
70 }
71 };
72 }
73 }
74
75 #endif /* _BIGRAMATTACK_H */
76 // ;;; Local Variables:
77 // ;;; mode:c++
78 // ;;; c-basic-offset:2
79 // ;;; End: