Go to the documentation of this file.00001
00002
00003
00004
00005
00006 #ifndef SG_UTIL_H
00007 #define SG_UTIL_H
00008
00009 #include <iostream>
00010 #include "SgBlackWhite.h"
00011 #include "SgBWArray.h"
00012
00013
00014 namespace SgUtil {
00015
00016 template<class T>
00017 inline void ForceInRange(const T& min, T* p, const T& max)
00018 {
00019 if (*p < min)
00020 *p = min;
00021 if (*p > max)
00022 *p = max;
00023 }
00024
00025 template <class T>
00026 inline bool InRange(const T& i, const T& from, const T& to)
00027 {
00028 return (i >= from) && (i <= to);
00029 }
00030
00031 template<class T>
00032 inline void LowerLimit(T& x, const T& limit)
00033 {
00034 if (x < limit)
00035 x = limit;
00036 }
00037
00038 template<class T>
00039 inline void UpperLimit(T& x, const T& limit)
00040 {
00041 if (x > limit)
00042 x = limit;
00043 }
00044
00045 }
00046
00047
00048
00049
00050 class SgBalancer
00051 {
00052 public:
00053 SgBalancer(int margin) :
00054 m_balance(0),
00055 m_nuCalls(0),
00056 m_margin(margin),
00057 m_played(0,0),
00058 m_rejected(0,0)
00059 { }
00060
00061 bool Play(SgBlackWhite color)
00062 {
00063 SG_ASSERT(SgIsBlackWhite(color));
00064 ++m_nuCalls;
00065 if (color == SG_BLACK)
00066 {
00067 if (m_balance < m_margin)
00068 {
00069 ++m_balance;
00070 ++m_played[SG_BLACK];
00071 return true;
00072 }
00073 }
00074 else if (m_balance > -m_margin)
00075 {
00076 --m_balance;
00077 ++m_played[SG_WHITE];
00078 return true;
00079 }
00080 ++m_rejected[color];
00081 return false;
00082 }
00083
00084 int Balance() const
00085 {
00086 return m_balance;
00087 }
00088
00089 int Margin() const
00090 {
00091 return m_margin;
00092 }
00093
00094 int NuCalls() const
00095 {
00096 return m_nuCalls;
00097 }
00098
00099 int NuPlayed(SgBlackWhite color) const
00100 {
00101 return m_played[color];
00102 }
00103
00104 int NuRejected(SgBlackWhite color) const
00105 {
00106 return m_rejected[color];
00107 }
00108
00109 private:
00110 int m_balance;
00111 int m_nuCalls;
00112 const int m_margin;
00113 SgBWArray<int> m_played;
00114 SgBWArray<int> m_rejected;
00115 };
00116
00117
00118
00119 std::ostream& operator<<(std::ostream& stream, const SgBalancer& balancer);
00120
00121
00122
00123 #endif // SG_UTIL_H
00124