Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

SgBoardColor.h

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


Sun Mar 13 2011 Doxygen 1.7.1