00001
00002
00003 #ifndef _PIECEEVAL_H
00004 #define _PIECEEVAL_H
00005
00006 #include "osl/eval/ptypeEvalTraits.h"
00007 #include "osl/eval/evalTraits.h"
00008 #include "osl/progress/progress32.h"
00009 #include "osl/progress/progress16.h"
00010 #include "osl/state/simpleState.h"
00011 #include "osl/misc/carray.h"
00012 #include <cassert>
00013
00014 namespace osl
00015 {
00016 namespace eval
00017 {
00018 class PtypeEvalTable
00019 {
00020 private:
00021 CArray<int, PTYPEO_SIZE> ptypeO2Val;
00022 CArray<int, PTYPEO_SIZE> promoteVal;
00023 CArray<int, PTYPEO_SIZE> captureVal;
00024 public:
00025 PtypeEvalTable();
00026 ~PtypeEvalTable();
00027 private:
00028 void init();
00029 public:
00033 int value(PtypeO ptypeO) const{
00034 assert(isValidPtypeO(ptypeO));
00035 return ptypeO2Val[ptypeO-PTYPEO_MIN];
00036 }
00040 int value(Ptype ptype) const{
00041 assert(isValid(ptype));
00042 return ptypeO2Val[ptype-PTYPEO_MIN];
00043 }
00047 int promoteValue(PtypeO ptypeO) const{
00048 assert(isPromoted(ptypeO));
00049 return promoteVal[ptypeO-PTYPEO_MIN];
00050 }
00054 int captureValue(PtypeO ptypeO) const{
00055 assert(isValidPtypeO(ptypeO));
00056 return captureVal[ptypeO-PTYPEO_MIN];
00057 }
00058 };
00059 extern const PtypeEvalTable Ptype_Eval_Table;
00060
00067 class PieceEval
00068 {
00069 int val;
00070 public:
00071 explicit PieceEval(const SimpleState& state);
00072 explicit PieceEval(int v) : val(v) {}
00073 static bool initialized() { return true; }
00074 void changeTurn() {}
00075 int value() const
00076 {
00077 assert(isConsistentValueForNormalState<PieceEval>(val));
00078 return val;
00079 }
00080 template <class State>
00081 static int diffWithMove(const State&, Move move)
00082 {
00083 int ret = 0;
00084 if (move.capturePtype() != PTYPE_EMPTY)
00085 ret += Ptype_Eval_Table.captureValue(move.capturePtypeO());
00086 if (move.isPromote())
00087 ret+=Ptype_Eval_Table.promoteValue(move.ptypeO());
00088 return ret;
00089 }
00090 static int infty() { return 57984; }
00091
00103 template<class State,Player P>
00104 static int computeDiffAfterMove(const State& state,Move move);
00105 template<class State>
00106 static int computeDiffAfterMove(const State& state,Move move)
00107 {
00108 assert(state.getTurn() == move.player());
00109 if (state.getTurn() == BLACK)
00110 return computeDiffAfterMove<State,BLACK>(state,move);
00111 else
00112 return computeDiffAfterMove<State,WHITE>(state,move);
00113 }
00120 template<Player P,class State>
00121 static int computeDiffAfterMoveForRP(const State& state,Move move)
00122 {
00123 assert(move.player()==P);
00124 typedef typename State::effect_state_t effect_state_t;
00125 const int diff = computeDiffAfterMove<effect_state_t,P>(state,move);
00126 return (P==BLACK) ? diff : -diff;
00127 }
00128 template<class State>
00129 static int computeDiffAfterMoveForRP(const State& state, Move move)
00130 {
00131 if (move.player()==BLACK)
00132 return computeDiffAfterMoveForRP<BLACK,State>(state,move);
00133 else
00134 return computeDiffAfterMoveForRP<WHITE,State>(state,move);
00135 }
00136 private:
00137 void addVal(int d) { val+=d; }
00138 public:
00140 int expect(const SimpleState& , Move move) const
00141 {
00142 if (move.isPass() || move.isDrop())
00143 return value();
00144 const PtypeO ptypeO=move.ptypeO();
00145 const PtypeO captured=move.capturePtypeOSafe();
00146 int result = val
00147 + Ptype_Eval_Table.value(ptypeO)
00148 - Ptype_Eval_Table.value(move.oldPtypeO());
00149 if (getPtype(captured) != PTYPE_EMPTY)
00150 result += Ptype_Eval_Table.value(osl::captured(captured))
00151 - Ptype_Eval_Table.value(captured);
00152 return result;
00153 }
00154
00155 const Progress32 progress32() const { return Progress32(0); }
00156 const Progress16 progress16() const { return Progress16(0); }
00157 static int seeScale() { return 1; }
00161 static int captureValue(PtypeO ptypeO)
00162 {
00163 return Ptype_Eval_Table.captureValue(ptypeO);
00164 }
00165 static int value(PtypeO ptypeO)
00166 {
00167 return Ptype_Eval_Table.value(ptypeO);
00168 }
00169
00170 void update(const SimpleState& , Move last_move)
00171 {
00172 if (last_move.isPass() || last_move.isDrop())
00173 return;
00174
00175 addVal(Ptype_Eval_Table.value(last_move.ptypeO())
00176 - Ptype_Eval_Table.value(last_move.oldPtypeO()));
00177 if (last_move.capturePtype() != PTYPE_EMPTY) {
00178 const PtypeO capture_ptypeo = last_move.capturePtypeO();
00179 addVal(Ptype_Eval_Table.value(captured(capture_ptypeo))
00180 - Ptype_Eval_Table.value(capture_ptypeo));
00181 }
00182 }
00183 const PieceMask pins(Player) const { return PieceMask(); }
00184 };
00185
00186 }
00187 using eval::PieceEval;
00188 }
00189
00190 #endif
00191
00192
00193
00194