neighboring8Direct.cc
Go to the documentation of this file.
00001 /* neighboring8Direct.cc
00002  */
00003 #include "osl/effect_util/neighboring8Direct.h"
00004 
00005 osl::effect_util::Neighboring8Direct::
00006 Table::Table()
00007 {
00008   init(BLACK);
00009   init(WHITE);
00010 }
00011 
00012 void osl::effect_util::Neighboring8Direct::
00013 Table::init(const Player player)
00014 {
00015   for (int p=PTYPE_PIECE_MIN; p<=PTYPE_MAX; ++p)
00016   {
00017     const Ptype ptype = static_cast<Ptype>(p);
00018     assert(isPiece(ptype));
00019     const PtypeO ptypeo = newPtypeO(player, ptype);
00020     const int mask = Ptype_Table.getMoveMask(ptype);
00021     for (int d=DIRECTION_MIN; d<=DIRECTION_MAX; ++d)
00022     {
00023       const Direction direction = static_cast<Direction>(d);
00024       if (! (mask & (1<<direction)))
00025         continue;
00026       const Offset offset = Board_Table.getOffset(player, direction);
00027       assert(! offset.zero());
00028       const int x = offset.dx();
00029       const int y = offset.dy();
00030       for (int dy=-1; dy<=1; ++dy)
00031       {
00032         for (int dx=-1; dx<=1; ++dx)
00033         {
00034           const Offset32 offset32 = Offset32(x+dx, y+dy);
00035           table[ptypeOIndex(ptypeo)][offset32.index()].
00036             has_unblockable_effect = true;
00037         }
00038       }
00039       if (isLong(direction))
00040       {
00041         assert(abs(x)<=1);
00042         assert(abs(y)<=1);
00043         for (int i=1; i<8; ++i)
00044         {
00045           const int long_x = x*i;
00046           const int long_y = y*i;
00047           const int target_x = x*(i+1);
00048           const int target_y = y*(i+1);
00049           const Offset32 offset32 = Offset32(target_x, target_y);
00050           Entry& e = table[ptypeOIndex(ptypeo)][offset32.index()];
00051           e.nearest = Offset(long_x, long_y);
00052         }
00053         for (int i=1; i<9; ++i)
00054         {
00055           const int long_x = x*i;
00056           const int long_y = y*i;
00057           for (int dy=-1; dy<=1; ++dy)
00058           {
00059             const int target_y = long_y+dy;
00060             if ((target_y < -8) || (8 < target_y))
00061               continue;
00062             for (int dx=-1; dx<=1; ++dx)
00063             {
00064               const int target_x = long_x+dx;
00065               if ((target_x < -8) || (8 < target_x))
00066                 continue;
00067               const Offset32 offset32 = Offset32(target_x, target_y);
00068               Entry& e = table[ptypeOIndex(ptypeo)][offset32.index()];
00069               // 近いところ優先
00070               if (e.nearest.zero())
00071               {         
00072                 e.nearest = Offset(long_x, long_y);
00073               }
00074             }
00075           }
00076         }
00077       }
00078     }
00079   }
00080 }
00081 
00082 bool osl::effect_util::Neighboring8Direct::
00083 hasEffectFromTo(const NumEffectState& state, PtypeO ptypeo, Square from, 
00084                 Square target, Direction d)
00085 {
00086   target += Board_Table.getOffsetForBlack(d); // 8 近傍全て試すなら手番による符合変換は不要
00087   return target.isOnBoard()
00088     && state.hasEffectIf(ptypeo, from, target);
00089 }
00090 
00091 bool osl::effect_util::Neighboring8Direct::
00092 hasEffectNaive(const NumEffectState& state, PtypeO ptypeo, Square from, 
00093                Square target)
00094 {
00095   const Ptype ptype = getPtype(ptypeo);
00096   if (! Ptype_Table.hasLongMove(ptype))
00097   {
00098     if (abs(from.y() - target.y()) > 3) // knight だけ3
00099       return false;
00100     if (abs(from.x() - target.x()) > 2)
00101       return false;
00102   }
00103   else if (ptype == LANCE)
00104   {
00105     if (abs(from.x() - target.x()) > 1)
00106       return false;
00107   }
00108 
00109   // naive な実装
00110   return hasEffectFromTo(state, ptypeo, from, target, UL)
00111     || hasEffectFromTo(state, ptypeo, from, target, U)
00112     || hasEffectFromTo(state, ptypeo, from, target, UR)
00113     || hasEffectFromTo(state, ptypeo, from, target, L)
00114     || hasEffectFromTo(state, ptypeo, from, target, R)
00115     || hasEffectFromTo(state, ptypeo, from, target, DL)
00116     || hasEffectFromTo(state, ptypeo, from, target, D)
00117     || hasEffectFromTo(state, ptypeo, from, target, DR);
00118 }
00119 
00120 // ;;; Local Variables:
00121 // ;;; mode:c++
00122 // ;;; c-basic-offset:2
00123 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines