All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
piece.h
Go to the documentation of this file.
1 #ifndef OSL_PIECE_H
2 #define OSL_PIECE_H
3 #include "osl/misc/loki.h"
4 #include "osl/player.h"
5 #include "osl/square.h"
6 #include "osl/ptype.h"
7 
8 #include <iosfwd>
9 namespace osl
10 {
11  class Piece;
12  inline bool operator==(Piece l, Piece r);
13  const int EMPTY_NUM=0x80;
14  const int EDGE_NUM=0x40;
22  class Piece
23  {
24  int piece;
25  Piece(int p) : piece(p)
26  {
27  }
28  public:
29  static const int SIZE=40;
30  static const Piece makeDirect(int value) { return Piece(value); }
31  int intValue() const { return piece; }
32  static const Piece EMPTY() { return Piece(BLACK,PTYPE_EMPTY,EMPTY_NUM,Square::STAND()); }
33  static const Piece EDGE() { return Piece(WHITE,PTYPE_EDGE,EDGE_NUM,Square::STAND()); }
34  static const int BitOffsetPtype=16;
35  static const int BitOffsetPromote=BitOffsetPtype+3;
37 
39  : piece((static_cast<int>(owner)<<20)
40  +(static_cast<int>(ptype)<<BitOffsetPtype)
41  +((num)<<8)+ square.uintValue())
42  {
43  }
45  {
46  }
50  static const Piece
51 #ifdef __GNUC__
52  __attribute__ ((pure))
53 #endif
55 
56  Ptype ptype() const {
57  return static_cast<Ptype>((piece>>BitOffsetPtype)&0xf);
58  }
59  PtypeO ptypeO() const {
60  return static_cast<PtypeO>(piece>>BitOffsetPtype);
61  }
62 
63  int number() const {
64  return ((piece&0xff00)>>8);
65  }
66 
67  const Square square() const {
68  return Square::makeDirect(piece&0xff);
69  }
70 
71  Piece& operator+=(Offset offset) {
72  piece += offset.intValue();
73  return *this;
74  }
75 
77  piece = (piece&0xffffff00)+square.uintValue();
78  }
79  private:
80  bool isOnBoardByOwner(Int2Type<BLACK>) const {
81  return static_cast<int>(static_cast<unsigned int>(piece)&0x800000ff)>0;
82  }
88  bool isOnBoardByOwner(Int2Type<WHITE>) const {
89  return static_cast<int>((-piece)&0x800000ff)>0;
90  }
91  public:
98  template<Player P>
99  bool isOnBoardByOwner() const { return isOnBoardByOwner(Int2Type<P>()); }
104  {
105  if(owner==BLACK)
106  return isOnBoardByOwner<BLACK>();
107  else
108  return isOnBoardByOwner<WHITE>();
109  }
110 
111  /* 成る. PROMOTE不可なpieceに適用不可 */
112  const Piece promote() const {
113  assert(canPromote(ptype()));
114  return Piece(piece-0x80000);
115  }
116 
117  /* 成りを戻す. PROMOTE不可なpieceに適用可 */
118  const Piece unpromote() const {
119  return Piece((int)piece|0x80000);
120  }
121 
126  const Piece captured() const {
127  // return (Piece)((((int)piece|0x80000)&0xffffff00)^0xfff00000);
128  // をoptimizeする
129  return Piece((piece&0xfff7ff00)^0xfff80000);
130  }
131 
132  const Piece promoteWithMask(int promote_mask) const {
133  assert(! (isPromoted() && promote_mask));
134  assert(promote_mask==0 || promote_mask==(1<<23));
135  return Piece(piece - (promote_mask>>(BitOffsetMovePromote-BitOffsetPromote)));
136  }
137 
138  const Piece checkPromote(bool promotep) const {
139  return Piece(piece - (promotep<<19));
140  }
141 
145  bool isPromoted() const { return (piece&(1<<19))==0; }
146 
151  bool isOnBoardNotPromoted() const{
152  int mask=piece&((1<<19)|0xff);
153  return mask>(1<<19);
154  }
155  bool isPromotedNotKingGold() const {
156  assert(ptype()!=KING && ptype()!=GOLD);
157  return isPromoted();
158  }
159 
160  bool isEmpty() const {
161  return (piece&0x8000)!=0;
162  }
163  static bool isEmptyNum(int num) {
164  return (num&0x80)!=0;
165  }
166  bool isEdge() const {
167  return (piece&0x4000)!=0;
168  }
169  static bool isEdgeNum(int num){
170  assert(!isEmptyNum(num));
171  return (num&0x40)!=0;
172  }
173  static bool isPieceNum(int num){
174  return (num&0xc0)==0;
175  }
176  template<Ptype T>
177  bool isPtype() const{
178  return (piece&0xf0000)==((T)<<BitOffsetPtype);
179  }
184  bool isPlayerPtype(Player pl,Ptype ptype) const{
185  assert(PTYPE_PIECE_MIN<=ptype && ptype<=PTYPE_MAX);
186  return (piece&0x1f0000)==(((ptype)<<BitOffsetPtype)|(pl&0x100000));
187  }
193  assert(PTYPE_PIECE_MIN<=ptype && ptype<=PTYPE_MAX);
194  assert(isBasic(ptype));
195  if(canPromote(ptype))
196  return (piece&0x170000)==(((osl::promote(ptype))<<BitOffsetPtype)|(pl&0x100000));
197  else
198  return isPlayerPtype(pl,ptype);
199  }
200  bool isPiece() const {
201  return (piece&0xc000)==0;
202  }
206  bool pieceIsBlack() const{
207  assert(isPiece());
208  return static_cast<int>(piece)>=0;
209  }
210  Player owner() const
211  {
212  assert(isPiece());
213  return static_cast<Player>(piece>>20);
214  }
215 
216  private:
223  bool canMoveOn(Int2Type<BLACK>) const {
224  return ((piece+0xe0000)&0x104000)==0;
225  }
226  bool canMoveOn(Int2Type<WHITE>) const {
227  return piece>=0;
228  }
229  public:
234  template<Player P>
235  bool canMoveOn() const { return canMoveOn(Int2Type<P>()); }
236 
237  bool canMoveOn(Player pl) const{
238  if(pl==BLACK)
239  return canMoveOn<BLACK>();
240  else
241  return canMoveOn<WHITE>();
242  }
243 
244  bool isOnBoard() const {
245  assert(square().isValid());
246  return ! square().isPieceStand();
247  }
248  };
249 
250  inline bool operator<(Piece l, Piece r)
251  {
252  return l.intValue() < r.intValue();
253  }
254  inline bool operator==(Piece l, Piece r)
255  {
256  return l.intValue() == r.intValue();
257  }
258  inline bool operator!=(Piece l, Piece r)
259  {
260  return ! (l == r);
261  }
262 
263  std::ostream& operator<<(std::ostream& os,const Piece piece);
264 }
265 
266 #endif /* OSL_PIECE_H */
267 // ;;; Local Variables:
268 // ;;; mode:c++
269 // ;;; c-basic-offset:2
270 // ;;; End: