Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef CASTLEFLAGS_H
00025 #define CASTLEFLAGS_H
00026
00027 #ifndef USE_PCH
00028 #endif
00029
00030 #include "Code.h"
00031 #include "Index.h"
00032 #include "Piece.h"
00033
00034 namespace cwchess {
00035
00036 uint8_t const black_rook_queen_side_moved = 1;
00037 uint8_t const black_rook_king_side_moved = 2;
00038 uint8_t const black_king_moved = 4;
00039 uint8_t const black_king_in_check = 8;
00040 uint8_t const white_king_in_check = 16;
00041 uint8_t const white_rook_queen_side_moved = 32;
00042 uint8_t const white_rook_king_side_moved = 64;
00043 uint8_t const white_king_moved = 128;
00044
00045 class ChessPosition;
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 class CastleFlags {
00064 private:
00065 uint8_t M_bits;
00066
00067 friend class ChessPosition;
00068
00069 CastleFlags(void) : M_bits(0) { }
00070 CastleFlags& operator=(uint8_t bits) { M_bits = bits; return* this; }
00071
00072
00073 void clear(void) { M_bits = 231; }
00074
00075
00076 void update_removed(Code const& code, Index const& index)
00077 {
00078 if (code == white_rook)
00079 {
00080 if (index == ia1)
00081 M_bits |= white_rook_queen_side_moved;
00082 else if (index == ih1)
00083 M_bits |= white_rook_king_side_moved;
00084 }
00085 else if (code == black_rook)
00086 {
00087 if (index == ia8)
00088 M_bits |= black_rook_queen_side_moved;
00089 else if (index == ih8)
00090 M_bits |= black_rook_king_side_moved;
00091 }
00092 else if(code == white_king)
00093 {
00094 if (index == ie1)
00095 M_bits |= white_king_moved;
00096 }
00097 else if (code == black_king)
00098 {
00099 if (index == ie8)
00100 M_bits |= black_king_moved;
00101 }
00102 }
00103
00104
00105 void update_placed(Code const& code, Index const& index)
00106 {
00107 if (code == white_rook)
00108 {
00109 if (index == ia1)
00110 M_bits& = ~white_rook_queen_side_moved;
00111 else if (index == ih1)
00112 M_bits& = ~white_rook_king_side_moved;
00113 }
00114 else if (code == black_rook)
00115 {
00116 if (index == ia8)
00117 M_bits& = ~black_rook_queen_side_moved;
00118 else if (index == ih8)
00119 M_bits& = ~black_rook_king_side_moved;
00120 }
00121 else if (code == white_king)
00122 {
00123 if (index == ie1)
00124 M_bits& = ~white_king_moved;
00125 }
00126 else if (code == black_king)
00127 {
00128 if (index == ie8)
00129 M_bits& = ~black_king_moved;
00130 }
00131 }
00132
00133
00134 void piece_moved_from(Piece const& piece, Index const& from);
00135
00136 public:
00137
00138 bool can_castle(Color const& color) const { return ((M_bits >> ((color == black) ? 0 : 5)) & 7) < 3; }
00139
00140
00141 bool can_castle_short(Color const& color) const
00142 {
00143
00144 if (__builtin_expect((M_bits & (black_king_moved | white_king_moved)) == (black_king_moved | white_king_moved), 1))
00145 return false;
00146
00147 uint8_t mask = color.is_white() ? (white_king_moved | white_rook_king_side_moved) : (black_king_moved | black_rook_king_side_moved);
00148
00149 return !(M_bits & mask);
00150 }
00151
00152
00153 bool can_castle_long(Color const& color) const
00154 {
00155
00156 if (__builtin_expect((M_bits & (black_king_moved | white_king_moved)) == (black_king_moved | white_king_moved), 1))
00157 return false;
00158
00159 uint8_t mask = color.is_white() ? (white_king_moved | white_rook_queen_side_moved) : (black_king_moved | black_rook_queen_side_moved);
00160
00161 return !(M_bits & mask);
00162 }
00163
00164
00165 void set_check(Color const& color, bool check) { uint8_t flag = 8 + color(); if (check) M_bits |= flag; else M_bits& = ~flag; }
00166
00167
00168 bool in_check(Color const& color) const { uint8_t flag = 8 + color(); return (M_bits & flag); }
00169
00170
00171 bool has_moved(Code const& code, Index const& index);
00172 };
00173
00174 }
00175
00176 #endif // CASTLEFLAGS_H