00001
00002
00003 #include "osl/checkmate/immediateCheckmateTable.h"
00004 #include "osl/boardTable.h"
00005 #include "osl/ptypeTable.h"
00006 namespace
00007 {
00008 using namespace osl;
00009 bool canCheckmate(Ptype ptype,Direction dir,unsigned int mask)
00010 {
00011
00012 if(ptype==KING || ptype==PAWN) return false;
00013
00014 if(!(Ptype_Table.getMoveMask(ptype)&
00015 (dirToMask(dir) | dirToMask(shortToLong(dir))))) return false;
00016 int dx=Board_Table.getDxForBlack(dir);
00017 int dy=Board_Table.getDyForBlack(dir);
00018 for(int l=0;l<8;l++){
00019 if((mask&(1<<l))==0) continue;
00020 Direction dir1=static_cast<Direction>(l);
00021 int dx1=Board_Table.getDxForBlack(dir1);
00022 int dy1=Board_Table.getDyForBlack(dir1);
00023 Offset32 o32(dx-dx1,dy-dy1);
00024 if(!Ptype_Table.getEffect(newPtypeO(BLACK,ptype),o32).hasEffect())
00025 return false;
00026 }
00027 return true;
00028 }
00029 }
00030
00031 osl::checkmate::ImmediateCheckmateTable::ImmediateCheckmateTable()
00032 {
00033
00034 for(int i=0;i<0x100;i++){
00035 for(int k=PTYPE_BASIC_MIN;k<=PTYPE_MAX;k++){
00036 unsigned char mask=0;
00037 Ptype ptype=static_cast<Ptype>(k);
00038 for(int j=0;j<8;j++){
00039
00040 if((i&(0x1<<j))!=0)continue;
00041 Direction dir=static_cast<Direction>(j);
00042 if(canCheckmate(ptype,dir,i))
00043 mask|=(1<<j);
00044 }
00045 ptypeDropMasks(i,ptype)=mask;
00046 }
00047 }
00048
00049 for(int i=0;i<0x10000;i++){
00050 unsigned char ptypeMask=0;
00051 for(int k=PTYPE_BASIC_MIN;k<=PTYPE_MAX;k++){
00052 Ptype ptype=static_cast<Ptype>(k);
00053 for(int j=0;j<8;j++){
00054
00055 if((i&(0x1<<j))==0) continue;
00056
00057 if((i&(0x100<<j))!=0)continue;
00058 Direction dir=static_cast<Direction>(j);
00059 if(canCheckmate(ptype,dir,(i>>8)&0xff)){
00060 ptypeMask|=1u<<(k-PTYPE_BASIC_MIN);
00061 goto nextPtype;
00062 }
00063 }
00064 nextPtype:;
00065 }
00066 dropPtypeMasks[i]=ptypeMask;
00067 }
00068
00069 for(int k=PTYPE_BASIC_MIN;k<=PTYPE_MAX;k++){
00070 Ptype ptype=static_cast<Ptype>(k);
00071 for(int j=0;j<8;j++){
00072 unsigned int mask=0;
00073 Direction dir=static_cast<Direction>(j);
00074 if(Ptype_Table.getMoveMask(ptype)&
00075 (dirToMask(dir) | dirToMask(shortToLong(dir)))){
00076 int dx=Board_Table.getDxForBlack(dir);
00077 int dy=Board_Table.getDyForBlack(dir);
00078 for(int l=0;l<8;l++){
00079 Direction dir1=static_cast<Direction>(l);
00080 int dx1=Board_Table.getDxForBlack(dir1);
00081 int dy1=Board_Table.getDyForBlack(dir1);
00082 Offset32 o32(dx-dx1,dy-dy1);
00083 if(!Ptype_Table.getEffect(newPtypeO(BLACK,ptype),o32).hasEffect()){
00084 if(!Board_Table.getShortOffsetNotKnight(o32).zero() &&
00085 !(dx==-dx1 && dy==-dy1)
00086 ){
00087 mask|=1<<l;
00088 }
00089 }
00090 }
00091 }
00092 blockingMasks(ptype,dir)=mask;
00093 }
00094 }
00095
00096 for(int k=PTYPE_PIECE_MIN;k<=PTYPE_MAX;k++){
00097 Ptype ptype=static_cast<Ptype>(k);
00098 for(int j=0;j<8;j++){
00099 unsigned int mask=0x1ff;
00100 Direction dir=static_cast<Direction>(j);
00101 if(Ptype_Table.getMoveMask(ptype)&
00102 (dirToMask(dir) | dirToMask(shortToLong(dir)))){
00103 mask=0;
00104 int dx=Board_Table.getDxForBlack(dir);
00105 int dy=Board_Table.getDyForBlack(dir);
00106 for(int l=0;l<8;l++){
00107 Direction dir1=static_cast<Direction>(l);
00108 int dx1=Board_Table.getDxForBlack(dir1);
00109 int dy1=Board_Table.getDyForBlack(dir1);
00110 Offset32 o32(dx-dx1,dy-dy1);
00111 if(dir!= dir1 &&
00112 !Ptype_Table.getEffect(newPtypeO(BLACK,ptype),o32).hasEffect()){
00113 mask|=1<<l;
00114 }
00115 }
00116 }
00117 noEffectMasks(ptype,dir)=mask;
00118 }
00119 }
00120 }
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130