bigramAttack.h
Go to the documentation of this file.
00001 /* bigramAttack.h
00002  */
00003 #ifndef _BIGRAMATTACK_H
00004 #define _BIGRAMATTACK_H
00005 
00006 #include "osl/rating/feature.h"
00007 
00008 namespace osl
00009 {
00010   namespace rating
00011   {
00012     class BigramAttack : public Feature
00013     {
00014       int property;
00015       bool same, focus_x;
00016     public:
00017       static const std::string name(int x1, int y1, int x2, int y2, int king_index, bool s, bool f);
00018       BigramAttack(int x1, int y1, int x2, int y2, int king_index, bool s, bool f) 
00019         : 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)
00020       {
00021       }
00022       // [0,4]
00023       static int indexKing(Player attack, Square king, bool focus_x)
00024       {
00025         int x = focus_x ? king.x() : king.y();
00026         if (! focus_x && attack == WHITE)
00027           x = 10 - x;
00028         if (x <= 3)
00029           return x-1;
00030         if (x >= 7)
00031           return x-5;
00032         return 2;
00033       }
00034       static int indexOfMove(Square king, Move move)
00035       {
00036         int x_diff = move.to().x() - king.x();
00037         if (abs(x_diff) > 2)
00038           return -1;
00039         x_diff += 2;
00040         assert(x_diff >= 0 && x_diff <= 4);
00041         int y_diff = move.to().y() - king.y();
00042         if (abs(y_diff) > 2)
00043           return -1;
00044         if (move.player() == WHITE)
00045           y_diff = -y_diff;
00046         y_diff += 2;
00047         assert(y_diff >= 0 && y_diff <= 4);
00048         return x_diff * 5 + y_diff;
00049       }
00050       static int index(const NumEffectState& state, Move move, const RatingEnv& env, bool same, bool focus_x) 
00051       {
00052         if (! env.history.hasLastMove(same+1))
00053           return -1;
00054         const Move prev = env.history.lastMove(same+1);
00055         if (! prev.isNormal())
00056           return -1;
00057         const Square king = state.kingSquare(alt(state.turn()));
00058         const int index1 = indexOfMove(king, prev);
00059         if (index1 < 0)
00060           return -1;
00061         const int index2 = indexOfMove(king, move);
00062         if (index2 < 0)
00063           return -1;
00064         return (index1 * 25 + index2) * 5 + indexKing(move.player(), king, focus_x);
00065       }
00066       bool match(const NumEffectState& state, Move move, const RatingEnv& env) const
00067       {
00068         int index = this->index(state, move, env, same, focus_x);
00069         return index == property;
00070       }
00071     };
00072   }
00073 }
00074 
00075 #endif /* _BIGRAMATTACK_H */
00076 // ;;; Local Variables:
00077 // ;;; mode:c++
00078 // ;;; c-basic-offset:2
00079 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines