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/>.
28 * A piece is coded using 8 bits:
31 * 4 bits for the index in pieces[c][t][i]
33 * FIXME? No warning given if index is > 4 bits
35 static const int C_MASK
= 0x1;
36 static const int T_MASK
= 0x7;
37 static const int I_MASK
= 0xF;
38 static const int C_SHIFT
= 0;
39 static const int T_SHIFT
= 1;
40 static const int I_SHIFT
= 4;
45 Piece() : code(EMPTY
) {}
47 Piece(Color c
, PieceType t
, int i
= 0) {
48 code
= (i
<< I_SHIFT
) |
49 (static_cast<int>(t
) << T_SHIFT
) |
50 (static_cast<int>(c
) << C_SHIFT
);
54 return static_cast<Color
>((code
>> C_SHIFT
) & C_MASK
);
56 PieceType
type() const {
57 return static_cast<PieceType
>((code
>> T_SHIFT
) & T_MASK
);
60 return (code
>> I_SHIFT
) & I_MASK
;
63 void set_index(int i) {
65 code |= (i << I_SHIFT);
68 bool is(const Color c
) const {
71 bool is(const PieceType t
) const {
74 bool is(const Color c
, const PieceType t
) const {
75 return is(c
) && is(t
);
77 bool operator==(const Piece
& other
) const {
78 return this->code
== other
.code
;
80 bool operator!=(const Piece
& other
) const {
81 return !(*this == other
);
83 std::string
to_string() const;
85 friend std::ostream
& operator<<(std::ostream
& out
, const Piece piece
);