feature.cc
Go to the documentation of this file.
00001 /* feature.cc
00002  */
00003 #include "osl/rating/feature.h"
00004 #include "osl/state/simpleState.tcc"
00005 #include <sstream>
00006 
00007 osl::rating::Feature::~Feature()
00008 {
00009 }
00010 
00011 const osl::CArray<const char*,4> osl::rating::Check::check_property = {{ "Di", "DO", "OC", "Bo" }};
00012 osl::rating::Check::Check(int p) : Feature(check_property[p]), property(p) 
00013 {
00014 }
00015 
00016 bool osl::rating::Check::match(const NumEffectState& state, Move move, const RatingEnv&) const
00017 {
00018   using namespace osl::move_classifier;
00019   if (property == 0 || property == 1) {
00020     if (! (PlayerMoveAdaptor<osl::move_classifier::DirectCheck>::isMember(state, move)
00021            && ! ConditionAdaptor<osl::move_classifier::OpenCheck>::isMember(state, move)))
00022       return false;
00023     return property == openLong(state, move);
00024   }
00025   if (property == 2)
00026     return ! PlayerMoveAdaptor<osl::move_classifier::DirectCheck>::isMember(state, move)
00027       && ConditionAdaptor<osl::move_classifier::OpenCheck>::isMember(state, move);
00028   return PlayerMoveAdaptor<osl::move_classifier::DirectCheck>::isMember(state, move)
00029     && ConditionAdaptor<osl::move_classifier::OpenCheck>::isMember(state, move);
00030 }
00031 
00032 const std::string osl::rating::
00033 Block::name(int self, int opponent)
00034 {
00035   std::ostringstream os;
00036   os << "B" << self << opponent;
00037   return os.str();  
00038 }
00039 
00040 const std::string osl::rating::Open::name(int property)
00041 {
00042   std::ostringstream os;
00043   os << "Open" << property / 4 << property % 4;
00044   return os.str();
00045 }
00046 
00047 struct osl::rating::ImmediateAddSupport::Test
00048 {
00049   bool *result;
00050   const NumEffectState& state;
00051   Move move;
00052   Test(bool *r, const NumEffectState& s, Move m)
00053     : result(r), state(s), move(m)
00054   {
00055   }
00056   template <Player P>
00057   void doAction(Piece /*piece*/, Square last_attack)
00058   {
00059     const Piece attacked = state.pieceAt(last_attack);
00060     if (attacked.isOnBoardByOwner(state.turn())
00061         && state.hasEffectIf(move.ptypeO(), move.to(), last_attack))
00062       *result = true;
00063   }
00064 };
00065 
00066 osl::rating::
00067 ImmediateAddSupport::ImmediateAddSupport(Ptype s, Ptype a)
00068   : Feature(std::string(Ptype_Table.getCsaName(s))+":"+Ptype_Table.getCsaName(a)),
00069     self(s), attack(a)
00070 {
00071 }
00072 
00073 bool osl::rating::
00074 ImmediateAddSupport::match(const NumEffectState& state, Move move, const RatingEnv& env) const
00075 {
00076   if (move.ptype() != self)
00077     return false;
00078   const Move last_move=env.history.lastMove();
00079   if (! last_move.isNormal()) 
00080     return false;
00081   if (last_move.ptype() != attack)
00082     return false;
00083   const Square last_to = last_move.to();
00084   if (last_to==move.to())
00085     return false;               // TakeBack は除く
00086   bool result = false;
00087   Test action(&result, state, move);
00088   state.forEachEffectOfPiece(state.pieceOnBoard(last_to), action);
00089   return result;
00090 }
00091 
00092 int osl::rating::
00093 ImmediateAddSupport::index(const NumEffectState& state, Move move, const RatingEnv& env)
00094 {
00095   const Move last_move=env.history.lastMove();
00096   if (! last_move.isNormal()) 
00097     return -1;
00098   const Square last_to = last_move.to();
00099   if (last_to==move.to())
00100     return -1;          // TakeBack は除く
00101   bool result = false;
00102   const Piece last_piece = state.pieceOnBoard(last_to);
00103   // BoardMask state.changedEffects(alt(Turn))?
00104   if (! Ptype_Table.hasLongMove(last_piece.ptype()))
00105   {
00106     Test action(&result, state, move);
00107     state.forEachEffectOfPiece(last_piece, action);
00108   }
00109   else
00110   {
00111     const Player Turn = state.turn();
00112     PieceMask pieces = state.piecesOnBoard(Turn) & state.effectedMask(alt(Turn));
00113     mask_t m = pieces.getMask(0);
00114     while (m.any()) {
00115       const Piece p = state.pieceOf(m.takeOneBit());
00116       if (state.hasEffectByPiece(last_piece, p.square())
00117           && state.hasEffectIf(move.ptypeO(), move.to(), p.square())) {
00118         result = true;
00119         break;
00120       }
00121     }
00122 #if OSL_WORDSIZE == 32
00123     if (! result) {
00124       m = pieces.getMask(1);
00125       while (m.any()) {
00126         const Piece p = state.pieceOf(m.takeOneBit()+32);
00127         if (state.hasEffectByPiece(last_piece, p.square())
00128             && state.hasEffectIf(move.ptypeO(), move.to(), p.square())) {
00129           result = true;
00130           break;
00131         }
00132       }
00133     }
00134 #endif
00135   }
00136   if (! result)
00137     return -1;
00138   return (move.ptype() - PTYPE_PIECE_MIN) * (PTYPE_MAX+1 - PTYPE_PIECE_MIN)
00139     + last_move.ptype() - PTYPE_PIECE_MIN;
00140 }
00141 
00142 const std::string osl::rating::Chase::name(Ptype self, Ptype target, bool drop, OpponentType opponent_type)
00143 {
00144   return std::string(Ptype_Table.getCsaName(self))
00145     +(drop ? "d" : "m")+">"+Ptype_Table.getCsaName(target)
00146     +(opponent_type == CAPTURE ? "c" : (opponent_type == DROP ? "d" : "e"));
00147 }
00148 
00149 /* ------------------------------------------------------------------------- */
00150 // ;;; Local Variables:
00151 // ;;; mode:c++
00152 // ;;; c-basic-offset:2
00153 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines