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 King8EffectEmptySquareBoth;
00086 template <bool Opening>
00087 class King8EffectEmptySquare : public King8EffectBase
00088 {
00089 friend class King8EffectEmptySquareBoth;
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 class King8EffectEmptySquareBoth : public King8EffectBase
00099 {
00100 public:
00101 static std::pair<CArray<int,2>, CArray<int,2> >
00102 evalWithUpdate(const osl::state::NumEffectState &new_state, Move last_move,
00103 const CArray<int,2>& last_value_opening,
00104 const CArray<int,2>& last_value_ending);
00105 };
00106 struct King8EffectEmptySquareOpening
00107 : public King8EffectEmptySquare<true>
00108 {
00109 };
00110 struct King8EffectEmptySquareEnding
00111 : public King8EffectEmptySquare<false>
00112 {
00113 };
00114
00115 class King8EffectDefenseSquareBoth;
00116 template <bool Opening>
00117 class King8EffectDefenseSquare
00118 : public King8EffectBase
00119 {
00120 friend class King8EffectDefenseSquareBoth;
00121 static CArray<int, 32> table;
00122 public:
00123 static void setUp(const Weights &weights);
00124 static const CArray<int,2> eval(const osl::state::NumEffectState &state);
00125 static const CArray<int,2> evalWithUpdate(const osl::state::NumEffectState &new_state, Move last_move,
00126 const CArray<int,2>& last_value);
00127 };
00128 class King8EffectDefenseSquareBoth : public King8EffectBase
00129 {
00130 public:
00131 static std::pair<CArray<int,2>, CArray<int,2> >
00132 evalWithUpdate(const osl::state::NumEffectState &new_state, Move last_move,
00133 const CArray<int,2>& last_value_opening,
00134 const CArray<int,2>& last_value_ending);
00135 };
00136
00137 struct King8EffectDefenseSquareOpening
00138 : public King8EffectDefenseSquare<true>
00139 {
00140 };
00141 struct King8EffectDefenseSquareEnding
00142 : public King8EffectDefenseSquare<false>
00143 {
00144 };
00145
00146 class King8EffectAll
00147 {
00148 public:
00149 enum { ONE_DIM = 32, DIM = 32 * 5 * 2};
00150 private:
00151 static CArray<int, ONE_DIM> base_table;
00152 static CArray<int, ONE_DIM> u_table;
00153 static CArray<int, ONE_DIM> d_table;
00154 static CArray<int, ONE_DIM> l_table;
00155 static CArray<int, ONE_DIM> r_table;
00156 static CArray<int, ONE_DIM> base_defense_piece_table;
00157 static CArray<int, ONE_DIM> u_defense_piece_table;
00158 static CArray<int, ONE_DIM> d_defense_piece_table;
00159 static CArray<int, ONE_DIM> l_defense_piece_table;
00160 static CArray<int, ONE_DIM> r_defense_piece_table;
00161 public:
00162 enum EffectState
00163 {
00164 NOT_EMPTY = -1,
00165 NO_EFFECT = 0,
00166 LESS_EFFECT,
00167 MORE_EFFECT,
00168 MORE_EFFECT_KING_ONLY
00169 };
00170 static void setUp(const Weights &weights);
00171 King8EffectAll() { }
00172 static int eval(const osl::state::NumEffectState &state,
00173 PieceMask black_mask, PieceMask white_mask);
00174 static void effectState(const NumEffectState &state,
00175 const Player defense,
00176 const Direction dir,
00177 EffectState &empty,
00178 EffectState &);
00179 static int index(const Direction dir, EffectState state);
00180 };
00181
00182 struct KingXBlockedBase
00183 {
00184 enum { DIM = 10 };
00185 typedef CArray<MultiInt, 10> table_t;
00186 static const MultiIntPair eval(const NumEffectState &state,
00187 const table_t& table);
00188 template <osl::Player P>
00189 static int index(Square king, int diff);
00190 template <osl::Player P>
00191 static bool isBlocked(const NumEffectState &state,
00192 int diff);
00193 #if 0
00194 static std::pair<CArray<int,2>,CArray<int,2> >
00195 evalWithUpdate(const NumEffectState &new_state, Move last_move,
00196 const CArray<int,2>& last_value_o,
00197 const CArray<int,2>& last_value_e,
00198 const table_t& table_o, const table_t& table_e);
00199 #endif
00200 };
00201 class KingXBlockedBoth;
00202
00203 class KingXBlocked : public KingXBlockedBase
00204 {
00205 friend class KingXBlockedBoth;
00206 friend class KingXBlockedYBase;
00207 public:
00208 static void setUp(const Weights &weights,int stage);
00209 static MultiIntPair eval(const osl::state::NumEffectState &state)
00210 {
00211 return KingXBlockedBase::eval(state, table);
00212 }
00213 private:
00214 static table_t table;
00215 };
00216
00217 class KingXBlockedYBase
00218 {
00219 public:
00220 enum { DIM = 90 };
00221 typedef CArray<MultiInt, 90> table_t;
00222 static const MultiIntPair eval(const NumEffectState &state,
00223 const table_t& table);
00224 static void
00225 evalWithUpdateBang(const NumEffectState &state, Move laste_move,
00226 MultiIntPair &last_values_and_out);
00227 template <osl::Player P>
00228 static int index(Square king, int diff);
00229 private:
00230 template <int Sign>
00231 static void adjust(int index, int index_y, MultiInt &out);
00232 };
00233
00234 class KingXBlockedY : public KingXBlockedYBase
00235 {
00236 friend class KingXBlockedBoth;
00237 friend class KingXBlockedYBase;
00238 public:
00239 static void setUp(const Weights &weights,int stage);
00240 static const MultiIntPair eval(const NumEffectState &state)
00241 {
00242 return KingXBlockedYBase::eval(state, table);
00243 }
00244 private:
00245 static table_t table;
00246 };
00247
00248 class KingXBlockedBoth : public KingXBlockedBase
00249 {
00250 public:
00251 static void
00252 evalWithUpdateBang(const NumEffectState &new_state, Move last_move,
00253 MultiIntPair& last_values_and_out);
00254 };
00255
00256 class KingXBothBlocked
00257 {
00258 friend class KingXBlockedYBase;
00259 public:
00260 enum { ONE_DIM = 5, DIM = ONE_DIM * EvalStages };
00261 static void setUp(const Weights &weights);
00262 static MultiIntPair eval(const NumEffectState &state);
00263 private:
00264 static CArray<MultiInt, ONE_DIM> table;
00265 static int index(const Square king)
00266 {
00267 const int x = king.x();
00268 return (x > 5 ? 9 - x : x - 1);;
00269 }
00270 template <Player P>
00271 static int indexY(const Square king)
00272 {
00273 const int x = king.x();
00274 const int y = (P == BLACK ? king.y() : 10 - king.y());
00275 return (y - 1) * 5 + (x > 5 ? 9 - x : x - 1);
00276 }
00277 template <int Sign>
00278 static void adjust(int index, int index_y, MultiInt &out);
00279 };
00280
00281 class KingXBothBlockedY
00282 {
00283 friend class KingXBlockedYBase;
00284 friend class KingXBothBlocked;
00285 public:
00286 enum { ONE_DIM = 5 * 9, DIM = ONE_DIM * EvalStages };
00287 static void setUp(const Weights &weights);
00288 private:
00289 static CArray<MultiInt, ONE_DIM> table;
00290 };
00291
00292 class KingXBlocked3
00293 {
00294 friend class KingXBlocked3Y;
00295 public:
00296 enum { ONE_DIM = 80, DIM = ONE_DIM * EvalStages };
00297 static MultiInt eval(const NumEffectState &state);
00298 static void setUp(const Weights &weights);
00299 private:
00300 template <int Sign>
00301 static void adjust(int index_y, MultiInt &result)
00302 {
00303 if(Sign>0)
00304 result += y_table[index_y];
00305 else
00306 result -= y_table[index_y];
00307 }
00308 template <Player P>
00309 static int index(const Square king, bool is_l,
00310 bool u_blocked, bool opp_u_blocked, bool opp_blocked)
00311 {
00312 int x = king.x();
00313 if (x >= 6)
00314 {
00315 x = 10 - x;
00316 if (P == BLACK)
00317 {
00318 is_l = !is_l;
00319 }
00320 }
00321 else if (P == WHITE && x <= 4)
00322 {
00323 is_l = !is_l;
00324 }
00325 return x - 1 + 5 * ((is_l ? 1 : 0) + 2 * ((u_blocked ? 1 : 0) + 2 * ((opp_u_blocked ? 1 : 0) +2 * (opp_blocked ? 1 : 0))));
00326 }
00327 template <Player P>
00328 static int indexY(const Square king, bool is_l,
00329 bool u_blocked, bool opp_u_blocked,
00330 bool opp_blocked)
00331 {
00332 int x = king.x();
00333 const int y = (P == BLACK ? king.y() : 10 - king.y());
00334 if (x >= 6)
00335 {
00336 x = 10 - x;
00337 if (P == BLACK)
00338 {
00339 is_l = !is_l;
00340 }
00341 }
00342 else if (P == WHITE && x <= 4)
00343 {
00344 is_l = !is_l;
00345 }
00346 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)))));
00347 }
00348 static CArray<MultiInt, 80> table;
00349 static CArray<MultiInt, 720> y_table;
00350 };
00351
00352 class KingXBlocked3Y
00353 {
00354 public:
00355 enum { ONE_DIM = 720, DIM = ONE_DIM * EvalStages };
00356 static void setUp(const Weights &weights);
00357 };
00358
00359 class AnagumaEmpty
00360 {
00361 public:
00362 enum { DIM = 4 };
00363 static void setUp(const Weights &weights,int stage);
00364 static MultiInt eval(const osl::state::NumEffectState &state);
00365 static int index(Square king, Square target);
00366 template <osl::Player Defense>
00367 static MultiInt evalOne(const NumEffectState &state);
00368 private:
00369 static CArray<MultiInt, 4> table;
00370 };
00371 }
00372 }
00373 }
00374
00375 #endif // EVAL_ML_KING8_H
00376
00377
00378
00379