Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

HexColor.hpp

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00002 /** @file
00003  */
00004 //----------------------------------------------------------------------------
00005 
00006 #ifndef HEXCOLOR_HPP
00007 #define HEXCOLOR_HPP
00008 
00009 #include "Benzene.hpp"
00010 
00011 _BEGIN_BENZENE_NAMESPACE_
00012 
00013 //----------------------------------------------------------------------------
00014 
00015 /** Available colors of a cell on a Hex board. 
00016     
00017     @note BLACK=0 and WHITE=1 is currently assumed by many pieces of code. 
00018 */
00019 typedef enum { BLACK=0, WHITE=1, EMPTY=2 } HexColor;
00020 
00021 //----------------------------------------------------------------------------
00022 
00023 /** Color of player to move first in a game of Hex. */
00024 static const HexColor FIRST_TO_PLAY = BLACK;
00025 
00026 //----------------------------------------------------------------------------
00027 
00028 /** Color of player who is trying to form a vertical chain (i.e. joining
00029     NORTH to SOUTH). */
00030 static const HexColor VERTICAL_COLOR = BLACK;
00031 
00032 //----------------------------------------------------------------------------
00033 
00034 /** Color that all dead cells are set to. */
00035 static const HexColor DEAD_COLOR = BLACK;
00036 
00037 //----------------------------------------------------------------------------
00038 
00039 /** Constant to denote an array to be indexed only by BLACK and
00040     WHITE. */
00041 static const int BLACK_AND_WHITE = 2;
00042 
00043 /** Constant to denote an array to be indexed by BLACK, WHITE, and
00044     EMPTY. */
00045 static const int BLACK_WHITE_EMPTY = 3;
00046 
00047 //----------------------------------------------------------------------------
00048 
00049 /** Iterator over BLACK and WHITE. */
00050 class BWIterator
00051 {
00052 public:
00053     BWIterator()
00054         : m_color(BLACK)
00055     { }
00056 
00057     /** Advance the state of the iteration to the next element. */
00058     void operator++()
00059     {
00060         ++m_color;
00061     }
00062 
00063     /** Return the value of the current element. */
00064     HexColor operator*() const
00065     {
00066         return static_cast<HexColor>(m_color);
00067     }
00068 
00069     /** Return true if iteration is valid, otherwise false. */
00070     operator bool() const
00071     {
00072         return m_color <= WHITE;
00073     }
00074 
00075 private:
00076     int m_color;
00077 
00078     /** Not implemented */
00079     BWIterator(const BWIterator&);
00080 
00081     /** Not implemented */
00082     BWIterator& operator=(const BWIterator&);
00083 };
00084 
00085 /** Iterator over BLACK and WHITE and EMPTY. */
00086 class ColorIterator
00087 {
00088 public:
00089     ColorIterator()
00090         : m_color(BLACK)
00091     { }
00092 
00093     /** Advance the state of the iteration to the next element. */
00094     void operator++()
00095     {
00096         ++m_color;
00097     }
00098 
00099     /** Return the value of the current element. */
00100     HexColor operator*() const
00101     {
00102         return static_cast<HexColor>(m_color);
00103     }
00104 
00105     /** Return true if iteration is valid, otherwise false. */
00106     operator bool() const
00107     {
00108         return m_color <= EMPTY;
00109     }
00110 
00111 private:
00112     int m_color;
00113 
00114     /** Not implemented */
00115     ColorIterator(const ColorIterator&);
00116 
00117     /** Not implemented */
00118     ColorIterator& operator=(const ColorIterator&);
00119 };
00120 
00121 //----------------------------------------------------------------------------
00122 
00123 /** Basic HexColor utilities. */
00124 namespace HexColorUtil 
00125 {
00126 
00127 /** Returns true if color is one of BLACK, WHITE, or EMPTY. */
00128 inline bool isValidColor(HexColor color)
00129 {
00130     return (color == BLACK || color == WHITE) || (color == EMPTY);
00131 }
00132 
00133 /** Returns true if color is BLACK or WHITE. */
00134 inline bool isBlackWhite(HexColor color)
00135 {
00136     return (color == BLACK || color == WHITE);
00137 }
00138 
00139 /** Returns a string representation of the given HexColor. */
00140 inline std::string toString(HexColor color)
00141 {
00142     HexAssert(isValidColor(color));
00143     if (color == BLACK) return "black";
00144     if (color == WHITE) return "white";
00145     return "empty";
00146 }
00147 
00148 /** Returns the opposite color for BLACK and WHITE, EMPTY for EMPTY. */
00149 inline HexColor otherColor(HexColor color)
00150 {
00151     HexAssert(isValidColor(color));
00152     if (color == EMPTY) return EMPTY;
00153     return (color == WHITE) ? BLACK : WHITE;
00154 }
00155 
00156 } // namespace HexColorUtil
00157 
00158 //----------------------------------------------------------------------------
00159 
00160 /** Overrides the standard "<<" operator; same as
00161     HexColorUtil::toString(). */
00162 inline std::ostream& operator<<(std::ostream& os, HexColor color)
00163 {
00164     os << HexColorUtil::toString(color);
00165     return os;
00166 }
00167 
00168 /** Overrides the standard "!" operator to be the same as
00169     HexColorUtil::otherColor(). */
00170 inline HexColor operator!(HexColor color)
00171 {
00172     return HexColorUtil::otherColor(color);
00173 }
00174 
00175 //----------------------------------------------------------------------------
00176 
00177 /** All possible sets of available colors. */
00178 typedef enum { BLACK_ONLY, WHITE_ONLY, EMPTY_ONLY, 
00179                NOT_BLACK, NOT_WHITE, NOT_EMPTY, 
00180                ALL_COLORS, NUM_COLOR_SETS } HexColorSet;
00181 
00182 /** Utilities on HexColorSets. */
00183 namespace HexColorSetUtil
00184 {
00185 
00186 /** Returns true if colorset is a valid HexColorSet. */
00187 inline bool isValid(HexColorSet colorset)
00188 {
00189     return (colorset >= BLACK_ONLY && colorset <= ALL_COLORS);
00190 }
00191 
00192 /** Converts a HexColorSet to a string. */
00193 inline std::string toString(HexColorSet colorset)
00194 {
00195     HexAssert(isValid(colorset));
00196     if (colorset == BLACK_ONLY) return "black_only";
00197     if (colorset == WHITE_ONLY) return "white_only";
00198     if (colorset == EMPTY_ONLY) return "empty_only";
00199     if (colorset == NOT_BLACK) return "not_black";
00200     if (colorset == NOT_WHITE) return "not_white";
00201     if (colorset == NOT_EMPTY) return "not_empty";
00202     return "all_colors";
00203 }
00204 
00205 /** Converts a string into a HexColorSet. */
00206 inline HexColorSet fromString(std::string str)
00207 {
00208     if (str == "black_only") return BLACK_ONLY;
00209     if (str == "white_only") return WHITE_ONLY;
00210     if (str == "empty_only") return EMPTY_ONLY;
00211     if (str == "not_black") return NOT_BLACK;
00212     if (str == "not_white") return NOT_WHITE;
00213     if (str == "not_empty") return NOT_EMPTY;
00214     if (str == "all_colors") return ALL_COLORS;
00215     HexAssert(false);
00216     return ALL_COLORS;
00217 }
00218     
00219 /** Returns true if color is in colorset. */
00220 inline bool InSet(HexColor color, HexColorSet colorset)
00221 {
00222     HexAssert(HexColorUtil::isValidColor(color));
00223     HexAssert(HexColorSetUtil::isValid(colorset));
00224  
00225     switch(colorset) {
00226     case BLACK_ONLY: return (color == BLACK);
00227     case WHITE_ONLY: return (color == WHITE);
00228     case EMPTY_ONLY: return (color == EMPTY);
00229     case  NOT_BLACK: return (color != BLACK);
00230     case  NOT_WHITE: return (color != WHITE);
00231     case  NOT_EMPTY: return (color != EMPTY);
00232     case ALL_COLORS: return true;
00233     default:
00234         HexAssert(false);
00235     }
00236     return true;
00237 }
00238 
00239 /** Returns the HexColorSet composed only of color. */
00240 inline HexColorSet Only(HexColor color)
00241 {
00242     HexAssert(HexColorUtil::isValidColor(color));
00243     if (color == BLACK) 
00244         return BLACK_ONLY;
00245     else if (color == WHITE)
00246         return WHITE_ONLY;
00247     return EMPTY_ONLY;
00248 }
00249 
00250 /** Returns the HexColorSet containing all but color. */
00251 inline HexColorSet NotColor(HexColor color)
00252 {
00253     HexAssert(HexColorUtil::isValidColor(color));
00254     if (color == BLACK) 
00255         return NOT_BLACK;
00256     else if (color == WHITE)
00257         return NOT_WHITE;
00258     return NOT_EMPTY;
00259 }
00260 
00261 /** Returns the HexColorSet containing color or empty; equivalent to:
00262     @code NotColor(HexColorUtil::otherColor(color)). @endcode */
00263 inline HexColorSet ColorOrEmpty(HexColor color)
00264 {
00265     HexAssert(HexColorUtil::isValidColor(color));
00266     if (color == BLACK)
00267         return NOT_WHITE;
00268     else if (color == WHITE)
00269         return NOT_BLACK;
00270     return EMPTY_ONLY;
00271 }
00272 
00273 } // namespace HexColorSetUtil
00274 
00275 //----------------------------------------------------------------------------
00276 
00277 _END_BENZENE_NAMESPACE_
00278 
00279 #endif // HEXCOLOR_HPP


6 Jan 2011 Doxygen 1.6.3