Code.h
Go to the documentation of this file.
1 // cwchessboard -- A C++ chessboard tool set
2 //
3 //! @file Code.h This file contains the declaration of class Code.
4 //
5 // Copyright (C) 2008, by
6 //
7 // Carlo Wood, Run on IRC <carlo@alinoe.com>
8 // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt
9 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 2 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 
24 #ifndef CODE_H
25 #define CODE_H
26 
27 #ifndef USE_PCH
28 #include <stdint.h>
29 #endif
30 
31 #include "Color.h"
32 #include "Type.h"
33 #include "Direction.h"
34 
35 namespace cwchess {
36 
37 /** @brief The POD base type of class Code.
38 *
39  * This class represents a chess piece, either
40  * pawn, rook, knight, bishop, queen or king,
41  * and it's color, white or black.
42 *
43  * See ColorData and TypeData for the encoding of the bits.
44 *
45  * @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
46 *
47  * In order to refer to 'nothing', use the default constructor Code().
48 * /
49 struct CodeData {
50  uint8_t M_bits; //!< 0000CTTT, where C is the color and TTT the type.
51 };
52 
53 //! A constant representing a white pawn.
55 //! A constant representing a white rook.
57 //! A constant representing a white knight.
59 //! A constant representing a white bishop.
61 //! A constant representing a white queen.
63 //! A constant representing a white king.
65 //! A constant representing a black pawn.
67 //! A constant representing a black rook;
69 //! A constant representing a black knight;
71 //! A constant representing a black bishop;
73 //! A constant representing a black queen;
75 //! A constant representing a black king;
77 
78 // Compare constants (this should never be needed, but why not add it).
79 inline bool operator==(CodeData c1, CodeData c2) { return c1.M_bits == c2.M_bits; }
80 inline bool operator!=(CodeData c1, CodeData c2) { return c1.M_bits != c2.M_bits; }
81 
82 // Used to cast Code to CwChessboardCode.
83 typedef uint16_t CwChessboardCode;
84 
85 /** @brief A chess piece type including color.
86 *
87  * This class represents a code for a chess piece
88  * that includes it's Type as well as it's Color.
89 *
90  * See CodeData for defined constants.
91 * /
92 class Code : protected CodeData {
93  public:
94 
95  /** @name Constructors* /
96  //@{
97 
98  //! Construct a Code object initialized as 'nothing'.
99  Code(void) { M_bits = 0; }
100 
101  //! Copy-constructor.
102  Code(Code const& code) { M_bits = code.M_bits; }
103 
104  //! Construct a Code object from a constant.
105  Code(CodeData code) { M_bits = code.M_bits; }
106 
107  //! Construct a Code object with color \a color and type \a type.
108  Code(Color const& color, Type const& type) { M_bits = color.M_bits | type.M_bits; }
109 
110  //! Explicit conversion from CwChessboardCode to Code.
111  explicit Code(CwChessboardCode code) { M_bits = CwChessboardCode_to_Code[code].M_bits; }
112 
113  //@}
114 
115  /** @name Assigment operators* /
116  //@{
117 
118  //! Assign from another Code.
119  Code& operator=(Code const& code) { M_bits = code.M_bits; return* this; }
120 
121  //! Assign from a constant.
122  Code& operator=(CodeData code) { M_bits = code.M_bits; return* this; }
123 
124  //! Change the type to \a type. Type may not be nothing (use clear() instead).
125  Code& operator=(Type const& type) { M_bits& = ~type_mask; M_bits |= type.M_bits; return* this; }
126 
127  //! Change the color to \a color.
128  Code& operator=(Color const& color) { M_bits& = ~color_mask; M_bits |= color.M_bits; return* this; }
129 
130  //! Set the type to nothing.
131  void clear(void) { M_bits = 0; }
132 
133  //@}
134 
135  /** @name Comparison operators* /
136  //@{
137 
138  friend bool operator==(Code const& c1, Code const& c2) { return c1.M_bits == c2.M_bits; }
139  friend bool operator==(Code const& c1, CodeData c2) { return c1.M_bits == c2.M_bits; }
140  friend bool operator==(CodeData c1, Code const& c2) { return c1.M_bits == c2.M_bits; }
141  friend bool operator!=(Code const& c1, Code const& c2) { return c1.M_bits != c2.M_bits; }
142  friend bool operator!=(Code const& c1, CodeData c2) { return c1.M_bits != c2.M_bits; }
143  friend bool operator!=(CodeData c1, Code const& c2) { return c1.M_bits != c2.M_bits; }
144 
145  //@}
146 
147  /** @name Accessors* /
148  //@{
149 
150  //! Returns TRUE if the type is a bishop, rook or queen.
151  bool is_a_slider(void) const { return (M_bits & type_mask) > 4; }
152 
153  //! Returns TRUE if the type is a rook or queen.
154  bool is_a_rookmover(void) const { return (M_bits & rook_bits) == rook_bits; }
155 
156  //! Returns TRUE if the type is a bishop or queen.
157  bool is_a_bishopmover(void) const { return (M_bits & bishop_bits) == bishop_bits; }
158 
159  //! Returns TRUE if the code represents 'nothing'.
160  bool is_nothing(void) const { return M_bits == 0; }
161 
162  //! Returns TRUE if the type is equal.
163  bool is_a(Type const& type) const { return (M_bits & type_mask) == type.M_bits; }
164 
165  //! Returns TRUE if the type is equal.
166  bool is_a(TypeData type) const { return (M_bits & type_mask) == type.M_bits; }
167 
168  //! Return TRUE if the color is equal.
169  bool is(Color const& color) const { return (M_bits & color_mask) == color.M_bits; }
170 
171  //! Return TRUE if the color is equal.
172  bool is(ColorData color) const { return (M_bits & color_mask) == color.M_bits; }
173 
174  //! Return TRUE if the colors are different.
175  bool has_opposite_color_of(Code const& code) { return (M_bits ^ code.M_bits) & color_mask; }
176 
177  //! Return TRUE if this piece moves along \a direction.
178  bool moves_along(Direction const& direction) { return (M_bits & direction.mover_flags().M_bits) == direction.mover_flags().M_bits; }
179 
180  //! Return the Type of this Code.
181  Type type(void) const { return Type(M_bits & type_mask); }
182 
183  //! Return the Color of this Code.
184  Color color(void) const { return Color(M_bits & color_mask); }
185 
186  //! Return the unlaying integral value.
187  uint8_t operator()(void) const { return M_bits; }
188 
189  //@}
190 
191  /** @name Special functions* /
192  //@{
193 
194  //! Toggle the color. May not be used on type 'nothing'.
195  void toggle_color(void) { M_bits ^= color_mask; }
196 
197  //! Casting operator.
198  operator CwChessboardCode(void) const { return Code_to_CwChessboardCode[M_bits]; }
199 
200  //@}
201 
202  private:
203  static CwChessboardCode Code_to_CwChessboardCode[16];
204  static CodeData CwChessboardCode_to_Code[14];
205 };
206 
207 } // namespace cwchess
208 
209 #endif // CODE_H
CodeData const black_pawn
A constant representing a black pawn.
Definition: Code.h:66
The POD base type of class Code.
Definition: Code.h:49
A namespace for all chess related objects that are not related to the GUI.
Definition: Array.h:39
This file contains the declaration of class Type.
bool is(Color const& color) const
Return TRUE if the color is equal.
Definition: Code.h:169
uint8_t operator()(void) const
Return the unlaying integral value.
Definition: Code.h:187
CodeData const white_queen
A constant representing a white queen.
Definition: Code.h:62
bool is_nothing(void) const
Returns TRUE if the code represents& #39;nothing&#39;.
Definition: Code.h:160
This file contains the declaration of class Direction.
Code(CodeData code)
Construct a Code object from a constant.
Definition: Code.h:105
Code(Color const& color, Type const& type)
Construct a Code object with color color and type type.
Definition: Code.h:108
CodeData const black_queen
A constant representing a black queen;.
Definition: Code.h:74
Code(void)
Construct a Code object initialized as& #39;nothing&#39;.
Definition: Code.h:99
Code & operator=(Code const& code)
Assign from another Code.
Definition: Code.h:119
Code & operator=(Color const& color)
Change the color to color.
Definition: Code.h:128
uint8_t const bishop_bits
The underlaying integral value of type& #39;bishop&#39;.
Definition: Type.h:57
bool moves_along(Direction const& direction)
Return TRUE if this piece moves along direction.
Definition: Code.h:178
uint8_t M_bits
0TKNQ000, T=can move two squares, K=can take king side, N=is not blocked, Q=can take queen side...
Definition: Flags.h:38
CodeData const black_rook
A constant representing a black rook;.
Definition: Code.h:68
BitBoardData const c1
The square c1.
Definition: BitBoard.h:98
bool is_a_slider(void) const
Returns TRUE if the type is a bishop, rook or queen.
Definition: Code.h:151
A chess piece type.
Definition: Type.h:87
Code & operator=(Type const& type)
Change the type to type. Type may not be nothing (use clear() instead).
Definition: Code.h:125
uint8_t M_bits
00000STT, where STT is the type. If S == 1 then the piece is a slider.
Definition: Type.h:50
bool has_opposite_color_of(Code const& code)
Return TRUE if the colors are different.
Definition: Code.h:175
Code(CwChessboardCode code)
Explicit conversion from CwChessboardCode to Code.
Definition: Code.h:111
CodeData const white_rook
A constant representing a white rook.
Definition: Code.h:56
The POD base type of class Color.
Definition: Color.h:44
bool is(ColorData color) const
Return TRUE if the color is equal.
Definition: Code.h:172
void clear(void)
Set the type to nothing.
Definition: Code.h:131
bool is_a(Type const& type) const
Returns TRUE if the type is equal.
Definition: Code.h:163
uint8_t const white_bits
The underlaying integral value of color& #39;white&#39;.
Definition: Color.h:49
bool is_a(TypeData type) const
Returns TRUE if the type is equal.
Definition: Code.h:166
uint8_t const king_bits
The underlaying integral value of type& #39;king&#39;.
Definition: Type.h:56
CodeData const black_bishop
A constant representing a black bishop;.
Definition: Code.h:72
bool is_a_rookmover(void) const
Returns TRUE if the type is a rook or queen.
Definition: Code.h:154
A chess piece type including color.
Definition: Code.h:92
This file contains the declaration of class Color.
Code(Code const& code)
Copy-constructor.
Definition: Code.h:102
Code & operator=(CodeData code)
Assign from a constant.
Definition: Code.h:122
uint8_t const black_bits
The underlaying integral value of color& #39;black&#39;.
Definition: Color.h:48
bool is_a_bishopmover(void) const
Returns TRUE if the type is a bishop or queen.
Definition: Code.h:157
CodeData const black_king
A constant representing a black king;.
Definition: Code.h:76
Type type(void) const
Return the Type of this Code.
Definition: Code.h:181
uint8_t const color_mask
A mask for the bits used for the color of a piece.
Definition: Color.h:50
CodeData const white_pawn
A constant representing a white pawn.
Definition: Code.h:54
CodeData const black_knight
A constant representing a black knight;.
Definition: Code.h:70
A color (black or white).
Definition: Color.h:67
CodeData const white_king
A constant representing a white king.
Definition: Code.h:64
uint8_t const type_mask
A mask for the bits used for the type of a piece.
Definition: Type.h:60
uint8_t M_bits
0000C000, where C=0 means black and C=1 means white.
Definition: Color.h:45
uint8_t const knight_bits
The underlaying integral value of type& #39;knight&#39;.
Definition: Type.h:55
void toggle_color(void)
Toggle the color. May not be used on type& #39;nothing&#39;.
Definition: Code.h:195
The POD base type of class Type.
Definition: Type.h:49
Color color(void) const
Return the Color of this Code.
Definition: Code.h:184
uint8_t const queen_bits
The underlaying integral value of type& #39;queen&#39;.
Definition: Type.h:59
CodeData const white_bishop
A constant representing a white bishop.
Definition: Code.h:60
BitBoardData const c2
The square c2.
Definition: BitBoard.h:106
CodeData const white_knight
A constant representing a white knight.
Definition: Code.h:58
uint8_t M_bits
0000CTTT, where C is the color and TTT the type.
Definition: Code.h:50
uint8_t const pawn_bits
The underlaying integral value of type& #39;pawn&#39;.
Definition: Type.h:54
uint8_t const rook_bits
The underlaying integral value of type& #39;rook&#39;.
Definition: Type.h:58

Copyright © 2006 - 2010 Carlo Wood.  All rights reserved.