Cute Chess  0.1
piece.h
1 /*
2  This file is part of Cute Chess.
3  Copyright (C) 2008-2018 Cute Chess authors
4 
5  Cute Chess is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  Cute Chess is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Cute Chess. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef PIECE_H
20 #define PIECE_H
21 
22 #include <QtGlobal>
23 #include "side.h"
24 
25 namespace Chess {
26 
40 class Piece
41 {
42  public:
44  static const int NoPiece = 0;
46  static const int WallPiece = 100;
47 
49  Piece(int type = NoPiece);
51  Piece(Side side, int type);
52 
54  bool operator==(const Piece& other) const;
56  bool operator!=(const Piece& other) const;
58  bool operator<(const Piece& other) const;
60  bool operator>(const Piece& other) const;
61 
63  bool isEmpty() const;
65  bool isValid() const;
67  bool isWall() const;
68 
70  Side side() const;
72  int type() const;
73 
75  void setSide(Side side);
77  void setType(int type);
78 
79  private:
80  quint16 m_data;
81 };
82 
83 
84 inline Piece::Piece(int type)
85  : m_data(type | (Side::NoSide << 10))
86 {
87 }
88 
89 inline Piece::Piece(Side side, int type)
90  : m_data(type | (side << 10))
91 {
92  Q_ASSERT(!side.isNull());
93  Q_ASSERT(type != WallPiece);
94  Q_ASSERT(type != NoPiece);
95 }
96 
97 inline bool Piece::operator==(const Piece& other) const
98 {
99  return m_data == other.m_data;
100 }
101 
102 inline bool Piece::operator!=(const Piece& other) const
103 {
104  return m_data != other.m_data;
105 }
106 
107 inline bool Piece::operator<(const Piece& other) const
108 {
109  return m_data < other.m_data;
110 }
111 
112 inline bool Piece::operator>(const Piece& other) const
113 {
114  return m_data > other.m_data;
115 }
116 
117 inline bool Piece::isEmpty() const
118 {
119  return type() == NoPiece;
120 }
121 
122 inline bool Piece::isValid() const
123 {
124  return !side().isNull();
125 }
126 
127 inline bool Piece::isWall() const
128 {
129  return type() == WallPiece;
130 }
131 
132 inline Side Piece::side() const
133 {
134  return Side(Side::Type(m_data >> 10));
135 }
136 
137 inline int Piece::type() const
138 {
139  return m_data & 0x3FF;
140 }
141 
143 {
144  m_data = type() | (side << 10);
145 }
146 
147 inline void Piece::setType(int type)
148 {
149  m_data = type | (m_data & 0xC00);
150 }
151 
152 } // namespace Chess
153 #endif // PIECE_H
static const int NoPiece
Definition: piece.h:44
bool operator==(const Piece &other) const
Definition: piece.h:97
bool isValid() const
Definition: piece.h:122
Piece(int type=NoPiece)
Definition: piece.h:84
bool isEmpty() const
Definition: piece.h:117
bool operator!=(const Piece &other) const
Definition: piece.h:102
bool operator<(const Piece &other) const
Definition: piece.h:107
bool isWall() const
Definition: piece.h:127
bool operator>(const Piece &other) const
Definition: piece.h:112
static const int WallPiece
Definition: piece.h:46
void setType(int type)
Definition: piece.h:147
Definition: boardscene.h:29
void setSide(Side side)
Definition: piece.h:142
bool isNull() const
Definition: side.h:89
The side or color of a chess player.
Definition: side.h:35
Side side() const
Definition: piece.h:132
int type() const
Definition: piece.h:137
A chess piece.
Definition: piece.h:40
Type
Definition: side.h:41