00001
00002
00003
00004
00005
00006
00007 #ifndef SG_SEARCHVALUE_H
00008 #define SG_SEARCHVALUE_H
00009
00010 #include <cstdlib>
00011 #include <string>
00012 #include "SgBlackWhite.h"
00013 #include "SgSearch.h"
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 class SgSearchValue
00028 {
00029 public:
00030 enum {
00031
00032 MAX_LEVEL = 125,
00033
00034
00035
00036
00037
00038
00039 MAX_VALUE = MAX_LEVEL * SgSearch::MAX_DEPTH,
00040
00041
00042
00043
00044 MIN_VALUE = -MAX_VALUE,
00045
00046
00047 MAX_KO_LEVEL = 3,
00048
00049
00050
00051
00052
00053
00054 KO_VALUE = MAX_VALUE - SgSearch::MAX_DEPTH,
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 MIN_PROVEN_VALUE = MAX_VALUE
00065 - (MAX_KO_LEVEL + 1) * SgSearch::MAX_DEPTH
00066 };
00067
00068 SgSearchValue();
00069
00070 explicit SgSearchValue(int v);
00071
00072 SgSearchValue(SgBlackWhite goodForPlayer, int depth);
00073
00074 SgSearchValue(SgBlackWhite goodForPlayer, int depth, int koLevel);
00075
00076
00077 operator int() const;
00078
00079 int Depth() const;
00080
00081
00082
00083
00084
00085 bool FromString(const std::string& s);
00086
00087 bool IsEstimate() const;
00088
00089 bool IsKoValue() const;
00090
00091 bool IsPositive() const;
00092
00093
00094 static inline bool IsSolved(int value);
00095
00096
00097 static inline bool SolvedValue(bool isWin);
00098
00099 bool IsSureValue() const;
00100
00101 int KoLevel() const;
00102
00103 void SetValueForPlayer(SgBlackWhite player);
00104
00105 int ValueForBlack() const;
00106
00107 int ValueForPlayer(SgBlackWhite player) const;
00108
00109 int ValueForWhite() const;
00110
00111
00112
00113
00114
00115 std::string ToString(int unitPerPoint = 1) const;
00116
00117 private:
00118 int m_value;
00119 };
00120
00121 inline SgSearchValue::SgSearchValue()
00122 : m_value(0)
00123 { }
00124
00125 inline SgSearchValue::SgSearchValue(int v)
00126 : m_value(v)
00127 {
00128 SG_ASSERT(-MAX_VALUE <= v && v <= MAX_VALUE);
00129 }
00130
00131 inline SgSearchValue::SgSearchValue(SgBlackWhite goodForPlayer, int depth)
00132 : m_value(MAX_VALUE - depth)
00133 {
00134 SG_ASSERT_BW(goodForPlayer);
00135 SG_ASSERT(0 <= depth && depth < SgSearch::MAX_DEPTH);
00136 SetValueForPlayer(goodForPlayer);
00137
00138 SG_ASSERT(KoLevel() == 0);
00139 SG_ASSERT(Depth() == depth);
00140 }
00141
00142 inline SgSearchValue::SgSearchValue(SgBlackWhite goodForPlayer, int depth, int koLevel)
00143 : m_value(MAX_VALUE - depth - koLevel * SgSearch::MAX_DEPTH)
00144 {
00145 SG_ASSERT_BW(goodForPlayer);
00146 SG_ASSERT(0 <= depth && depth < SgSearch::MAX_DEPTH);
00147 SG_ASSERT(0 <= koLevel && koLevel <= MAX_KO_LEVEL);
00148 SetValueForPlayer(goodForPlayer);
00149
00150 SG_ASSERT(KoLevel() == koLevel);
00151 SG_ASSERT(Depth() == depth);
00152 }
00153
00154 inline SgSearchValue::operator int() const
00155 {
00156 return m_value;
00157 }
00158
00159 inline int SgSearchValue::Depth() const
00160 {
00161 if (IsEstimate())
00162 return 0;
00163 else return (SgSearch::MAX_DEPTH - 1)
00164 - (std::abs(m_value) - 1) % SgSearch::MAX_DEPTH;
00165 }
00166
00167 inline bool SgSearchValue::IsEstimate() const
00168 {
00169 return -MIN_PROVEN_VALUE < m_value && m_value < MIN_PROVEN_VALUE;
00170 }
00171
00172 inline bool SgSearchValue::IsKoValue() const
00173 {
00174 return IsSureValue() && -KO_VALUE < m_value && m_value < KO_VALUE;
00175 }
00176
00177 inline bool SgSearchValue::IsPositive() const
00178 {
00179 return 0 <= m_value;
00180 }
00181
00182 inline bool SgSearchValue::IsSureValue() const
00183 {
00184 return m_value <= -MIN_PROVEN_VALUE || MIN_PROVEN_VALUE <= m_value;
00185 }
00186
00187 inline bool SgSearchValue::IsSolved(int value)
00188 {
00189 return abs(value) == MAX_VALUE;
00190 }
00191
00192 inline bool SgSearchValue::SolvedValue(bool isWin)
00193 {
00194 return isWin ? +MAX_VALUE : -MAX_VALUE;
00195 }
00196
00197 inline void SgSearchValue::SetValueForPlayer(SgBlackWhite player)
00198 {
00199 if (player == SG_WHITE)
00200 m_value = -m_value;
00201 }
00202
00203 inline int SgSearchValue::ValueForBlack() const
00204 {
00205 return +m_value;
00206 }
00207
00208 inline int SgSearchValue::ValueForPlayer(SgBlackWhite player) const
00209 {
00210 SG_ASSERT_BW(player);
00211 return player == SG_WHITE ? -m_value : +m_value;
00212 }
00213
00214 inline int SgSearchValue::ValueForWhite() const
00215 {
00216 return -m_value;
00217 }
00218
00219
00220
00221 #endif // SG_SEARCHVALUE_H