00001 //---------------------------------------------------------------------------- 00002 /** @file SgBoardColor.h 00003 State of a point on the board for games with Black, White, Empty states. 00004 */ 00005 //---------------------------------------------------------------------------- 00006 00007 #ifndef SG_BOARDCOLOR_H 00008 #define SG_BOARDCOLOR_H 00009 00010 #include <climits> 00011 #include "SgBlackWhite.h" 00012 #include <boost/static_assert.hpp> 00013 00014 //---------------------------------------------------------------------------- 00015 00016 /** Empty point. */ 00017 const int SG_EMPTY = 2; 00018 00019 /** Border point (outside of playing area) */ 00020 const int SG_BORDER = 3; 00021 00022 //---------------------------------------------------------------------------- 00023 00024 // SgEBWIterator and maybe other code relies on that 00025 BOOST_STATIC_ASSERT(SG_BLACK == 0); 00026 BOOST_STATIC_ASSERT(SG_WHITE == 1); 00027 BOOST_STATIC_ASSERT(SG_EMPTY == 2); 00028 00029 //---------------------------------------------------------------------------- 00030 00031 /** SG_BLACK, SG_WHITE, or SG_EMPTY */ 00032 typedef int SgEmptyBlackWhite; 00033 00034 /** SG_BLACK, SG_WHITE, SG_EMPTY, or SG_BORDER */ 00035 typedef int SgBoardColor; 00036 00037 #define SG_ASSERT_EBW(c) \ 00038 SG_ASSERT(c == SG_BLACK || c == SG_WHITE || c == SG_EMPTY) 00039 00040 #define SG_ASSERT_COLOR(c) \ 00041 SG_ASSERT(c == SG_BLACK || c == SG_WHITE || c == SG_EMPTY || c == SG_BORDER) 00042 00043 inline bool SgIsEmptyBlackWhite(SgBoardColor c) 00044 { 00045 return c == SG_BLACK || c == SG_WHITE || c == SG_EMPTY; 00046 } 00047 00048 inline SgBoardColor SgOpp(SgBoardColor c) 00049 { 00050 SG_ASSERT_COLOR(c); 00051 return c <= SG_WHITE ? SgOppBW(c) : c; 00052 } 00053 00054 inline char SgEBW(SgEmptyBlackWhite color) 00055 { 00056 SG_ASSERT_EBW(color); 00057 return color == SG_EMPTY ? 'E' : color == SG_BLACK ? 'B' : 'W'; 00058 } 00059 00060 //---------------------------------------------------------------------------- 00061 00062 /** Iterator over three colors, Empty, Black and White. 00063 Works analogously to SgBWIterator. 00064 */ 00065 class SgEBWIterator 00066 { 00067 public: 00068 SgEBWIterator() 00069 : m_color(SG_BLACK) 00070 { } 00071 00072 /** Advance the state of the iteration to the next element. 00073 Relies on current coding: incrementing SG_WHITE will yield value 00074 outside of {SG_EMPTY, SG_BLACK, SG_WHITE} 00075 */ 00076 void operator++() 00077 { 00078 ++m_color; 00079 } 00080 00081 /** Return the value of the current element. */ 00082 SgEmptyBlackWhite operator*() const 00083 { 00084 return m_color; 00085 } 00086 00087 /** Return true if iteration is valid, otherwise false. */ 00088 operator bool() const 00089 { 00090 return m_color <= SG_EMPTY; 00091 } 00092 00093 private: 00094 SgEmptyBlackWhite m_color; 00095 00096 /** Not implemented */ 00097 SgEBWIterator(const SgEBWIterator&); 00098 00099 /** Not implemented */ 00100 SgEBWIterator& operator=(const SgEBWIterator&); 00101 }; 00102 00103 //---------------------------------------------------------------------------- 00104 00105 #endif // SG_SGBOARDCOLOR_H