CastleFlags.h
Go to the documentation of this file.
1 // cwchessboard -- A C++ chessboard tool set
2 //
3 //! @file CastleFlags.h This file contains the declaration of class CastleFlags.
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 CASTLEFLAGS_H
25 #define CASTLEFLAGS_H
26 
27 #ifndef USE_PCH
28 #endif
29 
30 #include "Code.h"
31 #include "Index.h"
32 #include "Piece.h"
33 
34 namespace cwchess {
35 
36 uint8_t const black_rook_queen_side_moved = 1;
37 uint8_t const black_rook_king_side_moved = 2;
38 uint8_t const black_king_moved = 4;
39 uint8_t const black_king_in_check = 8;
40 uint8_t const white_king_in_check = 16;
41 uint8_t const white_rook_queen_side_moved = 32;
42 uint8_t const white_rook_king_side_moved = 64;
43 uint8_t const white_king_moved = 128;
44 
45 class ChessPosition;
46 
47 /** @brief A class to keep track of castling rights.
48 *
49  * This class is for internal use. ChessPosition takes cares
50  * of keeping track of castling rights and the user should
51  * never have to access or use this class.
52 *
53  * Each ChessPosition has one CastleFlags member that keeps
54  * track of which rook and king moved. Castling is only
55  * allowed when neither, the king nor the rook used to castle,
56  * did move.
57 *
58  * This object has no notion of temporary reasons why castling
59  * might not be allowed. However, two unused bits are used
60  * to store whether or not white/black is in check (that is not
61  * used for the castling part though).
62 * /
63 class CastleFlags {
64  private:
65  uint8_t M_bits;
66 
67  friend class ChessPosition;
68 
69  CastleFlags(void) : M_bits(0) { }
70  CastleFlags& operator=(uint8_t bits) { M_bits = bits; return* this; }
71 
72  // Called when all pieces are removed from the board.
73  void clear(void) { M_bits = 231; }
74 
75  // Called if \a code was removed from \a index.
76  void update_removed(Code const& code, Index const& index)
77  {
78  if (code == white_rook)
79  {
80  if (index == ia1)
81  M_bits |= white_rook_queen_side_moved;
82  else if (index == ih1)
83  M_bits |= white_rook_king_side_moved;
84  }
85  else if (code == black_rook)
86  {
87  if (index == ia8)
88  M_bits |= black_rook_queen_side_moved;
89  else if (index == ih8)
90  M_bits |= black_rook_king_side_moved;
91  }
92  else if(code == white_king)
93  {
94  if (index == ie1)
95  M_bits |= white_king_moved;
96  }
97  else if (code == black_king)
98  {
99  if (index == ie8)
100  M_bits |= black_king_moved;
101  }
102  }
103 
104  // Called if \a code was placed at \a index.
105  void update_placed(Code const& code, Index const& index)
106  {
107  if (code == white_rook)
108  {
109  if (index == ia1)
110  M_bits& = ~white_rook_queen_side_moved;
111  else if (index == ih1)
112  M_bits& = ~white_rook_king_side_moved;
113  }
114  else if (code == black_rook)
115  {
116  if (index == ia8)
117  M_bits& = ~black_rook_queen_side_moved;
118  else if (index == ih8)
119  M_bits& = ~black_rook_king_side_moved;
120  }
121  else if (code == white_king)
122  {
123  if (index == ie1)
124  M_bits& = ~white_king_moved;
125  }
126  else if (code == black_king)
127  {
128  if (index == ie8)
129  M_bits& = ~black_king_moved;
130  }
131  }
132 
133  // Called if the king or rook \a piece (initial position \a from) moved.
134  void piece_moved_from(Piece const& piece, Index const& from);
135 
136  public:
137  //! Return TRUE if \a color is still allowed to castle at all (not taking into account checks).
138  bool can_castle(Color const& color) const { return ((M_bits >> ((color == black) ? 0 : 5)) & 7) < 3; }
139 
140  //! Return TRUE if \a color is still allowed to castle short (not taking into account checks).
141  bool can_castle_short(Color const& color) const
142  {
143  // Speed up for the case that both, the black and the white king already moved.
144  if (__builtin_expect((M_bits & (black_king_moved | white_king_moved)) == (black_king_moved | white_king_moved), 1))
145  return false;
146  // Calculate mask: the king and the appropriate rook.
147  uint8_t mask = color.is_white() ? (white_king_moved | white_rook_king_side_moved) : (black_king_moved | black_rook_king_side_moved);
148  // Neither may have moved.
149  return !(M_bits & mask);
150  }
151 
152  //! Return TRUE if \a color is still allowed to castle long (not taking into account checks).
153  bool can_castle_long(Color const& color) const
154  {
155  // Speed up for the case that both, the black and the white king already moved.
156  if (__builtin_expect((M_bits & (black_king_moved | white_king_moved)) == (black_king_moved | white_king_moved), 1))
157  return false;
158  // Calculate mask: the king and the appropriate rook.
159  uint8_t mask = color.is_white() ? (white_king_moved | white_rook_queen_side_moved) : (black_king_moved | black_rook_queen_side_moved);
160  // Neither may have moved.
161  return !(M_bits & mask);
162  }
163 
164  // Set the 'in_check' bit.
165  void set_check(Color const& color, bool check) { uint8_t flag = 8 + color(); if (check) M_bits |= flag; else M_bits& = ~flag; }
166 
167  // Retrieve the 'in_check' bit.
168  bool in_check(Color const& color) const { uint8_t flag = 8 + color(); return (M_bits & flag); }
169 
170  //! Return TRUE if \a code at \a index is marked as having moved.
171  bool has_moved(Code const& code, Index const& index);
172 };
173 
174 } // namespace cwchess
175 
176 #endif // CASTLEFLAGS_H
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 Index.
IndexData const ia1
A constant representing the index to square a1.
Definition: Index.h:53
IndexData const ih1
A constant representing the index to square h1.
Definition: Index.h:67
bool has_moved(Code const& code, Index const& index)
Return TRUE if code at index is marked as having moved.
Definition: CastleFlags.cc:60
IndexData const ih8
A constant representing the index to square h8.
Definition: Index.h:179
IndexData const ia8
A constant representing the index to square a8.
Definition: Index.h:165
CodeData const black_rook
A constant representing a black rook;.
Definition: Code.h:68
The index of a chess square.
Definition: Index.h:211
CodeData const white_rook
A constant representing a white rook.
Definition: Code.h:56
A class to keep track of castling rights.
Definition: CastleFlags.h:63
A particular piece on the board.
Definition: Piece.h:45
bool can_castle(Color const& color) const
Return TRUE if color is still allowed to castle at all (not taking into account checks).
Definition: CastleFlags.h:138
bool is_white(void) const
Return TRUE if this color is white.
Definition: Color.h:111
A chess position.
Definition: ChessPosition.h:50
bool can_castle_long(Color const& color) const
Return TRUE if color is still allowed to castle long (not taking into account checks).
Definition: CastleFlags.h:153
A chess piece type including color.
Definition: Code.h:92
CodeData const black_king
A constant representing a black king;.
Definition: Code.h:76
A color (black or white).
Definition: Color.h:67
CodeData const white_king
A constant representing a white king.
Definition: Code.h:64
IndexData const ie8
A constant representing the index to square e8.
Definition: Index.h:173
This file contains the declaration of class Code.
This file contains the declaration of class Piece.
bool can_castle_short(Color const& color) const
Return TRUE if color is still allowed to castle short (not taking into account checks).
Definition: CastleFlags.h:141
IndexData const ie1
A constant representing the index to square e1.
Definition: Index.h:61
ColorData const black
A constant representing the color black.
Definition: Color.h:53

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