1 /* Copyright (C) 2007-2011 Vincent Ollivier
3 * Purple Haze is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * Purple Haze is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * A move is coded using 16 bits:
30 * 3 bits for the destination square rank
31 * 3 bits for the destination square file
32 * 3 bits for the origin square rank
33 * 3 bits for the origin square file
36 static const int MT_MASK
= 0xF;
37 static const int DR_MASK
= 0x7;
38 static const int DF_MASK
= 0x7;
39 static const int OR_MASK
= 0x7;
40 static const int OF_MASK
= 0x7;
42 static const int MT_SHIFT
= 0;
43 static const int DR_SHIFT
= 4;
44 static const int DF_SHIFT
= 7;
45 static const int OR_SHIFT
= 10;
46 static const int OF_SHIFT
= 13;
50 bool is_set(int i
) const { return code
& ( 1 << i
); };
53 Move() : code(NULL_MOVE
) {}
55 Move(Square o
, Square d
, MoveType t
) {
56 code
= ((o
& 7) << OF_SHIFT
) |
57 ((o
>> 4) << OR_SHIFT
) |
58 ((d
& 7) << DF_SHIFT
) |
59 ((d
>> 4) << DR_SHIFT
) |
63 File
get_orig_file() const {
64 return File((code
>> OF_SHIFT
) & OF_MASK
);
66 Rank
get_orig_rank() const {
67 return Rank((code
>> OR_SHIFT
) & OR_MASK
);
69 File
get_dest_file() const {
70 return File((code
>> DF_SHIFT
) & DF_MASK
);
72 Rank
get_dest_rank() const {
73 return Rank((code
>> DR_SHIFT
) & DR_MASK
);
75 Square
get_orig() const {
76 return Square(16 * get_orig_rank() + get_orig_file());
78 Square
get_dest() const {
79 return Square(16 * get_dest_rank() + get_dest_file());
81 MoveType
get_type() const {
82 return static_cast<MoveType
>((code
>> MT_SHIFT
) & MT_MASK
);
85 bool is_capture() const {
86 return is_set(2) && !is_null();
88 bool is_castle() const {
89 return !is_set(3) && !is_set(2) && is_set(1);
91 bool is_promotion() const {
94 bool is_double_pawn_push() const {
95 return get_type() == DOUBLE_PAWN_PUSH
;
97 bool is_en_passant() const {
98 return get_type() == EN_PASSANT
;
100 bool is_null() const {
101 return get_type() == NULL_MOVE
;
103 PieceType
get_promotion_type() const;
104 PieceType
get_castle_side() const;
107 // Static member function for sorting move in natural order
108 static bool numeric_comp(Move a, Move b) {
109 return (a.code < b.code);
113 bool operator==(const Move
& other
) const {
114 return this->code
== other
.code
;
116 bool operator!=(const Move
& other
) const {
117 return !(*this == other
);
119 std::string
to_string() const;
121 friend std::ostream
& operator<<(std::ostream
& out
, const Move move
);
122 friend class ExtendedMove
;
125 class ExtendedMove
: public Move
131 ExtendedMove() : score(0) { code
= NULL_MOVE
; }
132 ExtendedMove(Move m
, int s
= 0) : score(s
) { code
= m
.code
; }
134 char get_score() const { return score
; };
135 void set_score(int s
) { score
= s
; };
136 bool operator<(const ExtendedMove
& other
) const {
137 return this->score
> other
.score
;