Color.h
Go to the documentation of this file.
1 // cwchessboard -- A C++ chessboard tool set
2 //
3 //! @file Color.h This file contains the declaration of class Color.
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 COLOR_H
25 #define COLOR_H
26 
27 #ifndef USE_PCH
28 #include <stdint.h>
29 #endif
30 
31 namespace cwchess {
32 
33 class Code;
34 
35 /** @brief The POD base type of class Color.
36 *
37  * This class uses the same internal type as Code to store the color bit.
38  * It even uses the same bit (the fourth bit). All other bits are garanteed zero.
39 *
40  * If the bit is set then the object represents the color white.
41 *
42  * @sa Color, black, white
43 * /
44 struct ColorData {
45  uint8_t M_bits; //!< 0000C000, where C=0 means black and C=1 means white.
46 };
47 
48 uint8_t const black_bits = 0; //!< The underlaying integral value of color 'black'.
49 uint8_t const white_bits = 8; //!< The underlaying integral value of color 'white'.
50 uint8_t const color_mask = 8; //!< A mask for the bits used for the color of a piece.
51 
52 //! A constant representing the color black.
53 ColorData const black = { black_bits };
54 //! A constant representing the color white.
55 ColorData const white = { white_bits };
56 
57 // Compare constants (this should never be needed, but why not add it).
58 inline bool operator==(ColorData c1, ColorData c2) { return c1.M_bits == c2.M_bits; }
59 inline bool operator!=(ColorData c1, ColorData c2) { return c1.M_bits != c2.M_bits; }
60 
61 /** @brief A color (black or white).
62 *
63  * This class represents a chess color.
64 *
65  * See ColorData for defined constants.
66 * /
67 class Color : protected ColorData {
68  public:
69 
70  /** @name Constructors* /
71  //@{
72 
73  //! Construct an uninitialized Color object.
74  Color(void) { }
75 
76  //! Copy-constructor.
77  Color(Color const& color) { M_bits = color.M_bits; }
78 
79  //! Construct a Color object from a constant.
80  Color(ColorData color) { M_bits = color.M_bits; }
81 
82  //@}
83 
84  /** @name Assignment operators* /
85  //@{
86 
87  //! Assign from another Color object.
88  Color& operator=(Color const& color) { M_bits = color.M_bits; return* this; }
89 
90  //! Assign from a constant.
91  Color& operator=(ColorData color) { M_bits = color.M_bits; return* this; }
92 
93  //@}
94 
95  /** @name Comparison operators* /
96  //@{
97 
98  friend bool operator==(Color const& c1, Color const& c2) { return c1.M_bits == c2.M_bits; }
99  friend bool operator==(Color const& c1, ColorData c2) { return c1.M_bits == c2.M_bits; }
100  friend bool operator==(ColorData c1, Color const& c2) { return c1.M_bits == c2.M_bits; }
101  friend bool operator!=(Color const& c1, Color const& c2) { return c1.M_bits != c2.M_bits; }
102  friend bool operator!=(Color const& c1, ColorData c2) { return c1.M_bits != c2.M_bits; }
103  friend bool operator!=(ColorData c1, Color const& c2) { return c1.M_bits != c2.M_bits; }
104 
105  //@}
106 
107  /** @name Accessors* /
108  //@{
109 
110  //! Return TRUE if this color is white.
111  bool is_white(void) const { return M_bits; }
112 
113  //! Return TRUE if this color is black.
114  bool is_black(void) const { return !M_bits; }
115 
116  //! Return the underlaying integral value.
117  uint8_t operator()(void) const { return M_bits; }
118 
119  //@}
120 
121  /** @name Special functions* /
122  //@{
123 
124  //! Change the color from black to white or vica versa.
125  void toggle(void) { M_bits ^= color_mask; }
126 
127  //@}
128 
129  /** @name Visitors* /
130  //@{
131 
132  //! Return a Color object with the opposite color of this object.
133  Color opposite(void) const { ColorData data; data.M_bits = M_bits ^ color_mask; return Color(data); }
134 
135  //! Return a number that can be used as array index.
136  uint8_t index(void) const { return M_bits >> 3; }
137 
138  //! Return the index offset that advances one square in the direction of the pawns of this color.
139  uint8_t forward_index_offset(void) const { return (M_bits << 1) - 8; }
140 
141  //@}
142 
143 #ifndef DOXYGEN
144  private:
145  friend class Code;
146  // Constructor for class Code.
147  explicit Color(uint8_t bits) { M_bits = bits; }
148 #endif
149 };
150 
151 } // namespace cwchess
152 
153 #endif // COLOR_H
A namespace for all chess related objects that are not related to the GUI.
Definition: Array.h:39
Color opposite(void) const
Return a Color object with the opposite color of this object.
Definition: Color.h:133
ColorData const white
A constant representing the color white.
Definition: Color.h:55
void toggle(void)
Change the color from black to white or vica versa.
Definition: Color.h:125
Color & operator=(Color const& color)
Assign from another Color object.
Definition: Color.h:88
Color(void)
Construct an uninitialized Color object.
Definition: Color.h:74
uint8_t index(void) const
Return a number that can be used as array index.
Definition: Color.h:136
BitBoardData const c1
The square c1.
Definition: BitBoard.h:98
Color(ColorData color)
Construct a Color object from a constant.
Definition: Color.h:80
uint8_t operator()(void) const
Return the underlaying integral value.
Definition: Color.h:117
The POD base type of class Color.
Definition: Color.h:44
uint8_t const white_bits
The underlaying integral value of color& #39;white&#39;.
Definition: Color.h:49
bool is_white(void) const
Return TRUE if this color is white.
Definition: Color.h:111
Color & operator=(ColorData color)
Assign from a constant.
Definition: Color.h:91
A chess piece type including color.
Definition: Code.h:92
uint8_t const black_bits
The underlaying integral value of color& #39;black&#39;.
Definition: Color.h:48
uint8_t forward_index_offset(void) const
Return the index offset that advances one square in the direction of the pawns of this color...
Definition: Color.h:139
uint8_t const color_mask
A mask for the bits used for the color of a piece.
Definition: Color.h:50
A color (black or white).
Definition: Color.h:67
uint8_t M_bits
0000C000, where C=0 means black and C=1 means white.
Definition: Color.h:45
Color(Color const& color)
Copy-constructor.
Definition: Color.h:77
bool is_black(void) const
Return TRUE if this color is black.
Definition: Color.h:114
BitBoardData const c2
The square c2.
Definition: BitBoard.h:106
ColorData const black
A constant representing the color black.
Definition: Color.h:53

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