00001 // cwchessboard -- A C++ chessboard tool set 00002 // 00003 //! @file Code.h This file contains the declaration of class Code. 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 CODE_H 00025 #define CODE_H 00026 00027 #ifndef USE_PCH 00028 #include <stdint.h> 00029 #endif 00030 00031 #include "Color.h" 00032 #include "Type.h" 00033 #include "Direction.h" 00034 00035 namespace cwchess { 00036 00037 /** @brief The POD base type of class Code. 00038 * 00039 * This class represents a chess piece, either 00040 * pawn, rook, knight, bishop, queen or king, 00041 * and it's color, white or black. 00042 * 00043 * See ColorData and TypeData for the encoding of the bits. 00044 * 00045 * @sa Code, white_pawn, white_rook, white_knight, white_bishop, white_queen, white_king, black_pawn, black_rook, black_knight, black_bishop, black_queen, black_king 00046 * 00047 * In order to refer to 'nothing', use the default constructor Code(). 00048 * / 00049 struct CodeData { 00050 uint8_t M_bits; //!< 0000CTTT, where C is the color and TTT the type. 00051 }; 00052 00053 //! A constant representing a white pawn. 00054 CodeData const white_pawn = { white_bits | pawn_bits }; 00055 //! A constant representing a white rook. 00056 CodeData const white_rook = { white_bits | rook_bits }; 00057 //! A constant representing a white knight. 00058 CodeData const white_knight = { white_bits | knight_bits }; 00059 //! A constant representing a white bishop. 00060 CodeData const white_bishop = { white_bits | bishop_bits }; 00061 //! A constant representing a white queen. 00062 CodeData const white_queen = { white_bits | queen_bits }; 00063 //! A constant representing a white king. 00064 CodeData const white_king = { white_bits | king_bits }; 00065 //! A constant representing a black pawn. 00066 CodeData const black_pawn = { black_bits | pawn_bits }; 00067 //! A constant representing a black rook; 00068 CodeData const black_rook = { black_bits | rook_bits }; 00069 //! A constant representing a black knight; 00070 CodeData const black_knight = { black_bits | knight_bits }; 00071 //! A constant representing a black bishop; 00072 CodeData const black_bishop = { black_bits | bishop_bits }; 00073 //! A constant representing a black queen; 00074 CodeData const black_queen = { black_bits | queen_bits }; 00075 //! A constant representing a black king; 00076 CodeData const black_king = { black_bits | king_bits }; 00077 00078 // Compare constants (this should never be needed, but why not add it). 00079 inline bool operator==(CodeData c1, CodeData c2) { return c1.M_bits == c2.M_bits; } 00080 inline bool operator!=(CodeData c1, CodeData c2) { return c1.M_bits != c2.M_bits; } 00081 00082 // Used to cast Code to CwChessboardCode. 00083 typedef uint16_t CwChessboardCode; 00084 00085 /** @brief A chess piece type including color. 00086 * 00087 * This class represents a code for a chess piece 00088 * that includes it's Type as well as it's Color. 00089 * 00090 * See CodeData for defined constants. 00091 * / 00092 class Code : protected CodeData { 00093 public: 00094 00095 /** @name Constructors* / 00096 //@{ 00097 00098 //! Construct a Code object initialized as 'nothing'. 00099 Code(void) { M_bits = 0; } 00100 00101 //! Copy-constructor. 00102 Code(Code const& code) { M_bits = code.M_bits; } 00103 00104 //! Construct a Code object from a constant. 00105 Code(CodeData code) { M_bits = code.M_bits; } 00106 00107 //! Construct a Code object with color \a color and type \a type. 00108 Code(Color const& color, Type const& type) { M_bits = color.M_bits | type.M_bits; } 00109 00110 //! Explicit conversion from CwChessboardCode to Code. 00111 explicit Code(CwChessboardCode code) { M_bits = CwChessboardCode_to_Code[code].M_bits; } 00112 00113 //@} 00114 00115 /** @name Assigment operators* / 00116 //@{ 00117 00118 //! Assign from another Code. 00119 Code& operator=(Code const& code) { M_bits = code.M_bits; return* this; } 00120 00121 //! Assign from a constant. 00122 Code& operator=(CodeData code) { M_bits = code.M_bits; return* this; } 00123 00124 //! Change the type to \a type. Type may not be nothing (use clear() instead). 00125 Code& operator=(Type const& type) { M_bits& = ~type_mask; M_bits |= type.M_bits; return* this; } 00126 00127 //! Change the color to \a color. 00128 Code& operator=(Color const& color) { M_bits& = ~color_mask; M_bits |= color.M_bits; return* this; } 00129 00130 //! Set the type to nothing. 00131 void clear(void) { M_bits = 0; } 00132 00133 //@} 00134 00135 /** @name Comparison operators* / 00136 //@{ 00137 00138 friend bool operator==(Code const& c1, Code const& c2) { return c1.M_bits == c2.M_bits; } 00139 friend bool operator==(Code const& c1, CodeData c2) { return c1.M_bits == c2.M_bits; } 00140 friend bool operator==(CodeData c1, Code const& c2) { return c1.M_bits == c2.M_bits; } 00141 friend bool operator!=(Code const& c1, Code const& c2) { return c1.M_bits != c2.M_bits; } 00142 friend bool operator!=(Code const& c1, CodeData c2) { return c1.M_bits != c2.M_bits; } 00143 friend bool operator!=(CodeData c1, Code const& c2) { return c1.M_bits != c2.M_bits; } 00144 00145 //@} 00146 00147 /** @name Accessors* / 00148 //@{ 00149 00150 //! Returns TRUE if the type is a bishop, rook or queen. 00151 bool is_a_slider(void) const { return (M_bits & type_mask) > 4; } 00152 00153 //! Returns TRUE if the type is a rook or queen. 00154 bool is_a_rookmover(void) const { return (M_bits & rook_bits) == rook_bits; } 00155 00156 //! Returns TRUE if the type is a bishop or queen. 00157 bool is_a_bishopmover(void) const { return (M_bits & bishop_bits) == bishop_bits; } 00158 00159 //! Returns TRUE if the code represents 'nothing'. 00160 bool is_nothing(void) const { return M_bits == 0; } 00161 00162 //! Returns TRUE if the type is equal. 00163 bool is_a(Type const& type) const { return (M_bits & type_mask) == type.M_bits; } 00164 00165 //! Returns TRUE if the type is equal. 00166 bool is_a(TypeData type) const { return (M_bits & type_mask) == type.M_bits; } 00167 00168 //! Return TRUE if the color is equal. 00169 bool is(Color const& color) const { return (M_bits & color_mask) == color.M_bits; } 00170 00171 //! Return TRUE if the color is equal. 00172 bool is(ColorData color) const { return (M_bits & color_mask) == color.M_bits; } 00173 00174 //! Return TRUE if the colors are different. 00175 bool has_opposite_color_of(Code const& code) { return (M_bits ^ code.M_bits) & color_mask; } 00176 00177 //! Return TRUE if this piece moves along \a direction. 00178 bool moves_along(Direction const& direction) { return (M_bits & direction.mover_flags().M_bits) == direction.mover_flags().M_bits; } 00179 00180 //! Return the Type of this Code. 00181 Type type(void) const { return Type(M_bits & type_mask); } 00182 00183 //! Return the Color of this Code. 00184 Color color(void) const { return Color(M_bits & color_mask); } 00185 00186 //! Return the unlaying integral value. 00187 uint8_t operator()(void) const { return M_bits; } 00188 00189 //@} 00190 00191 /** @name Special functions* / 00192 //@{ 00193 00194 //! Toggle the color. May not be used on type 'nothing'. 00195 void toggle_color(void) { M_bits ^= color_mask; } 00196 00197 //! Casting operator. 00198 operator CwChessboardCode(void) const { return Code_to_CwChessboardCode[M_bits]; } 00199 00200 //@} 00201 00202 private: 00203 static CwChessboardCode Code_to_CwChessboardCode[16]; 00204 static CodeData CwChessboardCode_to_Code[14]; 00205 }; 00206 00207 } // namespace cwchess 00208 00209 #endif // CODE_H