00001 // cwchessboard -- A C++ chessboard tool set 00002 // 00003 //! @file Color.h This file contains the declaration of class Color. 00004 // 00005 // Copyright (C) 2008, by 00006 // 00007 // Carlo Wood, Run on IRC <carlo@alinoe.com> 00008 // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt 00009 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61 00010 // 00011 // This program is free software: you can redistribute it and/or modify 00012 // it under the terms of the GNU General Public License as published by 00013 // the Free Software Foundation, either version 2 of the License, or 00014 // (at your option) any later version. 00015 // 00016 // This program is distributed in the hope that it will be useful, 00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 // GNU General Public License for more details. 00020 // 00021 // You should have received a copy of the GNU General Public License 00022 // along with this program. If not, see <http://www.gnu.org/licenses/>. 00023 00024 #ifndef COLOR_H 00025 #define COLOR_H 00026 00027 #ifndef USE_PCH 00028 #include <stdint.h> 00029 #endif 00030 00031 namespace cwchess { 00032 00033 class Code; 00034 00035 /** @brief The POD base type of class Color. 00036 * 00037 * This class uses the same internal type as Code to store the color bit. 00038 * It even uses the same bit (the fourth bit). All other bits are garanteed zero. 00039 * 00040 * If the bit is set then the object represents the color white. 00041 * 00042 * @sa Color, black, white 00043 * / 00044 struct ColorData { 00045 uint8_t M_bits; //!< 0000C000, where C=0 means black and C=1 means white. 00046 }; 00047 00048 uint8_t const black_bits = 0; //!< The underlaying integral value of color 'black'. 00049 uint8_t const white_bits = 8; //!< The underlaying integral value of color 'white'. 00050 uint8_t const color_mask = 8; //!< A mask for the bits used for the color of a piece. 00051 00052 //! A constant representing the color black. 00053 ColorData const black = { black_bits }; 00054 //! A constant representing the color white. 00055 ColorData const white = { white_bits }; 00056 00057 // Compare constants (this should never be needed, but why not add it). 00058 inline bool operator==(ColorData c1, ColorData c2) { return c1.M_bits == c2.M_bits; } 00059 inline bool operator!=(ColorData c1, ColorData c2) { return c1.M_bits != c2.M_bits; } 00060 00061 /** @brief A color (black or white). 00062 * 00063 * This class represents a chess color. 00064 * 00065 * See ColorData for defined constants. 00066 * / 00067 class Color : protected ColorData { 00068 public: 00069 00070 /** @name Constructors* / 00071 //@{ 00072 00073 //! Construct an uninitialized Color object. 00074 Color(void) { } 00075 00076 //! Copy-constructor. 00077 Color(Color const& color) { M_bits = color.M_bits; } 00078 00079 //! Construct a Color object from a constant. 00080 Color(ColorData color) { M_bits = color.M_bits; } 00081 00082 //@} 00083 00084 /** @name Assignment operators* / 00085 //@{ 00086 00087 //! Assign from another Color object. 00088 Color& operator=(Color const& color) { M_bits = color.M_bits; return* this; } 00089 00090 //! Assign from a constant. 00091 Color& operator=(ColorData color) { M_bits = color.M_bits; return* this; } 00092 00093 //@} 00094 00095 /** @name Comparison operators* / 00096 //@{ 00097 00098 friend bool operator==(Color const& c1, Color const& c2) { return c1.M_bits == c2.M_bits; } 00099 friend bool operator==(Color const& c1, ColorData c2) { return c1.M_bits == c2.M_bits; } 00100 friend bool operator==(ColorData c1, Color const& c2) { return c1.M_bits == c2.M_bits; } 00101 friend bool operator!=(Color const& c1, Color const& c2) { return c1.M_bits != c2.M_bits; } 00102 friend bool operator!=(Color const& c1, ColorData c2) { return c1.M_bits != c2.M_bits; } 00103 friend bool operator!=(ColorData c1, Color const& c2) { return c1.M_bits != c2.M_bits; } 00104 00105 //@} 00106 00107 /** @name Accessors* / 00108 //@{ 00109 00110 //! Return TRUE if this color is white. 00111 bool is_white(void) const { return M_bits; } 00112 00113 //! Return TRUE if this color is black. 00114 bool is_black(void) const { return !M_bits; } 00115 00116 //! Return the underlaying integral value. 00117 uint8_t operator()(void) const { return M_bits; } 00118 00119 //@} 00120 00121 /** @name Special functions* / 00122 //@{ 00123 00124 //! Change the color from black to white or vica versa. 00125 void toggle(void) { M_bits ^= color_mask; } 00126 00127 //@} 00128 00129 /** @name Visitors* / 00130 //@{ 00131 00132 //! Return a Color object with the opposite color of this object. 00133 Color opposite(void) const { ColorData data; data.M_bits = M_bits ^ color_mask; return Color(data); } 00134 00135 //! Return a number that can be used as array index. 00136 uint8_t index(void) const { return M_bits >> 3; } 00137 00138 //! Return the index offset that advances one square in the direction of the pawns of this color. 00139 uint8_t forward_index_offset(void) const { return (M_bits << 1) - 8; } 00140 00141 //@} 00142 00143 #ifndef DOXYGEN 00144 private: 00145 friend class Code; 00146 // Constructor for class Code. 00147 explicit Color(uint8_t bits) { M_bits = bits; } 00148 #endif 00149 }; 00150 00151 } // namespace cwchess 00152 00153 #endif // COLOR_H