00001 // cwchessboard -- A C++ chessboard tool set 00002 // 00003 //! @file Type.h This file contains the declaration of class Type. 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 TYPE_H 00025 #define TYPE_H 00026 00027 #ifndef USE_PCH 00028 #include <stdint.h> 00029 #endif 00030 00031 #include "Color.h" 00032 00033 namespace cwchess { 00034 00035 class Code; 00036 00037 /** @brief The POD base type of class Type. 00038 * 00039 * This class uses the same internal type as Code to store the type bits. 00040 * It even uses the same bits (the three least significant bits). 00041 * All other bits are garanteed zero. 00042 * 00043 * If the third bit is set then the object represents a sliding piece (bishop, rook or queen). 00044 * If in addition the first bit is set it can moves like a bishop (bishop and queen), 00045 * or if the second bit is set it can move like a rook (rook and queen). 00046 * 00047 * @sa Type, nothing, pawn, knight, king, bishop, rook, queen 00048 * / 00049 struct TypeData { 00050 uint8_t M_bits; //!< 00000STT, where STT is the type. If S == 1 then the piece is a slider. 00051 }; 00052 00053 uint8_t const nothing_bits = 0; //!< The underlaying integral value of type 'nothing'. 00054 uint8_t const pawn_bits = 1; //!< The underlaying integral value of type 'pawn'. 00055 uint8_t const knight_bits = 2; //!< The underlaying integral value of type 'knight'. 00056 uint8_t const king_bits = 3; //!< The underlaying integral value of type 'king'. 00057 uint8_t const bishop_bits = 5; //!< The underlaying integral value of type 'bishop'. 00058 uint8_t const rook_bits = 6; //!< The underlaying integral value of type 'rook'. 00059 uint8_t const queen_bits = 7; //!< The underlaying integral value of type 'queen'. 00060 uint8_t const type_mask = 7; //!< A mask for the bits used for the type of a piece. 00061 00062 //! A constant representing the absence of a piece. 00063 TypeData const nothing = { nothing_bits }; 00064 //! A constant representing a pawn. 00065 TypeData const pawn = { pawn_bits }; 00066 //! A constant representing a knight. 00067 TypeData const knight = { knight_bits }; 00068 //! A constant representing a king. 00069 TypeData const king = { king_bits }; 00070 //! A constant representing a bishop. 00071 TypeData const bishop = { bishop_bits }; 00072 //! A constant representing a rook. 00073 TypeData const rook = { rook_bits }; 00074 //! A constant representing a queen. 00075 TypeData const queen = { queen_bits }; 00076 00077 // Compare constants (this should never be needed, but why not add it). 00078 inline bool operator==(TypeData t1, TypeData t2) { return t1.M_bits == t2.M_bits; } 00079 inline bool operator!=(TypeData t1, TypeData t2) { return t1.M_bits != t2.M_bits; } 00080 00081 /** @brief A chess piece type. 00082 * 00083 * This class represents a chess piece type. 00084 * 00085 * The class Code is a friend of this class. 00086 * / 00087 class Type : protected TypeData { 00088 public: 00089 /** @name Constructors* / 00090 //@{ 00091 00092 //! Construct an uninitialized Type. 00093 Type(void) { } 00094 00095 //! Copy-constructor. 00096 Type(Type const& type) { M_bits = type.M_bits; } 00097 00098 //! Construct a Type from a constant. 00099 Type(TypeData type) { M_bits = type.M_bits; } 00100 00101 //@} 00102 00103 /** @name Assignment operators* / 00104 //@{ 00105 00106 //! Assign from another Type. 00107 Type& operator=(Type const& type) { M_bits = type.M_bits; return* this; } 00108 00109 //! Assign from a constant. 00110 Type& operator=(TypeData type) { M_bits = type.M_bits; return* this; } 00111 00112 //@} 00113 00114 /** @name Comparison operators* / 00115 //@{ 00116 00117 friend bool operator==(Type const& t1, Type const& t2) { return t1.M_bits == t2.M_bits; } 00118 friend bool operator==(Type const& t1, TypeData t2) { return t1.M_bits == t2.M_bits; } 00119 friend bool operator==(TypeData t1, Type const& t2) { return t1.M_bits == t2.M_bits; } 00120 friend bool operator!=(Type const& t1, Type const& t2) { return t1.M_bits != t2.M_bits; } 00121 friend bool operator!=(Type const& t1, TypeData t2) { return t1.M_bits != t2.M_bits; } 00122 friend bool operator!=(TypeData t1, Type const& t2) { return t1.M_bits != t2.M_bits; } 00123 00124 //@} 00125 00126 /** @name Accessors* / 00127 //@{ 00128 00129 //! Returns TRUE if the type is a bishop, rook or queen. 00130 bool is_a_slider(void) const { return M_bits > 4; } 00131 00132 //! Returns TRUE if the type is a rook or queen. 00133 bool is_a_rookmover(void) const { return (M_bits & rook_bits) == rook_bits; } 00134 00135 //! Returns TRUE if the type is a bishop or queen. 00136 bool is_a_bishopmover(void) const { return (M_bits & bishop_bits) == bishop_bits; } 00137 00138 //! Return the underlaying integral value. 00139 uint8_t operator()(void) const { return M_bits; } 00140 00141 //@} 00142 00143 private: 00144 friend class Code; 00145 // Constructor for class Code. 00146 explicit Type(uint8_t bits) { M_bits = bits; } 00147 }; 00148 00149 } // namespace cwchess 00150 00151 #endif // TYPE_H