ptype.h
Go to the documentation of this file.
00001 #ifndef OSL_PTYPE_H
00002 #define OSL_PTYPE_H
00003 
00004 #include "osl/config.h"
00005 #include "osl/player.h"
00006 #include <cassert>
00007 #include <iosfwd>
00008 
00009 namespace osl
00010 {
00011 
00013   enum Ptype
00014   {
00015     PTYPE_EMPTY=0,
00016     PTYPE_EDGE=1,
00017     PPAWN=2,
00018     PLANCE=3,
00019     PKNIGHT=4,
00020     PSILVER=5,
00021     PBISHOP=6,
00022     PROOK=7,
00023     KING=8,
00024     GOLD=9,
00025     PAWN=10,
00026     LANCE=11,
00027     KNIGHT=12,
00028     SILVER=13,
00029     BISHOP=14,
00030     ROOK=15,
00031 
00032     PTYPE_MIN=0,
00033     PTYPE_BASIC_MIN=KING,
00034     PTYPE_PIECE_MIN=2,
00035     PTYPE_MAX=15,
00036   };
00037   const int PTYPE_SIZE=PTYPE_MAX-PTYPE_MIN+1;
00038   
00039   std::istream& operator>>(std::istream& is, Ptype& ptype);
00040   std::ostream& operator<<(std::ostream& os,const Ptype ptype);
00041   
00045   bool isValid(Ptype ptype);
00046 
00050   inline bool isPiece(Ptype ptype)
00051   {
00052     assert(isValid(ptype));
00053     return static_cast<int>(ptype)>=PTYPE_PIECE_MIN;
00054   }
00058   inline bool isBasic(Ptype ptype)
00059   {
00060     assert(isValid(ptype));
00061     return static_cast<int>(ptype)>PROOK;
00062   }
00063   
00067   inline bool isPromoted(Ptype ptype)
00068   {
00069     assert(isPiece(ptype));
00070     return static_cast<int>(ptype)<KING; 
00071   }
00072 
00077   inline bool canPromote(Ptype ptype)
00078   {
00079     assert(isPiece(ptype));
00080     return static_cast<int>(ptype)>GOLD; 
00081   }
00082   
00087   inline Ptype unpromote(Ptype ptype)
00088   {
00089     assert(isPiece(ptype));
00090     Ptype ret=static_cast<Ptype>(static_cast<int>(ptype)|8); 
00091     assert(isPiece(ret));
00092     return ret;
00093   }
00094   inline Ptype unpromoteSafe(Ptype ptype)
00095   {
00096     if (! isPiece(ptype))
00097       return ptype;
00098     return unpromote(ptype);
00099   }
00100   
00105   inline Ptype promote(Ptype ptype)
00106   {
00107     assert(canPromote(ptype));
00108     Ptype ret=static_cast<Ptype>(static_cast<int>(ptype)-8); 
00109     assert(isPiece(ret));
00110     return ret;
00111   }
00112 
00113   inline bool isMajorBasic(Ptype ptype)
00114   {
00115     return ptype >= 14;
00116   }
00117   inline bool isMajor(Ptype ptype)
00118   {
00119     assert(isPiece(ptype));
00120     return isMajorBasic(unpromote(ptype));
00121   }
00122   inline bool isMajorNonPieceOK(Ptype ptype)
00123   {
00124     return (static_cast<int>(ptype)|8)>=14;
00125   }
00126   
00131   enum PtypeO {
00132     PTYPEO_MIN= PTYPE_EMPTY-16,
00133     PTYPEO_MAX= 15,
00134   };
00135   
00136 #define NEW_PTYPEO(player,ptype) static_cast<PtypeO>(static_cast<int>(ptype)-(16&static_cast<int>(player)))
00137   inline unsigned int ptypeOIndex(PtypeO ptypeo)
00138   {
00139     const int result = ptypeo - PTYPEO_MIN;
00140     assert(result >= 0);
00141     return result;
00142   }
00143   inline PtypeO newPtypeO(Player player,Ptype ptype)
00144   {
00145     return static_cast<PtypeO>(static_cast<int>(ptype)-(16&static_cast<int>(player)));
00146   }
00147   
00148   
00149   inline Ptype getPtype(PtypeO ptypeO)
00150   {
00151     return static_cast<Ptype>(static_cast<int>(ptypeO)& 15);
00152   }
00153   
00155   inline PtypeO promote(PtypeO ptypeO)
00156   {
00157     assert(canPromote(getPtype(ptypeO)));
00158     PtypeO ret=static_cast<PtypeO>(static_cast<int>(ptypeO)-8); 
00159     assert(isPiece(getPtype(ret)));
00160     return ret;
00161   }
00162   
00164   inline PtypeO promoteWithMask(PtypeO ptypeO,int promoteMask)
00165   {
00166     assert(promoteMask==0 || promoteMask==0x800000);
00167     PtypeO ret=static_cast<PtypeO>(static_cast<int>(ptypeO)-(promoteMask>>20)); 
00168     return ret;
00169   }
00170   
00172   inline PtypeO unpromote(PtypeO ptypeO)
00173   {
00174     return static_cast<PtypeO>(static_cast<int>(ptypeO)|8); 
00175   }
00176 
00177   bool isValidPtypeO(int ptypeO);
00178   
00182   inline bool isPiece(PtypeO ptypeO)
00183   {
00184     assert(isValidPtypeO(ptypeO));
00185     return isPiece(getPtype(ptypeO));
00186   }
00187 
00188   inline Player getOwner(PtypeO ptypeO)
00189   {
00190     assert(isPiece(ptypeO));
00191     return static_cast<Player>(static_cast<int>(ptypeO)>>31);
00192   }
00193   
00194 
00196   inline PtypeO captured(PtypeO ptypeO)
00197   {
00198     assert(isPiece(ptypeO));
00199     return static_cast<PtypeO>((static_cast<int>(ptypeO)|8)^(~15));
00200   }
00201   
00203   inline PtypeO alt(PtypeO ptypeO)
00204   {
00205     assert(isPiece(ptypeO));
00206     return static_cast<PtypeO>(static_cast<int>(ptypeO)^(~15));
00207   }
00208 
00213   inline PtypeO altIfPiece(PtypeO ptypeO)
00214   {
00215     int v=static_cast<int>(ptypeO);
00216     return static_cast<PtypeO>(v^((1-(v&15))&~15));
00217   }
00218 
00219   inline bool canPromote(PtypeO ptypeO)
00220   {
00221     return canPromote(getPtype(ptypeO));
00222   }
00223 
00224 
00228   inline bool isPromoted(PtypeO ptypeO)
00229   {
00230     assert(isValidPtypeO(ptypeO));
00231     return isPromoted(getPtype(ptypeO));
00232   }
00233 
00234 
00235   const PtypeO PTYPEO_EMPTY=newPtypeO(BLACK,PTYPE_EMPTY);
00236   const PtypeO PTYPEO_EDGE=newPtypeO(WHITE,PTYPE_EDGE);
00237   
00238   std::ostream& operator<<(std::ostream& os,const PtypeO ptypeO);
00239   
00240   const int PTYPEO_SIZE=PTYPEO_MAX-PTYPEO_MIN+1;
00241   
00242 } // namespace osl
00243 
00244 #endif /* OSL_PTYPE_H */
00245 // ;;; Local Variables:
00246 // ;;; mode:c++
00247 // ;;; c-basic-offset:2
00248 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines