pieceEval.h
Go to the documentation of this file.
00001 /* pieceEval.h
00002  */
00003 #ifndef OSL_PIECEEVAL_H
00004 #define OSL_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/numEffectState.h"
00011 #include "osl/misc/carray.h"
00012 #include <cassert>
00013 
00014 namespace osl
00015 {
00016   namespace eval
00017   {
00018     class PtypeEvalTable
00019     {
00020     protected:
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 
00028     public:
00032       int value(PtypeO ptypeO) const{
00033         assert(isValidPtypeO(ptypeO));
00034         return ptypeO2Val[ptypeO-PTYPEO_MIN];
00035       }
00039       int value(Ptype ptype) const{
00040         assert(isValid(ptype));
00041         return ptypeO2Val[ptype-PTYPEO_MIN];
00042       }
00046       int promoteValue(PtypeO ptypeO) const{
00047         assert(isPromoted(ptypeO));
00048         return promoteVal[ptypeO-PTYPEO_MIN];
00049       }
00053       int captureValue(PtypeO ptypeO) const{
00054         assert(isValidPtypeO(ptypeO));
00055         return captureVal[ptypeO-PTYPEO_MIN];
00056       }
00057       void reset(const CArray<int, PTYPE_SIZE>& values);
00058     };
00059     extern const PtypeEvalTable Ptype_Eval_Table;
00060 
00067     class PieceEval
00068     {
00069       int val;
00070     public:
00071       explicit PieceEval(const NumEffectState& 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       static int diffWithMove(const NumEffectState&, Move move)
00081       {
00082         int ret = 0;
00083         if (move.capturePtype() != PTYPE_EMPTY)
00084           ret += Ptype_Eval_Table.captureValue(move.capturePtypeO());
00085         if (move.isPromotion())
00086           ret+=Ptype_Eval_Table.promoteValue(move.ptypeO());
00087         return ret;
00088       }
00089       static int infty() { return 57984; }
00090 
00102       template<Player P>
00103       static int computeDiffAfterMove(const NumEffectState& state,Move move);
00104       static int computeDiffAfterMove(const NumEffectState& state,Move move)
00105       {
00106         assert(state.turn() == move.player());
00107         if (state.turn() == BLACK)
00108           return computeDiffAfterMove<BLACK>(state,move);
00109         else
00110           return computeDiffAfterMove<WHITE>(state,move);
00111       }
00118       template<Player P>
00119       static int computeDiffAfterMoveForRP(const NumEffectState& state,Move move)
00120       {
00121         assert(move.player()==P);
00122         const int diff = computeDiffAfterMove<P>(state,move);
00123         return (P==BLACK) ? diff : -diff;
00124       }
00125       static int computeDiffAfterMoveForRP(const NumEffectState& state, Move move)
00126       {
00127         if (move.player()==BLACK)
00128           return computeDiffAfterMoveForRP<BLACK>(state,move);
00129         else
00130           return computeDiffAfterMoveForRP<WHITE>(state,move);
00131       }
00132     private:
00133       void addVal(int d) { val+=d; }
00134     public:
00135       const Move suggestMove(const NumEffectState&) const 
00136       {
00137         return Move();
00138       }
00140       int expect(const NumEffectState& /*state*/, 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 NumEffectState& /*new_state*/, 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 
00184       static const PtypeEvalTable Piece_Value;
00185     };
00186 
00187   } // namespace eval
00188   using eval::PieceEval;
00189 } // namespace osl
00190 
00191 #endif /* OSL_PIECEEVAL_H */
00192 // ;;; Local Variables:
00193 // ;;; mode:c++
00194 // ;;; c-basic-offset:2
00195 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines