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/>.
29 * A piece is coded using 8 bits:
32 * 4 bits for the index in pieces[c][t][i]
34 * FIXME? No warning given if index is > 4 bits
36 static const int C_MASK
= 0x1;
37 static const int T_MASK
= 0x7;
38 static const int I_MASK
= 0xF;
39 static const int C_SHIFT
= 0;
40 static const int T_SHIFT
= 1;
41 static const int I_SHIFT
= 4;
46 Piece() : code(EMPTY
) {}
48 Piece(Color c
, PieceType t
, int i
= 0) {
49 code
= (i
<< I_SHIFT
) |
50 (static_cast<int>(t
) << T_SHIFT
) |
51 (static_cast<int>(c
) << C_SHIFT
);
54 Color
get_color() const {
55 return static_cast<Color
>((code
>> C_SHIFT
) & C_MASK
);
57 PieceType
get_type() const {
58 return static_cast<PieceType
>((code
>> T_SHIFT
) & T_MASK
);
60 int get_index() const {
61 return (code
>> I_SHIFT
) & I_MASK
;
64 void set_index(int i) {
66 code |= (i << I_SHIFT);
69 bool operator==(const Piece
& other
) const {
70 return this->code
== other
.code
;
72 bool operator!=(const Piece
& other
) const {
73 return !(*this == other
);
75 std::string
to_string() const;
77 friend std::ostream
& operator<<(std::ostream
& out
, const Piece piece
);