square.h
Go to the documentation of this file.
00001 /* square.h
00002  */
00003 #ifndef FEATURE_POSITION_H
00004 #define FEATURE_POSITION_H
00005 
00006 #include "osl/rating/feature.h"
00007 #include "osl/move.h"
00008 #include "osl/ptypeTable.h"
00009 #include <string>
00010 
00011 namespace osl
00012 {
00013   namespace rating
00014   {
00015     struct DropPtype
00016     {
00017       Ptype ptype;
00018       bool drop;
00019       DropPtype(Ptype p, bool d) : ptype(p), drop(d)
00020       {
00021       }
00022       bool match(Move m) const
00023       {
00024         return (m.ptype() == ptype) && (m.isDrop() == drop);
00025       }
00026       static std::string name(Ptype ptype, bool drop) 
00027       {
00028         return std::string(Ptype_Table.getCsaName(ptype)) + (drop ? "d" : "m");
00029       }
00030       enum { UNIT = PTYPE_MAX+1-PTYPE_PIECE_MIN + (PTYPE_MAX+1 - (PTYPE_BASIC_MIN+1)) };
00031       static int index(Move move) 
00032       {
00033         const Ptype ptype = move.ptype();
00034         int index;
00035         if (! isBasic(ptype) || ptype == KING)
00036           index = ptype - PTYPE_PIECE_MIN;
00037         else
00038           index = (PTYPE_BASIC_MIN+1-PTYPE_PIECE_MIN) + (ptype - (PTYPE_BASIC_MIN+1))*2 + move.isDrop();
00039         return index;
00040       }
00041     };
00042 
00043     class RelativeKingX : public Feature
00044     {
00045       int x, old_x;
00046       Ptype ptype;
00047       bool attack;
00048     public:
00049       static const std::string name(int x, int old_x, bool /*attack*/, Ptype);
00050       RelativeKingX(int ix, int iox, bool a, Ptype p) 
00051         : Feature(name(ix,iox,a,p)), x(ix), old_x(iox), ptype(p), attack(a) {}
00052       // [0,8]
00053       static int makeX(bool attack, const NumEffectState& state, Move move) 
00054       {
00055         const Square king = state.kingSquare(attack ? alt(move.player()) : move.player());
00056         return abs(move.to().x() - king.x());
00057       }
00058       // [0,9]
00059       static int makeOldX(bool attack, const NumEffectState& state, Move move) 
00060       {
00061         if (move.isDrop())
00062           return 9;
00063         const Square king = state.kingSquare(attack ? alt(move.player()) : move.player());
00064         return abs(move.from().x() - king.x());
00065       }
00066       bool match(const NumEffectState& state, Move move, const RatingEnv&) const
00067       {
00068         if (ptype != move.ptype())
00069           return false;
00070         return makeX(attack, state, move) == x && makeOldX(attack, state, move) == old_x;
00071       }
00072       static int index(bool attack, const NumEffectState& state, Move move)
00073       {
00074         const int x = makeX(attack, state, move), old_x = makeOldX(attack, state, move);
00075         const int ptype = move.ptype() - PTYPE_PIECE_MIN;
00076         return (x*10+old_x)*(PTYPE_MAX+1 - PTYPE_PIECE_MIN) + ptype;
00077       }
00078     };
00079 
00080     class RelativeKingY : public Feature
00081     {
00082       int y, old_y;
00083       Ptype ptype;
00084       bool attack;
00085     public:
00086       static const std::string name(int y, int old_y, bool /*attack*/, Ptype ptype);
00087       RelativeKingY(int iy, int ioy, bool a, Ptype p) 
00088         : Feature(name(iy,ioy,a,p)), y(iy), old_y(ioy), ptype(p), attack(a) {}
00089       // [-8,8]
00090       static int makeY(bool attack, const NumEffectState& state, Move move)
00091       {
00092         const Square king = state.kingSquare(attack ? alt(move.player()) : move.player());
00093         int diff = move.to().y() - king.y();
00094         if (move.player() == WHITE)
00095           diff = -diff;
00096         return diff;
00097       }
00098       // [-8,9]
00099       static int makeOldY(bool attack, const NumEffectState& state, Move move)
00100       {
00101         if (move.isDrop())
00102           return 9;
00103         const Square king = state.kingSquare(attack ? alt(move.player()) : move.player());
00104         int diff = move.from().y() - king.y();
00105         if (move.player() == WHITE)
00106           diff = -diff;
00107         return diff;
00108       }
00109       bool match(const NumEffectState& state, Move move, const RatingEnv&) const
00110       {
00111         if (ptype != move.ptype())
00112           return false;
00113         return makeY(attack, state, move) == y && makeOldY(attack, state, move) == old_y;
00114       }
00115       static int index(bool attack, const NumEffectState& state, Move move)
00116       {
00117         const int y = makeY(attack, state, move) + 8;
00118         const int old_y = makeOldY(attack, state, move) + 8;
00119         const int ptype = move.ptype() - PTYPE_PIECE_MIN;
00120         return (y*18+old_y)*(PTYPE_MAX+1 - PTYPE_PIECE_MIN) + ptype;
00121       }
00122     };
00123 
00124     class SquareX : public Feature, DropPtype
00125     {
00126       int x;
00127       static const std::string name(int x);
00128     public:
00129       SquareX(int ix, Ptype ptype, bool drop) 
00130         : Feature(name(ix)+DropPtype::name(ptype, drop)), DropPtype(ptype, drop), x(ix) {}
00131       static int makeX(Move move)
00132       {
00133         int mx = move.to().x();
00134         if (mx > 5)
00135           mx = 10-mx;
00136         return mx;
00137       }
00138       bool match(const NumEffectState& , Move move, const RatingEnv&) const
00139       {
00140         if (! DropPtype::match(move))
00141           return false;
00142         return x == makeX(move);
00143       }
00144     };
00145 
00146     class SquareY : public Feature, DropPtype
00147     {
00148       int y;
00149       static const std::string name(int y);
00150     public:
00151       SquareY(int iy, Ptype ptype, bool drop) 
00152         : Feature(name(iy)+DropPtype::name(ptype, drop)), DropPtype(ptype, drop), y(iy) {}
00153       static int makeY(Move move) 
00154       {
00155         int my = move.to().y();
00156         if (move.player() == WHITE)
00157           my = 10 - my;
00158         return my;
00159       }
00160       bool match(const NumEffectState&, Move move, const RatingEnv&) const
00161       {
00162         if (! DropPtype::match(move))
00163           return false;
00164         return y == makeY(move);
00165       }
00166     };
00167   }
00168 }
00169 
00170 #endif /* FEATURE_POSITION_H */
00171 // ;;; Local Variables:
00172 // ;;; mode:c++
00173 // ;;; c-basic-offset:2
00174 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines