00001 #ifndef _PTYPE_H
00002 #define _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
00099 inline Ptype promote(Ptype ptype)
00100 {
00101 assert(canPromote(ptype));
00102 Ptype ret=static_cast<Ptype>(static_cast<int>(ptype)-8);
00103 assert(isPiece(ret));
00104 return ret;
00105 }
00106
00107 inline bool isMajorBasic(Ptype ptype)
00108 {
00109 return ptype >= 14;
00110 }
00111 inline bool isMajor(Ptype ptype)
00112 {
00113 assert(isPiece(ptype));
00114 return isMajorBasic(unpromote(ptype));
00115 }
00116 inline bool isMajorNonPieceOK(Ptype ptype)
00117 {
00118 return (static_cast<int>(ptype)|8)>=14;
00119 }
00120
00125 enum PtypeO {
00126 PTYPEO_MIN= PTYPE_EMPTY-16,
00127 PTYPEO_MAX= 15,
00128 };
00129
00130 #define NEW_PTYPEO(player,ptype) static_cast<PtypeO>(static_cast<int>(ptype)-(16&static_cast<int>(player)))
00131 inline unsigned int ptypeOIndex(PtypeO ptypeo)
00132 {
00133 const int result = ptypeo - PTYPEO_MIN;
00134 assert(result >= 0);
00135 return result;
00136 }
00137 inline PtypeO newPtypeO(Player player,Ptype ptype)
00138 {
00139 return static_cast<PtypeO>(static_cast<int>(ptype)-(16&static_cast<int>(player)));
00140 }
00141
00142
00143 inline Ptype getPtype(PtypeO ptypeO)
00144 {
00145 return static_cast<Ptype>(static_cast<int>(ptypeO)& 15);
00146 }
00147
00149 inline PtypeO promote(PtypeO ptypeO)
00150 {
00151 assert(canPromote(getPtype(ptypeO)));
00152 PtypeO ret=static_cast<PtypeO>(static_cast<int>(ptypeO)-8);
00153 assert(isPiece(getPtype(ret)));
00154 return ret;
00155 }
00156
00158 inline PtypeO promoteWithMask(PtypeO ptypeO,int promoteMask)
00159 {
00160 assert(promoteMask==0 || promoteMask==0x800000);
00161 PtypeO ret=static_cast<PtypeO>(static_cast<int>(ptypeO)-(promoteMask>>20));
00162 return ret;
00163 }
00164
00166 inline PtypeO unpromote(PtypeO ptypeO)
00167 {
00168 return static_cast<PtypeO>(static_cast<int>(ptypeO)|8);
00169 }
00170
00171 bool isValidPtypeO(int ptypeO);
00172
00176 inline bool isPiece(PtypeO ptypeO)
00177 {
00178 assert(isValidPtypeO(ptypeO));
00179 return isPiece(getPtype(ptypeO));
00180 }
00181
00182 inline Player getOwner(PtypeO ptypeO)
00183 {
00184 assert(isPiece(ptypeO));
00185 return static_cast<Player>(static_cast<int>(ptypeO)>>31);
00186 }
00187
00188
00190 inline PtypeO captured(PtypeO ptypeO)
00191 {
00192 assert(isPiece(ptypeO));
00193 return static_cast<PtypeO>((static_cast<int>(ptypeO)|8)^(~15));
00194 }
00195
00197 inline PtypeO alt(PtypeO ptypeO)
00198 {
00199 assert(isPiece(ptypeO));
00200 return static_cast<PtypeO>(static_cast<int>(ptypeO)^(~15));
00201 }
00202
00207 inline PtypeO altIfPiece(PtypeO ptypeO)
00208 {
00209 int v=static_cast<int>(ptypeO);
00210 return static_cast<PtypeO>(v^((1-(v&15))&~15));
00211 }
00212
00213 inline bool canPromote(PtypeO ptypeO)
00214 {
00215 return canPromote(getPtype(ptypeO));
00216 }
00217
00218
00222 inline bool isPromoted(PtypeO ptypeO)
00223 {
00224 assert(isValidPtypeO(ptypeO));
00225 return isPromoted(getPtype(ptypeO));
00226 }
00227
00228
00229 const PtypeO PTYPEO_EMPTY=newPtypeO(BLACK,PTYPE_EMPTY);
00230 const PtypeO PTYPEO_EDGE=newPtypeO(WHITE,PTYPE_EDGE);
00231
00232 std::ostream& operator<<(std::ostream& os,const PtypeO ptypeO);
00233
00234 const int PTYPEO_SIZE=PTYPEO_MAX-PTYPEO_MIN+1;
00235
00236 }
00237
00238 #endif
00239
00240
00241
00242