Add Game::is_dangerous() for Futility Pruning
[purplehaze.git] / src / piece.h
blob8384770bb42cac2f4e198b6ea8dd59b2e83b3957
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/>.
17 #ifndef PIECE_H
18 #define PIECE_H
20 #include <bitset>
21 #include <string>
23 #include "common.h"
25 class Piece
27 private:
29 * A piece is coded using 8 bits:
30 * 1 bit for the color
31 * 3 bits for the type
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;
43 unsigned char code;
45 public:
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) {
65 code &= I_MASK;
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);
80 #endif /* !PIECE_H */