00001
00002
00003 #ifndef _BIGRAMATTACK_H
00004 #define _BIGRAMATTACK_H
00005
00006 #include "osl/rating/feature.h"
00007 #include "osl/category/bigramPosition.h"
00008
00009 namespace osl
00010 {
00011 namespace rating
00012 {
00013 class BigramAttack : public Feature
00014 {
00015 int property;
00016 bool same, focus_x;
00017 public:
00018 static const std::string name(int x1, int y1, int x2, int y2, int king_index, bool s, bool f);
00019 BigramAttack(int x1, int y1, int x2, int y2, int king_index, bool s, bool f)
00020 : 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)
00021 {
00022 }
00023
00024 static int indexKing(Player attack, Position king, bool focus_x)
00025 {
00026 int x = focus_x ? king.x() : king.y();
00027 if (! focus_x && attack == WHITE)
00028 x = 10 - x;
00029 if (x <= 3)
00030 return x-1;
00031 if (x >= 7)
00032 return x-5;
00033 return 2;
00034 }
00035 static int indexOfMove(Position king, Move move)
00036 {
00037 int x_diff = move.to().x() - king.x();
00038 if (abs(x_diff) > 2)
00039 return -1;
00040 x_diff += 2;
00041 assert(x_diff >= 0 && x_diff <= 4);
00042 int y_diff = move.to().y() - king.y();
00043 if (abs(y_diff) > 2)
00044 return -1;
00045 if (move.player() == WHITE)
00046 y_diff = -y_diff;
00047 y_diff += 2;
00048 assert(y_diff >= 0 && y_diff <= 4);
00049 return x_diff * 5 + y_diff;
00050 }
00051 static int index(const NumEffectState& state, Move move, const RatingEnv& env, bool same, bool focus_x)
00052 {
00053 if (! env.history.hasLastMove(same+1))
00054 return -1;
00055 const Move prev = env.history.lastMove(same+1);
00056 if (! prev.isNormal())
00057 return -1;
00058 const Position king = state.getKingPosition(alt(state.getTurn()));
00059 const int index1 = indexOfMove(king, prev);
00060 if (index1 < 0)
00061 return -1;
00062 const int index2 = indexOfMove(king, move);
00063 if (index2 < 0)
00064 return -1;
00065 return (index1 * 25 + index2) * 5 + indexKing(move.player(), king, focus_x);
00066 }
00067 bool match(const NumEffectState& state, Move move, const RatingEnv& env) const
00068 {
00069 int index = this->index(state, move, env, same, focus_x);
00070 return index == property;
00071 }
00072 };
00073
00074 class BigramAttackTable : public Feature
00075 {
00076 int first, last;
00077 public:
00078 static const std::string name(int first);
00079 BigramAttackTable(int f, int l) : Feature(name(f)), first(f), last(l)
00080 {
00081 }
00082 bool match(const NumEffectState& state, Move move, const RatingEnv& env) const
00083 {
00084 category::CategoryEnv e(&state, last, &env.history);
00085 const int prob = category::BigramAttack::probability(e, move);
00086 return first <= prob && prob < last;
00087 }
00088 };
00089 }
00090 }
00091
00092 #endif
00093
00094
00095
00096