00001 //---------------------------------------------------------------------------- 00002 /** @file SgBlackWhite.h 00003 Color of player in two-player games (black/white). */ 00004 //---------------------------------------------------------------------------- 00005 00006 #ifndef SG_BLACKWHITE_H 00007 #define SG_BLACKWHITE_H 00008 00009 #include <boost/static_assert.hpp> 00010 00011 //---------------------------------------------------------------------------- 00012 00013 /** Black stone, black player. */ 00014 const int SG_BLACK = 0; 00015 00016 /** White stone, white player. */ 00017 const int SG_WHITE = 1; 00018 00019 // must be consecutive for color for-loops 00020 BOOST_STATIC_ASSERT(SG_BLACK + 1 == SG_WHITE); 00021 00022 /** SG_BLACK or SG_WHITE */ 00023 typedef int SgBlackWhite; 00024 00025 inline bool SgIsBlackWhite(int c) 00026 { 00027 return c == SG_BLACK || c == SG_WHITE; 00028 } 00029 00030 #define SG_ASSERT_BW(c) SG_ASSERT(SgIsBlackWhite(c)) 00031 00032 inline SgBlackWhite SgOppBW(SgBlackWhite c) 00033 { 00034 SG_ASSERT_BW(c); 00035 return SG_BLACK + SG_WHITE - c; 00036 } 00037 00038 inline char SgBW(SgBlackWhite color) 00039 { 00040 SG_ASSERT_BW(color); 00041 return color == SG_BLACK ? 'B' : 'W'; 00042 } 00043 00044 //---------------------------------------------------------------------------- 00045 00046 /** Iterator over both colors, Black and White. 00047 The function Opp() returns the opponent since this is often needed too. 00048 00049 Usage example: 00050 @verbatim 00051 for (SgBWIterator it; it; ++it) 00052 { 00053 "this section will be executed twice:" 00054 "first with *it == SG_BLACK, then with *it == SG_WHITE" 00055 (unless it encounters a break or return inside) 00056 } 00057 @endverbatim */ 00058 class SgBWIterator 00059 { 00060 public: 00061 SgBWIterator() 00062 : m_color(SG_BLACK) 00063 { } 00064 00065 /** Advance the state of the iteration to the next element. */ 00066 void operator++() 00067 { 00068 SG_ASSERT_BW(m_color); 00069 ++m_color; 00070 } 00071 00072 /** Return the value of the current element. */ 00073 SgBlackWhite operator*() const 00074 { 00075 return m_color; 00076 } 00077 00078 /** Return the value of the current element. */ 00079 SgBlackWhite Opp() const 00080 { 00081 return SgOppBW(m_color); 00082 } 00083 00084 /** Return true if iteration is valid, otherwise false. */ 00085 operator bool() const 00086 { 00087 return m_color <= SG_WHITE; 00088 } 00089 00090 private: 00091 int m_color; 00092 00093 /** Not implemented */ 00094 SgBWIterator(const SgBWIterator&); 00095 00096 /** Not implemented */ 00097 SgBWIterator& operator=(const SgBWIterator&); 00098 }; 00099 00100 //---------------------------------------------------------------------------- 00101 00102 #endif // SG_BLACKWHITE_H