Type.h
Go to the documentation of this file.
1 // cwchessboard -- A C++ chessboard tool set
2 //
3 //! @file Type.h This file contains the declaration of class Type.
4 //
5 // Copyright (C) 2008, by
6 //
7 // Carlo Wood, Run on IRC <carlo@alinoe.com>
8 // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt
9 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 2 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 
24 #ifndef TYPE_H
25 #define TYPE_H
26 
27 #ifndef USE_PCH
28 #include <stdint.h>
29 #endif
30 
31 #include "Color.h"
32 
33 namespace cwchess {
34 
35 class Code;
36 
37 /** @brief The POD base type of class Type.
38 *
39  * This class uses the same internal type as Code to store the type bits.
40  * It even uses the same bits (the three least significant bits).
41  * All other bits are garanteed zero.
42 *
43  * If the third bit is set then the object represents a sliding piece (bishop, rook or queen).
44  * If in addition the first bit is set it can moves like a bishop (bishop and queen),
45  * or if the second bit is set it can move like a rook (rook and queen).
46 *
47  * @sa Type, nothing, pawn, knight, king, bishop, rook, queen
48 * /
49 struct TypeData {
50  uint8_t M_bits; //!< 00000STT, where STT is the type. If S == 1 then the piece is a slider.
51 };
52 
53 uint8_t const nothing_bits = 0; //!< The underlaying integral value of type 'nothing'.
54 uint8_t const pawn_bits = 1; //!< The underlaying integral value of type 'pawn'.
55 uint8_t const knight_bits = 2; //!< The underlaying integral value of type 'knight'.
56 uint8_t const king_bits = 3; //!< The underlaying integral value of type 'king'.
57 uint8_t const bishop_bits = 5; //!< The underlaying integral value of type 'bishop'.
58 uint8_t const rook_bits = 6; //!< The underlaying integral value of type 'rook'.
59 uint8_t const queen_bits = 7; //!< The underlaying integral value of type 'queen'.
60 uint8_t const type_mask = 7; //!< A mask for the bits used for the type of a piece.
61 
62 //! A constant representing the absence of a piece.
63 TypeData const nothing = { nothing_bits };
64 //! A constant representing a pawn.
65 TypeData const pawn = { pawn_bits };
66 //! A constant representing a knight.
67 TypeData const knight = { knight_bits };
68 //! A constant representing a king.
69 TypeData const king = { king_bits };
70 //! A constant representing a bishop.
71 TypeData const bishop = { bishop_bits };
72 //! A constant representing a rook.
73 TypeData const rook = { rook_bits };
74 //! A constant representing a queen.
75 TypeData const queen = { queen_bits };
76 
77 // Compare constants (this should never be needed, but why not add it).
78 inline bool operator==(TypeData t1, TypeData t2) { return t1.M_bits == t2.M_bits; }
79 inline bool operator!=(TypeData t1, TypeData t2) { return t1.M_bits != t2.M_bits; }
80 
81 /** @brief A chess piece type.
82 *
83  * This class represents a chess piece type.
84 *
85  * The class Code is a friend of this class.
86 * /
87 class Type : protected TypeData {
88  public:
89  /** @name Constructors* /
90  //@{
91 
92  //! Construct an uninitialized Type.
93  Type(void) { }
94 
95  //! Copy-constructor.
96  Type(Type const& type) { M_bits = type.M_bits; }
97 
98  //! Construct a Type from a constant.
99  Type(TypeData type) { M_bits = type.M_bits; }
100 
101  //@}
102 
103  /** @name Assignment operators* /
104  //@{
105 
106  //! Assign from another Type.
107  Type& operator=(Type const& type) { M_bits = type.M_bits; return* this; }
108 
109  //! Assign from a constant.
110  Type& operator=(TypeData type) { M_bits = type.M_bits; return* this; }
111 
112  //@}
113 
114  /** @name Comparison operators* /
115  //@{
116 
117  friend bool operator==(Type const& t1, Type const& t2) { return t1.M_bits == t2.M_bits; }
118  friend bool operator==(Type const& t1, TypeData t2) { return t1.M_bits == t2.M_bits; }
119  friend bool operator==(TypeData t1, Type const& t2) { return t1.M_bits == t2.M_bits; }
120  friend bool operator!=(Type const& t1, Type const& t2) { return t1.M_bits != t2.M_bits; }
121  friend bool operator!=(Type const& t1, TypeData t2) { return t1.M_bits != t2.M_bits; }
122  friend bool operator!=(TypeData t1, Type const& t2) { return t1.M_bits != t2.M_bits; }
123 
124  //@}
125 
126  /** @name Accessors* /
127  //@{
128 
129  //! Returns TRUE if the type is a bishop, rook or queen.
130  bool is_a_slider(void) const { return M_bits > 4; }
131 
132  //! Returns TRUE if the type is a rook or queen.
133  bool is_a_rookmover(void) const { return (M_bits & rook_bits) == rook_bits; }
134 
135  //! Returns TRUE if the type is a bishop or queen.
136  bool is_a_bishopmover(void) const { return (M_bits & bishop_bits) == bishop_bits; }
137 
138  //! Return the underlaying integral value.
139  uint8_t operator()(void) const { return M_bits; }
140 
141  //@}
142 
143  private:
144  friend class Code;
145  // Constructor for class Code.
146  explicit Type(uint8_t bits) { M_bits = bits; }
147 };
148 
149 } // namespace cwchess
150 
151 #endif // TYPE_H
A namespace for all chess related objects that are not related to the GUI.
Definition: Array.h:39
TypeData const bishop
A constant representing a bishop.
Definition: Type.h:71
TypeData const queen
A constant representing a queen.
Definition: Type.h:75
TypeData const rook
A constant representing a rook.
Definition: Type.h:73
TypeData const pawn
A constant representing a pawn.
Definition: Type.h:65
TypeData const king
A constant representing a king.
Definition: Type.h:69
uint8_t const bishop_bits
The underlaying integral value of type& #39;bishop&#39;.
Definition: Type.h:57
A chess piece type.
Definition: Type.h:87
uint8_t M_bits
00000STT, where STT is the type. If S == 1 then the piece is a slider.
Definition: Type.h:50
uint8_t operator()(void) const
Return the underlaying integral value.
Definition: Type.h:139
Type & operator=(TypeData type)
Assign from a constant.
Definition: Type.h:110
Type(Type const& type)
Copy-constructor.
Definition: Type.h:96
bool is_a_slider(void) const
Returns TRUE if the type is a bishop, rook or queen.
Definition: Type.h:130
uint8_t const king_bits
The underlaying integral value of type& #39;king&#39;.
Definition: Type.h:56
TypeData const knight
A constant representing a knight.
Definition: Type.h:67
bool is_a_bishopmover(void) const
Returns TRUE if the type is a bishop or queen.
Definition: Type.h:136
Type & operator=(Type const& type)
Assign from another Type.
Definition: Type.h:107
A chess piece type including color.
Definition: Code.h:92
This file contains the declaration of class Color.
TypeData const nothing
A constant representing the absence of a piece.
Definition: Type.h:63
Type(void)
Construct an uninitialized Type.
Definition: Type.h:93
uint8_t const type_mask
A mask for the bits used for the type of a piece.
Definition: Type.h:60
uint8_t const knight_bits
The underlaying integral value of type& #39;knight&#39;.
Definition: Type.h:55
Type(TypeData type)
Construct a Type from a constant.
Definition: Type.h:99
The POD base type of class Type.
Definition: Type.h:49
uint8_t const queen_bits
The underlaying integral value of type& #39;queen&#39;.
Definition: Type.h:59
bool is_a_rookmover(void) const
Returns TRUE if the type is a rook or queen.
Definition: Type.h:133
uint8_t const nothing_bits
The underlaying integral value of type& #39;nothing&#39;.
Definition: Type.h:53
uint8_t const pawn_bits
The underlaying integral value of type& #39;pawn&#39;.
Definition: Type.h:54
uint8_t const rook_bits
The underlaying integral value of type& #39;rook&#39;.
Definition: Type.h:58

Copyright © 2006 - 2010 Carlo Wood.  All rights reserved.