Fix gtest dependency for Travis CI
[purplehaze.git] / src / move.h
blobe6cdab66b9affdc1333106a77eb0e87b0e9a634d
1 /* Copyright (C) 2007-2012 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/>.
17 #ifndef MOVE_H
18 #define MOVE_H
20 #include <cstdint>
21 #include <string>
23 #include "common.h"
25 class Move
27 private:
29 * A move is coded using 16 bits:
30 * 4 bits for the type
31 * 3 bits for the destination square rank
32 * 3 bits for the destination square file
33 * 3 bits for the origin square rank
34 * 3 bits for the origin square file
37 static const int MT_MASK = 0xF;
38 static const int DR_MASK = 0x7;
39 static const int DF_MASK = 0x7;
40 static const int OR_MASK = 0x7;
41 static const int OF_MASK = 0x7;
43 static const int MT_SHIFT = 0;
44 static const int DR_SHIFT = 4;
45 static const int DF_SHIFT = 7;
46 static const int OR_SHIFT = 10;
47 static const int OF_SHIFT = 13;
49 static uint16_t encode(Square o, Square d, MoveType t) {
50 uint16_t code = 0;
51 code |= (o & 7) << OF_SHIFT;
52 code |= (o >> 4) << OR_SHIFT;
53 code |= (d & 7) << DF_SHIFT;
54 code |= (d >> 4) << DR_SHIFT;
55 code |= static_cast<uint16_t>(t);
56 return code;
59 uint16_t code;
61 bool is_set(int i) const {
62 return code & ( 1 << i);
65 public:
66 Move() : code(NULL_MOVE) {}
68 Move(Square o, Square d, MoveType t) : code(encode(o, d, t)) {}
70 File orig_file() const {
71 return File((code >> OF_SHIFT) & OF_MASK);
73 Rank orig_rank() const {
74 return Rank((code >> OR_SHIFT) & OR_MASK);
76 File dest_file() const {
77 return File((code >> DF_SHIFT) & DF_MASK);
79 Rank dest_rank() const {
80 return Rank((code >> DR_SHIFT) & DR_MASK);
82 Square orig() const {
83 return Square(16 * orig_rank() + orig_file());
85 Square dest() const {
86 return Square(16 * dest_rank() + dest_file());
88 MoveType type() const {
89 return static_cast<MoveType>((code >> MT_SHIFT) & MT_MASK);
92 bool is_capture() const {
93 return is_set(2) && !is_null();
95 bool is_castle() const {
96 return !is_set(3) && !is_set(2) && is_set(1);
98 bool is_promotion() const {
99 return is_set(3);
101 bool is_double_pawn_push() const {
102 return type() == DOUBLE_PAWN_PUSH;
104 bool is_en_passant() const {
105 return type() == EN_PASSANT;
107 bool is_null() const {
108 return type() == NULL_MOVE;
110 PieceType promotion_type() const;
111 PieceType castle_side() const;
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);
124 class ExtendedMove : public Move
126 private:
127 int8_t val;
129 public:
130 ExtendedMove() : val(0) {}
131 ExtendedMove(Move m, int v = 0) : Move(m), val(v) {}
133 int8_t value() const {
134 return val;
136 bool operator<(const ExtendedMove& other) const {
137 return this->val > other.val;
141 #endif /* !MOVE_H */