evalTraits.h
Go to the documentation of this file.
00001 /* evalTraits.h
00002  */
00003 #ifndef _EVAL_TRAITS_H
00004 #define _EVAL_TRAITS_H
00005 
00006 #include "osl/player.h"
00007 #include <algorithm>
00008 namespace osl
00009 {
00010   namespace eval
00011   {
00012     template<Player P>
00013     struct EvalTraits;
00014 
00015     template<>
00016     struct EvalTraits<BLACK>
00017     {
00018       static const int delta=1;
00019       static const int MAX_VALUE = 250000000;
00020       // infty become specific to Evaluation class
00021       static int max(int v1,int v2){ return std::max(v1,v2); }
00022       static int min(int v1,int v2){ return std::min(v1,v2); }
00023 
00024       static bool betterThan(int v1,int v2) 
00025       {
00026         return v1 > v2;
00027       }
00028       static bool notLessThan(int v1,int v2) {
00029         return v1 >= v2;
00030       }
00034       static int convert(int value)
00035       {
00036         assert(value >= 0);
00037         return value;
00038       }
00039     };
00040 
00041     template<>
00042     struct EvalTraits<WHITE>
00043     {
00044       static const int delta= -EvalTraits<BLACK>::delta;
00045       static const int MAX_VALUE= -EvalTraits<BLACK>::MAX_VALUE;
00046       static int max(int v1,int v2){ return std::min(v1,v2); }
00047       static int min(int v1,int v2){ return std::max(v1,v2); }
00048 
00049       static bool betterThan(int v1,int v2) 
00050       {
00051         return v1 < v2;
00052       }
00053       static bool notLessThan(int v1,int v2) 
00054       {
00055         return v1 <= v2;
00056       }
00060       static int convert(int value)
00061       {
00062         assert(value >= 0);
00063         return -value;
00064       }
00065     };
00066 
00067     inline bool betterThan(Player p, int v1,int v2)
00068     {
00069       assert(isValid(p));
00070       if (p == BLACK)
00071         return EvalTraits<BLACK>::betterThan(v1,v2);
00072       else
00073         return EvalTraits<WHITE>::betterThan(v1,v2);
00074     }
00075     inline bool notLessThan(Player p, int v1,int v2)
00076     {
00077       assert(isValid(p));
00078       if (p == BLACK)
00079         return EvalTraits<BLACK>::notLessThan(v1,v2);
00080       else
00081         return EvalTraits<WHITE>::notLessThan(v1,v2);
00082     }
00083 
00084     inline int max(Player p, int v1, int v2)
00085     {
00086       assert(isValid(p));
00087       if (p == BLACK)
00088         return EvalTraits<BLACK>::max(v1,v2);
00089       else
00090         return EvalTraits<WHITE>::max(v1,v2);
00091     }
00092     inline int min(Player p, int v1, int v2)
00093     {
00094       assert(isValid(p));
00095       if (p == BLACK)
00096         return EvalTraits<BLACK>::min(v1,v2);
00097       else
00098         return EvalTraits<WHITE>::min(v1,v2);
00099     }
00100 
00104     inline int delta(Player p)
00105     {
00106       assert(isValid(p));
00107       if (p == BLACK)
00108         return EvalTraits<BLACK>::delta;
00109       else
00110         return EvalTraits<WHITE>::delta;
00111     }
00112 
00116     inline int convert(Player P, int value)
00117     {
00118       assert(value >= 0);
00119       return value*delta(P);
00120     }
00121 
00125     template <class Eval>
00126     inline bool isConsistentValueForNormalState(int value)
00127     {
00128       const int infty = Eval::infty();
00129       return ((value % 2) == 0)
00130         && EvalTraits<BLACK>::betterThan(value, EvalTraits<WHITE>::convert(infty))
00131         && EvalTraits<WHITE>::betterThan(value, infty);
00132     }
00133     inline bool isConsistentValue(int value)
00134     {
00135       return (value % 2) == 0
00136         && (EvalTraits<BLACK>::MAX_VALUE >= value)
00137         && (EvalTraits<WHITE>::MAX_VALUE <= value);
00138     }
00139   } // namespace eval
00140   using eval::EvalTraits;
00141 } // namespace osl
00142 
00143 #endif /* _EVAL_TRAITS_H */
00144 // ;;; Local Variables:
00145 // ;;; mode:c++
00146 // ;;; c-basic-offset:2
00147 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines