square.h Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
square.h
Go to the documentation of this file.
1 /* square.h
2 */
3 #ifndef OSL_SQUARE_H
4 #define OSL_SQUARE_H
5
6 #include "osl/misc/loki.h"
7 #include "osl/offset.h"
8 #include "osl/directionTraits.h"
9 #include <boost/utility/enable_if.hpp>
10
11 namespace osl
12 {
38 class Square;
39 bool operator==(Square l, Square r);
40
41 class Square
42 {
43 unsigned int square;
44 explicit Square(int p) : square(p)
45 {
46 }
47 public:
48 static const Square makeDirect(int value) { return Square(value); }
49 unsigned int uintValue() const { return square; }
50 enum {
51 PIECE_STAND=0,
52 MIN=0,
53 SIZE=0x100
54 };
55 Square() : square(PIECE_STAND)
56 {
57 }
58 static const Square STAND() { return Square(PIECE_STAND); }
59 Square(int x, int y) : square((x*Offset::BOARD_HEIGHT)+y+1)
60 {
61 assert(square < SIZE);
62 }
66 static const Square makeNoCheck(int x, int y) {
67 return Square((x*Offset::BOARD_HEIGHT)+y+1);
68 }
69 static const Square nth(unsigned int i) { return Square(i+MIN); }
73 int x() const { return square >> 4; }
77 int y() const { return (square&0xf)-1; }
81 int y1() const { return square&0xf; }
82 unsigned int index() const { return square - MIN; }
83 static unsigned int indexMax() { return SIZE - MIN; }
84 int indexForOffset32() const { return square + (square&0xf0); }
85
86 bool isPieceStand() const { return square == PIECE_STAND; }
87 bool isOnBoardSlow() const;
93 bool isOnBoard() const {
94 return (0xffffff88&(square-0x12)&
95 ((unsigned int)((square&0x77)^0x12)+0xffffff77))==0;
96 }
101 bool isEdge() const {
102 assert(!isPieceStand() && 0<=x() && x()<=10 && 0<=y() && y()<=10);
103 return (0x88&(square-0x12)&((square&0x11)+0xf7))!=0;
104 }
105 bool isValid() const;
106
107
108 const Square squareForBlackSlow(Player player) const;
109
110 const Square squareForBlack(Int2Type<BLACK>) const{
111 assert(isOnBoard());
112 Square ret=*this;
113 return ret;
114 }
115
116 const Square squareForBlack(Int2Type<WHITE>) const{
117 assert(isOnBoard());
118 Square ret=makeDirect(Square(9,9).uintValue()+Square(1,1).uintValue()-uintValue());
119 return ret;
120 }
125 template<Player P>
126 const Square squareForBlack() const{
127 return squareForBlack(Int2Type<P>());
128 }
129 const Square squareForBlack(Player player) const{
130 if(player==BLACK)
131 return squareForBlack<BLACK>();
132 else
133 return squareForBlack<WHITE>();
134 }
135
136 const Square rotate180() const
137 {
138 return squareForBlack<WHITE>();
139 }
140 const Square rotate180EdgeOK() const
141 {
142 Square ret=makeDirect(Square(9,9).uintValue()+Square(1,1).uintValue()-uintValue());
143 return ret;
144 }
145 const Square rotate180Safe() const
146 {
147 if (isPieceStand())
148 return *this;
149 return squareForBlack<WHITE>();
150 }
151 const Square flipHorizontal() const
152 {
153 if (isPieceStand())
154 return *this;
155 return Square(10-x(), y());
156 }
157
158 static const Square onBoardMax(){ return Square(9,9); }
159 static const Square onBoardMin(){ return Square(1,1); }
160
164 bool isOnBoardRegion() const {
165 return static_cast<unsigned int>(index()-onBoardMin().index())
166 <= static_cast<unsigned int>(onBoardMax().index()-onBoardMin().index());
167 }
168
169 Square& operator++() {
170 square += 1;
171 return *this;
172 }
173
174 static int reverseX(int x) { return 10-x; }
175 static int reverseY(int y) { return 10-y; }
176 private:
177 template<Player P>
178 static bool canPromoteY(int y,Int2Type<P>);
179 static bool canPromoteY(int y,Int2Type<BLACK>)
180 {
181 return y <= 3;
182 }
183 static bool canPromoteY(int y,Int2Type<WHITE>)
184 {
185 return y >= 7;
186 }
187 bool canPromote(Int2Type<BLACK>) const{
188 return (uintValue()&0xf)<=4;
189 }
190 bool canPromote(Int2Type<WHITE>) const{
191 return (uintValue()&0x8)!=0;
192 }
193 public:
194 template <Player P>
195 static bool canPromoteY(int y) { return canPromoteY(y,Int2Type<P>()); }
196 template <Player P>
197 bool canPromote() const{
198 return canPromote(Int2Type<P>());
199 }
200 bool canPromote(Player player) const
201 {
202 if (player==BLACK)
203 return canPromote<BLACK>();
204 else
205 return canPromote<WHITE>();
206 }
211 bool isULRD(Square sq) const{
212 assert(isOnBoard() && sq.isOnBoard());
213 unsigned int v=uintValue() ^ sq.uintValue();
214 return (((v+0xefull)^v)&0x110ull)!=0x110ull;
215 }
219 bool isUD(Square sq) const{
220 assert(isOnBoard() && sq.isOnBoard());
221 unsigned int v=uintValue() ^ sq.uintValue();
222 return (v&0xf0)==0;
223 }
227 template<Player P>
228 bool isU(Square sq) const{
229 assert(isOnBoard() && sq.isOnBoard());
230 unsigned int v=uintValue() ^ sq.uintValue();
231 if(P==BLACK)
232 return ((v|(uintValue()-sq.uintValue()))&0xf0)==0;
233 else
234 return ((v|(sq.uintValue()-uintValue()))&0xf0)==0;
235 }
239 bool isLR(Square sq) const{
240 assert(isOnBoard() && sq.isOnBoard());
241 unsigned int v=uintValue() ^ sq.uintValue();
242 return (v&0xf)==0;
243 }
244 Square& operator+=(Offset offset) {
245 square += offset.intValue();
246 return *this;
247 }
248 Square& operator-=(Offset offset) {
249 square -= offset.intValue();
250 return *this;
251 }
252 const Square operator+(Offset offset) const {
253 Square result(*this);
254 return result+=offset;
255 }
256 const Square operator-(Offset offset) const {
257 Square result(*this);
258 return result-=offset;
259 }
260 const Offset operator-(Square other) const {
261 return Offset::makeDirect(square - other.square);
262 }
263 template<int Y>
264 bool yEq() {
265 return (uintValue()&0xf)==(Y+1);
266 }
267 template<int Y>
268 bool yLe(typename boost::enable_if_c<Y != 2>::type * =0) {
269 return (uintValue()&0xf)<=(Y+1);
270 }
271 template<int Y>
272 bool yLe(typename boost::enable_if_c<Y == 2>::type * =0) {
273 return (uintValue()&0xc)==0;
274 }
275 template<int Y>
276 bool yGe(typename boost::enable_if_c<Y!=7>::type * =0) {
277 return (uintValue()&0xf)>=(Y+1);
278 }
279 template<int Y>
280 bool yGe(typename boost::enable_if_c<Y==7>::type * =0) {
281 return (uintValue()&0x8)!=0;
282 }
283 template <Player P, Direction D>
284 const Square neighbor() const {
285 return *this + DirectionPlayerTraits<D,P>::offset();
286 }
287 template <Player P, Direction D>
288 const Square back() const {
289 return neighbor<PlayerTraits<P>::opponent,D>();
290 }
291 const Square neighbor(Player P, Direction D) const;
292 const Square back(Player P, Direction D) const;
293 };
294
295 inline bool operator==(Square l, Square r)
296 {
297 return l.uintValue() == r.uintValue();
298 }
299 inline bool operator!=(Square l, Square r)
300 {
301 return ! (l == r);
302 }
303 inline bool operator<(Square l, Square r)
304 {
305 return l.uintValue() < r.uintValue();
306 }
307 inline bool operator>(Square l, Square r)
308 {
309 return l.uintValue() > r.uintValue();
310 }
311 std::ostream& operator<<(std::ostream&, Square);
312
313 } // namespace osl
314
315 #endif /* OSL_POSITION_H */
316 // ;;; Local Variables:
317 // ;;; mode:c++
318 // ;;; c-basic-offset:2
319 // ;;; End: