00001
00002
00003
00004 #ifndef EVAL_ML_KING8_H
00005 #define EVAL_ML_KING8_H
00006
00007 #include "osl/eval/ml/weights.h"
00008 #include "osl/eval/ml/evalStagePair.h"
00009 #include "osl/ptype.h"
00010 #include "osl/misc/carray.h"
00011 #include "osl/state/numEffectState.h"
00012 #include "osl/container/tripleInt.h"
00013
00014 namespace osl
00015 {
00016 namespace eval
00017 {
00018 namespace ml
00019 {
00020 class King8Effect
00021 {
00022 public:
00023 enum { DIM = 32 + 32 + 288 + 288 };
00024 static void setUp(const Weights &weights);
00025 static int eval(const osl::state::NumEffectState &state);
00026 private:
00027 enum EffectState
00028 {
00029 NOT_EMPTY = -1,
00030 NO_EFFECT = 0,
00031 LESS_EFFECT,
00032 MORE_EFFECT,
00033 MORE_EFFECT_KING_ONLY
00034 };
00035 static CArray<int, 32> empty_table;
00036 static CArray<int, 32> defense_table;
00037 static CArray<int, 288> empty_y_table;
00038 static CArray<int, 288> defense_y_table;
00039 static int index(const Direction dir,
00040 EffectState state);
00041 static int indexY(Piece king,
00042 const Direction dir,
00043 EffectState state);
00044 static void effectState(const NumEffectState &state,
00045 const Player defense,
00046 const Direction dir,
00047 EffectState &empty,
00048 EffectState &);
00049 };
00050
00051
00052 class King8EffectBase
00053 {
00054 public:
00055 enum { DIM = 32 };
00056 typedef CArray<int, 32> table_t;
00057 enum EffectState
00058 {
00059 NOT_EMPTY = -1,
00060 NO_EFFECT = 0,
00061 LESS_EFFECT,
00062 MORE_EFFECT,
00063 MORE_EFFECT_KING_ONLY
00064 };
00065 template <class MakeEffectState>
00066 static const CArray<int,2> evalCommon(const osl::state::NumEffectState &state, const table_t&);
00067 template <class MakeEffectState>
00068 static const CArray<int,2> evalWithUpdateCommon(const osl::state::NumEffectState &new_state, Move last_move,
00069 const CArray<int,2>& last_value, const table_t&);
00070
00071 template <class MakeEffectState>
00072 static std::pair<CArray<int,2>, CArray<int,2> >
00073 evalWithUpdateCommon(const NumEffectState &new_state, Move last_move,
00074 const CArray<int,2>& last_value_opening, const CArray<int,2>& last_value_ending,
00075 const table_t&, const table_t&);
00076
00077 struct MakeEffectStateSimple;
00078 struct MakeEffectStateDefense;
00079 protected:
00080 static int index(const Direction dir, EffectState state)
00081 {
00082 return dir * 4 + state;
00083 }
00084 };
00085 class King8EffectEmptyPositionBoth;
00086 template <bool Opening>
00087 class King8EffectEmptyPosition : public King8EffectBase
00088 {
00089 friend class King8EffectEmptyPositionBoth;
00090 static table_t table;
00091 public:
00092 static void setUp(const Weights &weights);
00093 static const CArray<int,2> eval(const osl::state::NumEffectState &state);
00094 static const CArray<int,2> evalWithUpdate(const osl::state::NumEffectState &new_state, Move last_move,
00095 const CArray<int,2>& last_value);
00096 };
00097
00098 struct King8EffectEmptyPositionBoth : public King8EffectBase
00099 {
00100 static std::pair<CArray<int,2>, CArray<int,2> >
00101 evalWithUpdate(const osl::state::NumEffectState &new_state, Move last_move,
00102 const CArray<int,2>& last_value_opening,
00103 const CArray<int,2>& last_value_ending);
00104 };
00105 struct King8EffectEmptyPositionOpening
00106 : public King8EffectEmptyPosition<true>
00107 {
00108 };
00109 struct King8EffectEmptyPositionEnding
00110 : public King8EffectEmptyPosition<false>
00111 {
00112 };
00113
00114 class King8EffectDefensePositionBoth;
00115 template <bool Opening>
00116 class King8EffectDefensePosition
00117 : public King8EffectBase
00118 {
00119 friend class King8EffectDefensePositionBoth;
00120 static CArray<int, 32> table;
00121 public:
00122 static void setUp(const Weights &weights);
00123 static const CArray<int,2> eval(const osl::state::NumEffectState &state);
00124 static const CArray<int,2> evalWithUpdate(const osl::state::NumEffectState &new_state, Move last_move,
00125 const CArray<int,2>& last_value);
00126 };
00127 struct King8EffectDefensePositionBoth : public King8EffectBase
00128 {
00129 static std::pair<CArray<int,2>, CArray<int,2> >
00130 evalWithUpdate(const osl::state::NumEffectState &new_state, Move last_move,
00131 const CArray<int,2>& last_value_opening,
00132 const CArray<int,2>& last_value_ending);
00133 };
00134
00135 struct King8EffectDefensePositionOpening
00136 : public King8EffectDefensePosition<true>
00137 {
00138 };
00139 struct King8EffectDefensePositionEnding
00140 : public King8EffectDefensePosition<false>
00141 {
00142 };
00143
00144 class King8EffectAll
00145 {
00146 public:
00147 enum { ONE_DIM = 32, DIM = 32 * 5 * 2};
00148 private:
00149 static CArray<int, ONE_DIM> base_table;
00150 static CArray<int, ONE_DIM> u_table;
00151 static CArray<int, ONE_DIM> d_table;
00152 static CArray<int, ONE_DIM> l_table;
00153 static CArray<int, ONE_DIM> r_table;
00154 static CArray<int, ONE_DIM> base_defense_piece_table;
00155 static CArray<int, ONE_DIM> u_defense_piece_table;
00156 static CArray<int, ONE_DIM> d_defense_piece_table;
00157 static CArray<int, ONE_DIM> l_defense_piece_table;
00158 static CArray<int, ONE_DIM> r_defense_piece_table;
00159 public:
00160 enum EffectState
00161 {
00162 NOT_EMPTY = -1,
00163 NO_EFFECT = 0,
00164 LESS_EFFECT,
00165 MORE_EFFECT,
00166 MORE_EFFECT_KING_ONLY
00167 };
00168 static void setUp(const Weights &weights);
00169 King8EffectAll() { }
00170 static int eval(const osl::state::NumEffectState &state,
00171 PieceMask black_mask, PieceMask white_mask);
00172 static void effectState(const NumEffectState &state,
00173 const Player defense,
00174 const Direction dir,
00175 EffectState &empty,
00176 EffectState &);
00177 static int index(const Direction dir, EffectState state);
00178 };
00179
00180 struct KingXBlockedBase
00181 {
00182 enum { DIM = 10 };
00183 typedef CArray<MultiInt, 10> table_t;
00184 static const MultiIntPair eval(const NumEffectState &state,
00185 const table_t& table);
00186 template <osl::Player P>
00187 static int index(Position king, int diff);
00188 template <osl::Player P>
00189 static bool isBlocked(const NumEffectState &state,
00190 int diff);
00191 #if 0
00192 static std::pair<CArray<int,2>,CArray<int,2> >
00193 evalWithUpdate(const NumEffectState &new_state, Move last_move,
00194 const CArray<int,2>& last_value_o,
00195 const CArray<int,2>& last_value_e,
00196 const table_t& table_o, const table_t& table_e);
00197 #endif
00198 };
00199 class KingXBlockedBoth;
00200
00201 class KingXBlocked : public KingXBlockedBase
00202 {
00203 friend class KingXBlockedBoth;
00204 friend class KingXBlockedYBase;
00205 public:
00206 static void setUp(const Weights &weights,int stage);
00207 static MultiIntPair eval(const osl::state::NumEffectState &state)
00208 {
00209 return KingXBlockedBase::eval(state, table);
00210 }
00211 private:
00212 static table_t table;
00213 };
00214
00215 struct KingXBlockedYBase
00216 {
00217 enum { DIM = 90 };
00218 typedef CArray<MultiInt, 90> table_t;
00219 static const MultiIntPair eval(const NumEffectState &state,
00220 const table_t& table);
00221 static void
00222 evalWithUpdateBang(const NumEffectState &state, Move laste_move,
00223 MultiIntPair &last_values_and_out);
00224 template <osl::Player P>
00225 static int index(Position king, int diff);
00226 private:
00227 template <int Sign>
00228 static void adjust(int index, int index_y, MultiInt &out);
00229 };
00230
00231 class KingXBlockedY : public KingXBlockedYBase
00232 {
00233 friend class KingXBlockedBoth;
00234 friend class KingXBlockedYBase;
00235 public:
00236 static void setUp(const Weights &weights,int stage);
00237 static const MultiIntPair eval(const NumEffectState &state)
00238 {
00239 return KingXBlockedYBase::eval(state, table);
00240 }
00241 private:
00242 static table_t table;
00243 };
00244
00245 struct KingXBlockedBoth : public KingXBlockedBase
00246 {
00247 static void
00248 evalWithUpdateBang(const NumEffectState &new_state, Move last_move,
00249 MultiIntPair& last_values_and_out);
00250 };
00251
00252 class KingXBothBlocked
00253 {
00254 friend class KingXBlockedYBase;
00255 public:
00256 enum { ONE_DIM = 5, DIM = ONE_DIM * EvalStages };
00257 static void setUp(const Weights &weights);
00258 static MultiIntPair eval(const NumEffectState &state);
00259 private:
00260 static CArray<MultiInt, ONE_DIM> table;
00261 static int index(const Position king)
00262 {
00263 const int x = king.x();
00264 return (x > 5 ? 9 - x : x - 1);;
00265 }
00266 template <Player P>
00267 static int indexY(const Position king)
00268 {
00269 const int x = king.x();
00270 const int y = (P == BLACK ? king.y() : 10 - king.y());
00271 return (y - 1) * 5 + (x > 5 ? 9 - x : x - 1);
00272 }
00273 template <int Sign>
00274 static void adjust(int index, int index_y, MultiInt &out);
00275 };
00276
00277 class KingXBothBlockedY
00278 {
00279 friend class KingXBlockedYBase;
00280 friend class KingXBothBlocked;
00281 public:
00282 enum { ONE_DIM = 5 * 9, DIM = ONE_DIM * EvalStages };
00283 static void setUp(const Weights &weights);
00284 private:
00285 static CArray<MultiInt, ONE_DIM> table;
00286 };
00287
00288 class KingXBlocked3
00289 {
00290 friend class KingXBlocked3Y;
00291 public:
00292 enum { ONE_DIM = 80, DIM = ONE_DIM * EvalStages };
00293 static MultiInt eval(const NumEffectState &state);
00294 static void setUp(const Weights &weights);
00295 private:
00296 template <int Sign>
00297 static void adjust(int index_y, MultiInt &result)
00298 {
00299 if(Sign>0)
00300 result += y_table[index_y];
00301 else
00302 result -= y_table[index_y];
00303 }
00304 template <Player P>
00305 static int index(const Position king, bool is_l,
00306 bool u_blocked, bool opp_u_blocked, bool opp_blocked)
00307 {
00308 int x = king.x();
00309 if (x >= 6)
00310 {
00311 x = 10 - x;
00312 if (P == BLACK)
00313 {
00314 is_l = !is_l;
00315 }
00316 }
00317 else if (P == WHITE && x <= 4)
00318 {
00319 is_l = !is_l;
00320 }
00321 return x - 1 + 5 * ((is_l ? 1 : 0) + 2 * ((u_blocked ? 1 : 0) + 2 * ((opp_u_blocked ? 1 : 0) +2 * (opp_blocked ? 1 : 0))));
00322 }
00323 template <Player P>
00324 static int indexY(const Position king, bool is_l,
00325 bool u_blocked, bool opp_u_blocked,
00326 bool opp_blocked)
00327 {
00328 int x = king.x();
00329 const int y = (P == BLACK ? king.y() : 10 - king.y());
00330 if (x >= 6)
00331 {
00332 x = 10 - x;
00333 if (P == BLACK)
00334 {
00335 is_l = !is_l;
00336 }
00337 }
00338 else if (P == WHITE && x <= 4)
00339 {
00340 is_l = !is_l;
00341 }
00342 return x - 1 + 5 * (y - 1 + 9 * ((is_l ? 1 : 0) + 2 * ((u_blocked ? 1 : 0) + 2 * ((opp_u_blocked ? 1 : 0) + 2 * (opp_blocked ? 1 : 0)))));
00343 }
00344 static CArray<MultiInt, 80> table;
00345 static CArray<MultiInt, 720> y_table;
00346 };
00347
00348 class KingXBlocked3Y
00349 {
00350 public:
00351 enum { ONE_DIM = 720, DIM = ONE_DIM * EvalStages };
00352 static void setUp(const Weights &weights);
00353 };
00354
00355 class AnagumaEmpty
00356 {
00357 public:
00358 enum { DIM = 4 };
00359 static void setUp(const Weights &weights,int stage);
00360 static MultiInt eval(const osl::state::NumEffectState &state);
00361 static int index(Position king, Position target);
00362 template <osl::Player Defense>
00363 static MultiInt evalOne(const NumEffectState &state);
00364 private:
00365 static CArray<MultiInt, 4> table;
00366 };
00367 }
00368 }
00369 }
00370
00371 #endif // EVAL_ML_KING8_H
00372
00373
00374
00375