effect5x3Table.cc
Go to the documentation of this file.
00001 /* effect5x3Table.cc
00002  */
00003 
00004 #include "osl/progress/effect5x3Table.h"
00005 #include "osl/centering5x3.h"
00006 #include "osl/ptype.h"
00007 #include "osl/ptypeTable.h"
00008 #include "osl/boardTable.h"
00009 
00010 #include <iostream>
00011 
00012 namespace osl
00013 {
00014   namespace progress
00015   {
00016     namespace
00017     {
00024       int countShort(PtypeO ptypeO,int center_dx, int center_dy){
00025         int ret=0;
00026         for(int dy=std::max(-8,center_dy-1);
00027             dy<=std::min(8,center_dy+1);dy++)
00028           for(int dx=std::max(-8,center_dx-2);
00029               dx<=std::min(8,center_dx+2);dx++){
00030             EffectContent ec=Ptype_Table.getEffect(ptypeO,Offset32(dx,dy));
00031             // unBlockableではなくて,ほんとのshortが欲しい
00032             if(ec == EffectContent::DIRECT()){
00033               ret+=8;
00034             }
00035           }
00036         return ret;
00037       }
00044       LongEffect calcLong(Direction d,int center_dx, int center_dy){
00045         LongEffect ret;
00046         ret.minIndex=0;
00047         ret.maxIndex=0;
00048         int dy_min=std::max(-8,center_dy-1);
00049         int dy_max=std::min(8,center_dy+1);
00050         int dx_min=std::max(-8,center_dx-2);
00051         int dx_max=std::min(8,center_dx+2);
00052         int dx=Board_Table.getDxForBlack(d);
00053         int dy=Board_Table.getDyForBlack(d);
00054         assert(dx!=0 || dy!=0);
00055         int i;
00056         
00057         for(i=1;i<9;i++){
00058           if(dx_min<=dx*i && dx*i <=dx_max &&
00059              dy_min<=dy*i && dy*i <=dy_max) break;
00060         }
00061         if(i==9) return ret;
00062         ret.minIndex=i;
00063         for(;i<9;i++){
00064           if(!(dx_min<=dx*i && dx*i <=dx_max &&
00065                dy_min<=dy*i && dy*i <=dy_max)) break;
00066         }
00067         ret.maxIndex=i-1;
00068         ret.offset=Offset(dx,dy);
00069         assert(!ret.offset.zero());
00070         return ret;
00071       }
00078       LongEffect4 calcLong4(PtypeO ptypeO,int center_dx, int center_dy){
00079         LongEffect4 ret;
00080         int index=0;
00081         
00082         Player pl=getOwner(ptypeO);
00083         for(int i=0;i<8;i++){
00084           Direction d=static_cast<Direction>(i);
00088           Offset32 o32(Board_Table.getDxForBlack(d)*playerToMul(pl),
00089                        Board_Table.getDyForBlack(d)*playerToMul(pl));
00090           Direction longD=Board_Table.getLongDirection<BLACK>(o32);
00091           if((Ptype_Table.getMoveMask(getPtype(ptypeO))&
00092               dirToMask(shortToLong(d)))!=0){
00093             assert(index<4);
00094             ret[index]=calcLong(longD,center_dx,center_dy);
00095             if(!ret[index].offset.zero())index++;
00096           }
00097         }
00098         return ret;
00099       }
00100     }
00101 #ifndef MINIMAL
00102     std::ostream& operator<<(std::ostream& os,LongEffect const& longEffect){
00103       return os << "(" << longEffect.offset << "," << longEffect.minIndex 
00104                 << "," << longEffect.maxIndex << ")";
00105     }
00106 #endif
00107   }
00108 }
00109 
00110 void osl::progress::Effect5x3Table::setupOnStand()
00111 {
00112   onStand[PAWN]  =StandPAWN;
00113   onStand[LANCE] =StandLANCE;
00114   onStand[KNIGHT]=StandKNIGHT;
00115   onStand[SILVER]=StandSILVER;
00116   onStand[GOLD]  =StandGOLD;
00117   onStand[BISHOP]=StandBISHOP;
00118   onStand[ROOK]  =StandROOK;
00119   for(int i=PTYPE_PIECE_MIN;i<=PTYPE_MAX;i++){
00120     Ptype ptype=static_cast<Ptype>(i);
00121     if(unpromote(ptype)!=ptype)
00122       onStand[ptype]=onStand[unpromote(ptype)];
00123   }
00124 }
00125 
00126 
00127 void osl::progress::Effect5x3Table::setupShortEffect()
00128 {
00129   Player pl=BLACK;
00130   for(int i=0;i<2;i++,pl=alt(pl)){
00131     for(int j=PTYPE_PIECE_MIN;j<=PTYPE_MAX;j++){
00132       PtypeO ptypeO=newPtypeO(pl,static_cast<Ptype>(j));
00133       for(int dy= -8;dy<=8;dy++)
00134         for(int dx= -8;dx<=8;dx++){
00135           Offset32 o32(dx,dy);
00136           shortEffect[ptypeOIndex(ptypeO)][o32.index()]=
00137             countShort(ptypeO,dx,dy);
00138         }
00139     }
00140   }
00141 }
00142 void osl::progress::Effect5x3Table::setupBlockEffect()
00143 {
00144   Player pl=BLACK;
00145   for(int i=0;i<2;i++,pl=alt(pl)){
00146     for(int j=0;j<8;j++){
00147       Direction d=static_cast<Direction>(j);
00148       for(int dy= -8;dy<=8;dy++)
00149         for(int dx= -8;dx<=8;dx++){
00150           Offset32 o32(dx,dy);
00151           blockEffect[j][o32.index()]=
00152             calcLong(d,dx,dy);
00153         }
00154     }
00155   }
00156 }
00157 void osl::progress::Effect5x3Table::setupLongEffect()
00158 {
00159   Player pl=BLACK;
00160   for(int i=0;i<2;i++,pl=alt(pl)){
00161     for(int j=PTYPE_PIECE_MIN;j<=PTYPE_MAX;j++){
00162       PtypeO ptypeO=newPtypeO(pl,static_cast<Ptype>(j));
00163       for(int dy= -8;dy<=8;dy++)
00164         for(int dx= -8;dx<=8;dx++){
00165           Offset32 o32(dx,dy);
00166           longEffect[ptypeOIndex(ptypeO)][o32.index()]=
00167             calcLong4(ptypeO,dx,dy);
00168         }
00169     }
00170   }
00171 }
00172 
00173 void osl::progress::Effect5x3Table::setupAttackEffect()
00174 {
00175   // 黒の攻める際のテーブルを作成
00176   // 玉は白
00177   for(int x=1;x<=9;x++)
00178     for(int y=1;y<=9;y++){
00179       const Square king(x,y);
00180       const Square center = Centering5x3::adjustCenter(king);
00181       for(int dx=0;dx<5;dx++){
00182         for(int dy=0;dy<3;dy++){
00183           const Square p(center.x()+dx-2,center.y()+dy-1);
00184           int val=16-std::abs(p.x()-king.x());
00185           attackEffect[BLACK][king.index()][dx*3+dy]=val;
00186         }
00187       }
00188     }
00189   for(int x=1;x<=9;x++)
00190     for(int y=1;y<=9;y++){
00191       const Square king(x,y);
00192       const Square r_king=king.rotate180();
00193       for(int dx=0;dx<5;dx++){
00194         for(int dy=0;dy<3;dy++){
00195           int r_dx=4-dx,r_dy=2-dy;
00196           attackEffect[WHITE][king.index()][dx*3+dy]=
00197             attackEffect[BLACK][r_king.index()][r_dx*3+r_dy];
00198         }
00199       }
00200     }
00201 }
00202 void osl::progress::Effect5x3Table::setupDefenseEffect()
00203 {
00204   // 白が守る際のテーブルを作成
00205   // 玉は白
00206   for(int x=1;x<=9;x++)
00207     for(int y=1;y<=9;y++){
00208       const Square king(x,y);
00209       const Square center = Centering5x3::adjustCenter(king);
00210       for(int dx=0;dx<5;dx++){
00211         for(int dy=0;dy<3;dy++){
00212           const Square p(center.x()+dx-2,center.y()+dy-1);
00213           int val=16-std::abs(p.x()-king.x());
00214           defenseEffect[WHITE][king.index()][dx*3+dy]=val;
00215         }
00216       }
00217     }
00218   for(int x=1;x<=9;x++)
00219     for(int y=1;y<=9;y++){
00220       const Square king(x,y);
00221       const Square r_king=king.rotate180();
00222       for(int dx=0;dx<5;dx++){
00223         for(int dy=0;dy<3;dy++){
00224           int r_dx=4-dx,r_dy=2-dy;
00225           defenseEffect[BLACK][king.index()][dx*3+dy]=
00226             defenseEffect[WHITE][r_king.index()][r_dx*3+r_dy];
00227         }
00228       }
00229     }
00230 }
00231 
00232 osl::progress::Effect5x3Table::Effect5x3Table()
00233 {
00234   setupOnStand();
00235   setupShortEffect();
00236   setupBlockEffect();
00237   setupLongEffect();
00238   setupAttackEffect();
00239   setupDefenseEffect();
00240 }
00241 
00242 /* ------------------------------------------------------------------------- */
00243 // ;;; Local Variables:
00244 // ;;; mode:c++
00245 // ;;; c-basic-offset:2
00246 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines