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