Reorder README
[purplehaze.git] / src / piece.h
blob0a08962fde286b071f69053f78163d88eabdd3f4
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 PIECE_H
18 #define PIECE_H
20 #include <string>
22 #include "common.h"
24 class Piece
26 private:
28 * A piece is coded using 8 bits:
29 * 1 bit for the color
30 * 3 bits for the type
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;
42 unsigned char code;
44 public:
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);
53 Color color() const {
54 return static_cast<Color>((code >> C_SHIFT) & C_MASK);
56 PieceType type() const {
57 return static_cast<PieceType>((code >> T_SHIFT) & T_MASK);
59 int index() const {
60 return (code >> I_SHIFT) & I_MASK;
63 void set_index(int i) {
64 code &= I_MASK;
65 code |= (i << I_SHIFT);
68 bool is(const Color c) const {
69 return color() == c;
71 bool is(const PieceType t) const {
72 return type() == t;
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);
88 #endif /* !PIECE_H */