Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

SgBlackWhite.h

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


17 Jun 2010 Doxygen 1.4.7