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
00121 enum PtypeO {
00122 PTYPEO_MIN= PTYPE_EMPTY-16,
00123 PTYPEO_MAX= 15,
00124 };
00125
00126 #define NEW_PTYPEO(player,ptype) static_cast<PtypeO>(static_cast<int>(ptype)-(16&static_cast<int>(player)))
00127 inline unsigned int ptypeOIndex(PtypeO ptypeo)
00128 {
00129 const int result = ptypeo - PTYPEO_MIN;
00130 assert(result >= 0);
00131 return result;
00132 }
00133 inline PtypeO newPtypeO(Player player,Ptype ptype)
00134 {
00135 return static_cast<PtypeO>(static_cast<int>(ptype)-(16&static_cast<int>(player)));
00136 }
00137
00138
00139 inline Ptype getPtype(PtypeO ptypeO)
00140 {
00141 return static_cast<Ptype>(static_cast<int>(ptypeO)& 15);
00142 }
00143
00145 inline PtypeO promote(PtypeO ptypeO)
00146 {
00147 assert(canPromote(getPtype(ptypeO)));
00148 PtypeO ret=static_cast<PtypeO>(static_cast<int>(ptypeO)-8);
00149 assert(isPiece(getPtype(ret)));
00150 return ret;
00151 }
00152
00156 inline PtypeO promoteWithMask(PtypeO ptypeO,int promoteMask)
00157 {
00158 assert(promoteMask==0 || promoteMask==0x80000);
00159 PtypeO ret=static_cast<PtypeO>(static_cast<int>(ptypeO)-(promoteMask>>16));
00160 return ret;
00161 }
00162
00164 inline PtypeO unpromote(PtypeO ptypeO)
00165 {
00166 return static_cast<PtypeO>(static_cast<int>(ptypeO)|8);
00167 }
00168
00173 inline PtypeO unpromoteWithMask(PtypeO ptypeO,int promoteMask)
00174 {
00175 assert(promoteMask==0 || promoteMask==0x80000);
00176 PtypeO ret=static_cast<PtypeO>(static_cast<int>(ptypeO)+(promoteMask>>16));
00177 return ret;
00178 }
00179 bool isValidPtypeO(int ptypeO);
00180
00184 inline bool isPiece(PtypeO ptypeO)
00185 {
00186 assert(isValidPtypeO(ptypeO));
00187 return isPiece(getPtype(ptypeO));
00188 }
00189
00190 inline Player getOwner(PtypeO ptypeO)
00191 {
00192 assert(isPiece(ptypeO));
00193 return static_cast<Player>(static_cast<int>(ptypeO)>>31);
00194 }
00195
00196
00198 inline PtypeO captured(PtypeO ptypeO)
00199 {
00200 assert(isPiece(ptypeO));
00201 return static_cast<PtypeO>((static_cast<int>(ptypeO)|8)^(~15));
00202 }
00203
00204 inline bool canPromote(PtypeO ptypeO)
00205 {
00206 return canPromote(getPtype(ptypeO));
00207 }
00208
00209
00213 inline bool isPromoted(PtypeO ptypeO)
00214 {
00215 assert(isValidPtypeO(ptypeO));
00216 return isPromoted(getPtype(ptypeO));
00217 }
00218
00219
00220 const PtypeO PTYPEO_EMPTY=newPtypeO(BLACK,PTYPE_EMPTY);
00221 const PtypeO PTYPEO_EDGE=newPtypeO(WHITE,PTYPE_EDGE);
00222
00223 std::ostream& operator<<(std::ostream& os,const PtypeO ptypeO);
00224
00225 const int PTYPEO_SIZE=PTYPEO_MAX-PTYPEO_MIN+1;
00226
00227 }
00228
00229 #endif
00230
00231
00232
00233