All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ptype.h
Go to the documentation of this file.
1 #ifndef OSL_PTYPE_H
2 #define OSL_PTYPE_H
3 
4 #include "osl/config.h"
5 #include "osl/player.h"
6 #include <cassert>
7 #include <iosfwd>
8 
9 namespace osl
10 {
11 
13  enum Ptype
14  {
17  PPAWN=2,
18  PLANCE=3,
22  PROOK=7,
23  KING=8,
24  GOLD=9,
25  PAWN=10,
26  LANCE=11,
27  KNIGHT=12,
28  SILVER=13,
29  BISHOP=14,
30  ROOK=15,
31 
36  };
38 
39  std::istream& operator>>(std::istream& is, Ptype& ptype);
40  std::ostream& operator<<(std::ostream& os,const Ptype ptype);
41 
45  bool isValid(Ptype ptype);
46 
50  inline bool isPiece(Ptype ptype)
51  {
52  assert(isValid(ptype));
53  return static_cast<int>(ptype)>=PTYPE_PIECE_MIN;
54  }
58  inline bool isBasic(Ptype ptype)
59  {
60  assert(isValid(ptype));
61  return static_cast<int>(ptype)>PROOK;
62  }
63 
67  inline bool isPromoted(Ptype ptype)
68  {
69  assert(isPiece(ptype));
70  return static_cast<int>(ptype)<KING;
71  }
72 
77  inline bool canPromote(Ptype ptype)
78  {
79  assert(isPiece(ptype));
80  return static_cast<int>(ptype)>GOLD;
81  }
82 
87  inline Ptype unpromote(Ptype ptype)
88  {
89  assert(isPiece(ptype));
90  Ptype ret=static_cast<Ptype>(static_cast<int>(ptype)|8);
91  assert(isPiece(ret));
92  return ret;
93  }
94  inline Ptype unpromoteSafe(Ptype ptype)
95  {
96  if (! isPiece(ptype))
97  return ptype;
98  return unpromote(ptype);
99  }
100 
105  inline Ptype promote(Ptype ptype)
106  {
107  assert(canPromote(ptype));
108  Ptype ret=static_cast<Ptype>(static_cast<int>(ptype)-8);
109  assert(isPiece(ret));
110  return ret;
111  }
112 
113  inline bool isMajorBasic(Ptype ptype)
114  {
115  return ptype >= 14;
116  }
117  inline bool isMajor(Ptype ptype)
118  {
119  assert(isPiece(ptype));
120  return isMajorBasic(unpromote(ptype));
121  }
122  inline bool isMajorNonPieceOK(Ptype ptype)
123  {
124  return (static_cast<int>(ptype)|8)>=14;
125  }
126 
131  enum PtypeO {
134  };
135 
136 #define NEW_PTYPEO(player,ptype) static_cast<PtypeO>(static_cast<int>(ptype)-(16&static_cast<int>(player)))
137  inline unsigned int ptypeOIndex(PtypeO ptypeo)
138  {
139  const int result = ptypeo - PTYPEO_MIN;
140  assert(result >= 0);
141  return result;
142  }
143  inline PtypeO newPtypeO(Player player,Ptype ptype)
144  {
145  return static_cast<PtypeO>(static_cast<int>(ptype)-(16&static_cast<int>(player)));
146  }
147 
148 
149  inline Ptype getPtype(PtypeO ptypeO)
150  {
151  return static_cast<Ptype>(static_cast<int>(ptypeO)& 15);
152  }
153 
155  inline PtypeO promote(PtypeO ptypeO)
156  {
157  assert(canPromote(getPtype(ptypeO)));
158  PtypeO ret=static_cast<PtypeO>(static_cast<int>(ptypeO)-8);
159  assert(isPiece(getPtype(ret)));
160  return ret;
161  }
162 
164  inline PtypeO promoteWithMask(PtypeO ptypeO,int promoteMask)
165  {
166  assert(promoteMask==0 || promoteMask==0x800000);
167  PtypeO ret=static_cast<PtypeO>(static_cast<int>(ptypeO)-(promoteMask>>20));
168  return ret;
169  }
170 
172  inline PtypeO unpromote(PtypeO ptypeO)
173  {
174  return static_cast<PtypeO>(static_cast<int>(ptypeO)|8);
175  }
176 
177  bool isValidPtypeO(int ptypeO);
178 
182  inline bool isPiece(PtypeO ptypeO)
183  {
184  assert(isValidPtypeO(ptypeO));
185  return isPiece(getPtype(ptypeO));
186  }
187 
188  inline Player getOwner(PtypeO ptypeO)
189  {
190  assert(isPiece(ptypeO));
191  return static_cast<Player>(static_cast<int>(ptypeO)>>31);
192  }
193 
194 
196  inline PtypeO captured(PtypeO ptypeO)
197  {
198  assert(isPiece(ptypeO));
199  return static_cast<PtypeO>((static_cast<int>(ptypeO)|8)^(~15));
200  }
201 
203  inline PtypeO alt(PtypeO ptypeO)
204  {
205  assert(isPiece(ptypeO));
206  return static_cast<PtypeO>(static_cast<int>(ptypeO)^(~15));
207  }
208 
213  inline PtypeO altIfPiece(PtypeO ptypeO)
214  {
215  int v=static_cast<int>(ptypeO);
216  return static_cast<PtypeO>(v^((1-(v&15))&~15));
217  }
218 
219  inline bool canPromote(PtypeO ptypeO)
220  {
221  return canPromote(getPtype(ptypeO));
222  }
223 
224 
228  inline bool isPromoted(PtypeO ptypeO)
229  {
230  assert(isValidPtypeO(ptypeO));
231  return isPromoted(getPtype(ptypeO));
232  }
233 
234 
237 
238  std::ostream& operator<<(std::ostream& os,const PtypeO ptypeO);
239 
241 
242 } // namespace osl
243 
244 #endif /* OSL_PTYPE_H */
245 // ;;; Local Variables:
246 // ;;; mode:c++
247 // ;;; c-basic-offset:2
248 // ;;; End: