Piece.h
Go to the documentation of this file.
1 // cwchessboard -- A C++ chessboard tool set
2 //
3 //! @file Piece.h This file contains the declaration of class Piece.
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 PIECE_H
25 #define PIECE_H
26 
27 #ifndef USE_PCH
28 #endif
29 
30 #include "Code.h"
31 #include "Flags.h"
32 
33 namespace cwchess {
34 
35 /** @brief A particular piece on the board.
36 *
37  * This class represents pieces on the chessboard
38  * (in the broadest sense of 'piece', including pawn, queen, king and even nothing).
39 *
40  * Since the flags are hidden and not of the users concern, you can
41  * consider a Piece to be equivalent with Code.
42 *
43  * @sa ChessPosition::piece_at
44 * /
45 class Piece {
46  protected:
47  Code M_code; //!< The Code of this piece.
48  Flags M_flags; //!< Flags for this piece.
49 
50  public:
51 
52  /** @name Constructors* /
53  //@{
54 
55  //! Construct an empty piece (nothing).
56  Piece(void) : M_code(), M_flags(fl_none) { }
57 
58  /** @brief Construct a fully initialized Piece.
59  *
60  * @param color : The color of the piece.
61  * @param type : The type of the piece.
62  * @param flags : All flags.
63  * /
64  Piece(Color const& color, Type const& type, Flags const& flags) : M_code(color, type), M_flags(flags) { }
65 
66  /** @brief Construct a fully initialized Piece with all Flags reset.
67  *
68  * @param color : The color of the piece.
69  * @param type : The type of the piece.
70  * /
71  Piece(Color const& color, Type const& type) : M_code(color, type), M_flags(fl_none) { }
72 
73  /** @brief Construct a fully initialized Piece.
74  *
75  * @param code : The chess piece Code.
76  * @param flags : All flags.
77  * /
78  Piece(Code const& code, Flags const& flags) : M_code(code), M_flags(flags) { }
79 
80  //! Copy-constructor.
81  Piece(Piece const& piece) : M_code(piece.M_code), M_flags(piece.M_flags) { }
82 
83  //@}
84 
85  /** @name Assignment operators* /
86  //@{
87 
88  //! Assign from another Piece.
89  Piece& operator=(Piece const& piece) { M_code = piece.M_code; M_flags = piece.M_flags; return* this; }
90 
91  //! Just change the color.
92  Piece& operator=(Color const& color) { M_code = color; return* this; }
93 
94  //! Just change the type.
95  Piece& operator=(Type const& type) { M_code = type; return* this; }
96 
97  //! Just change the code.
98  Piece& operator=(Code const& code) { M_code = code; return* this; }
99 
100  //! Just change the flags.
101  Piece& operator=(Flags const& flags) { M_flags = flags; return* this; }
102 
103  //@}
104 
105  /** @name Accessors* /
106  //@{
107 
108  /** @brief Return the color of the piece.
109  *
110  * Only valid if the type is not 'nothing'.
111  * /
112  Color color(void) const { return M_code.color(); }
113 
114  //! The type of this piece.
115  Type type(void) const { return M_code.type(); }
116 
117  //! The flags of this piece.
118  Flags flags(void) const { return M_flags; }
119 
120  //! The code of this piece.
121  Code code(void) const { return M_code; }
122 
123  //@}
124 
125  private:
126  friend class ChessPosition;
127 #ifdef PIECE_TEST_H
128  friend class testsuite::PieceTest;
129 #endif
130 
131  //! Change the flags of this piece.
132  void set_flags(Flags const& flags) { M_flags = flags; }
133 
134  //! Change the type of this piece.
135  void set_type(Type const& type) { if (type == nothing) { M_code.clear(); M_flags.clear(); } else M_code = type; }
136 
137  //! Reset the 'can take queen side' flag, if any.
138  void reset_can_take_queen_side(void) { M_flags.reset(fl_pawn_can_take_queen_side); }
139 
140  //! Reset the 'can take king side' flag, if any.
141  void reset_can_take_king_side(void) { M_flags.reset(fl_pawn_can_take_king_side); }
142 
143  //! Reset the 'is not blocked' flag, if any.
144  void reset_is_not_blocked(void) { M_flags.reset(fl_pawn_is_not_blocked|fl_pawn_can_move_two_squares); }
145 
146  //! Reset the 'can move two squares' flag, if any.
147  void reset_can_move_two_squares(void) { M_flags.reset(fl_pawn_can_move_two_squares); }
148 
149  //! Set the 'can take queen side' flag.
150  void set_can_take_queen_side(void) { M_flags.set(fl_pawn_can_take_queen_side); }
151 
152  //! Set the 'can take king side' flag.
153  void set_can_take_king_side(void) { M_flags.set(fl_pawn_can_take_king_side); }
154 
155  //! Set the 'is not blocked' flag.
156  void set_is_not_blocked(void) { M_flags.set(fl_pawn_is_not_blocked); }
157 
158  //! Set the 'can move two squares' flag.
159  void set_can_move_two_squares(void) { M_flags.set(fl_pawn_can_move_two_squares); }
160 
161  //! Copy the 'is not blocked' flag to the 'can move two squares' flag, if set.
162  void set_can_move_two_squares_if_not_blocked(void) { M_flags.set_can_move_two_squares_if_not_blocked(); }
163 
164  public:
165  /** @name Comparison operators* /
166  //@{
167 
168  /** @brief Return TRUE if the Code of the pieces are equal.
169  *
170  * This operator ignores the flags.
171  * /
172  friend bool operator==(Piece const& piece1, Piece const& piece2) { return piece1.M_code == piece2.M_code; }
173 
174  /** @brief Return TRUE if the Code of the pieces are unequal.
175  *
176  * This operator ignores the flags.
177  * /
178  friend bool operator!=(Piece const& piece1, Piece const& piece2) { return piece1.M_code != piece2.M_code; }
179 
180  //! Return TRUE if \a piece is a \a type.
181  friend bool operator==(Piece const& piece, Type const& type) { return piece.M_code.is_a(type); }
182 
183  //! Return TRUE if \a piece is a \a type.
184  friend bool operator==(Type const& type, Piece const& piece) { return piece.M_code.is_a(type); }
185 
186  //! Return TRUE if \a piece is not a \a type.
187  friend bool operator!=(Piece const& piece, Type const& type) { return !piece.M_code.is_a(type); }
188 
189  //! Return TRUE if \a piece is not a \a type.
190  friend bool operator!=(Type const& type, Piece const& piece) { return !piece.M_code.is_a(type); }
191 
192  //! Return TRUE if \a piece has color \a color.
193  friend bool operator==(Piece const& piece, Color const& color) { return piece.M_code.is(color); }
194 
195  //! Return TRUE if \a piece has color \a color.
196  friend bool operator==(Color const& color, Piece const& piece) { return piece.M_code.is(color); }
197 
198  //! Return TRUE if \a piece does not have color \a color.
199  friend bool operator!=(Piece const& piece, Color const& color) { return !piece.M_code.is(color); }
200 
201  //! Return TRUE if \a piece does not have color \a color.
202  friend bool operator!=(Color const& color, Piece const& piece) { return !piece.M_code.is(color); }
203 
204  //! Return TRUE if \a piece has code \a code.
205  friend bool operator==(Piece const& piece, Code const& code) { return piece.M_code == code; }
206 
207  //! Return TRUE if \a piece has code \a code.
208  friend bool operator==(Code const& code, Piece const& piece) { return piece.M_code == code; }
209 
210  //! Return TRUE if \a piece does not have code \a code.
211  friend bool operator!=(Piece const& piece, Code const& code) { return piece.M_code != code; }
212 
213  //! Return TRUE if \a piece does not have code \a code.
214  friend bool operator!=(Code const& code, Piece const& piece) { return piece.M_code != code; }
215 
216  //@}
217 };
218 
219 } // namespace cwchess
220 
221 #endif // PIECE_H
Piece & operator=(Flags const& flags)
Just change the flags.
Definition: Piece.h:101
A namespace for all chess related objects that are not related to the GUI.
Definition: Array.h:39
friend bool operator!=(Piece const& piece, Type const& type)
Return TRUE if piece is not a type.
Definition: Piece.h:187
Piece & operator=(Piece const& piece)
Assign from another Piece.
Definition: Piece.h:89
bool is(Color const& color) const
Return TRUE if the color is equal.
Definition: Code.h:169
void set(FlagsData flags)
Set the bits that are set in flags.
Definition: Flags.h:149
friend bool operator!=(Type const& type, Piece const& piece)
Return TRUE if piece is not a type.
Definition: Piece.h:190
friend bool operator==(Piece const& piece1, Piece const& piece2)
Return TRUE if the Code of the pieces are equal.
Definition: Piece.h:172
FlagsData const fl_pawn_is_not_blocked
A constant representing the flag& #39;pawn is not blocked&#39;.
Definition: Flags.h:47
friend bool operator==(Color const& color, Piece const& piece)
Return TRUE if piece has color color.
Definition: Piece.h:196
friend bool operator!=(Piece const& piece, Color const& color)
Return TRUE if piece does not have color color.
Definition: Piece.h:199
Piece(void)
Construct an empty piece (nothing).
Definition: Piece.h:56
Flags M_flags
Flags for this piece.
Definition: Piece.h:48
Flags representing the state of a piece on the chessboard.
Definition: Flags.h:85
friend bool operator==(Code const& code, Piece const& piece)
Return TRUE if piece has code code.
Definition: Piece.h:208
friend bool operator==(Piece const& piece, Code const& code)
Return TRUE if piece has code code.
Definition: Piece.h:205
Piece(Piece const& piece)
Copy-constructor.
Definition: Piece.h:81
Piece & operator=(Color const& color)
Just change the color.
Definition: Piece.h:92
Piece & operator=(Code const& code)
Just change the code.
Definition: Piece.h:98
friend bool operator==(Type const& type, Piece const& piece)
Return TRUE if piece is a type.
Definition: Piece.h:184
friend bool operator==(Piece const& piece, Type const& type)
Return TRUE if piece is a type.
Definition: Piece.h:181
FlagsData const fl_pawn_can_take_king_side
A constant representing the flag& #39;pawn can take king side&#39;.
Definition: Flags.h:48
Type type(void) const
The type of this piece.
Definition: Piece.h:115
A chess piece type.
Definition: Type.h:87
void reset(FlagsData flags)
Clear the bits that are set in flags.
Definition: Flags.h:152
Piece(Code const& code, Flags const& flags)
Construct a fully initialized Piece.
Definition: Piece.h:78
A particular piece on the board.
Definition: Piece.h:45
friend bool operator!=(Piece const& piece1, Piece const& piece2)
Return TRUE if the Code of the pieces are unequal.
Definition: Piece.h:178
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
friend bool operator==(Piece const& piece, Color const& color)
Return TRUE if piece has color color.
Definition: Piece.h:193
friend bool operator!=(Piece const& piece, Code const& code)
Return TRUE if piece does not have code code.
Definition: Piece.h:211
Color color(void) const
Return the color of the piece.
Definition: Piece.h:112
A chess position.
Definition: ChessPosition.h:50
A chess piece type including color.
Definition: Code.h:92
Piece(Color const& color, Type const& type)
Construct a fully initialized Piece with all Flags reset.
Definition: Piece.h:71
FlagsData const fl_pawn_can_move_two_squares
A constant representing the flag& #39;pawn can move two squares&#39;.
Definition: Flags.h:49
Code M_code
The Code of this piece.
Definition: Piece.h:47
TypeData const nothing
A constant representing the absence of a piece.
Definition: Type.h:63
void set_can_move_two_squares_if_not_blocked(void)
Set the fl_pawn_can_move_two_squares bit iff fl_pawn_is_not_blocked bit is already set...
Definition: Flags.h:160
Type type(void) const
Return the Type of this Code.
Definition: Code.h:181
Flags flags(void) const
The flags of this piece.
Definition: Piece.h:118
A color (black or white).
Definition: Color.h:67
This file contains the declaration of class Flags.
Piece & operator=(Type const& type)
Just change the type.
Definition: Piece.h:95
This file contains the declaration of class Code.
Color color(void) const
Return the Color of this Code.
Definition: Code.h:184
Piece(Color const& color, Type const& type, Flags const& flags)
Construct a fully initialized Piece.
Definition: Piece.h:64
friend bool operator!=(Color const& color, Piece const& piece)
Return TRUE if piece does not have color color.
Definition: Piece.h:202
FlagsData const fl_pawn_can_take_queen_side
A constant representing the flag& #39;pawn can take queen side&#39;.
Definition: Flags.h:46
void clear(void)
Set all flags to 0.
Definition: Flags.h:111
friend bool operator!=(Code const& code, Piece const& piece)
Return TRUE if piece does not have code code.
Definition: Piece.h:214
Code code(void) const
The code of this piece.
Definition: Piece.h:121

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