square.h
Go to the documentation of this file.
/* square.h
*/
#ifndef FEATURE_POSITION_H
#define FEATURE_POSITION_H
#include "osl/rating/feature.h"
#include "osl/basic_type.h"
#include "osl/bits/ptypeTable.h"
#include <string>
namespace osl
{
namespace rating
{
struct DropPtype
{
Ptype ptype;
bool drop;
DropPtype(Ptype p, bool d) : ptype(p), drop(d)
{
}
bool match(Move m) const
{
return (m.ptype() == ptype) && (m.isDrop() == drop);
}
static std::string name(Ptype ptype, bool drop)
{
return std::string(Ptype_Table.getCsaName(ptype)) + (drop ? "d" : "m");
}
enum { UNIT = PTYPE_MAX+1-PTYPE_PIECE_MIN + (PTYPE_MAX+1 - (PTYPE_BASIC_MIN+1)) };
static int index(Move move)
{
const Ptype ptype = move.ptype();
int index;
if (! isBasic(ptype) || ptype == KING)
index = ptype - PTYPE_PIECE_MIN;
else
index = (PTYPE_BASIC_MIN+1-PTYPE_PIECE_MIN) + (ptype - (PTYPE_BASIC_MIN+1))*2 + move.isDrop();
return index;
}
};
class RelativeKingX : public Feature
{
int x, old_x;
Ptype ptype;
bool attack;
public:
static const std::string name(int x, int old_x, bool /*attack*/, Ptype);
RelativeKingX(int ix, int iox, bool a, Ptype p)
: Feature(name(ix,iox,a,p)), x(ix), old_x(iox), ptype(p), attack(a) {}
// [0,8]
static int makeX(bool attack, const NumEffectState& state, Move move)
{
const Square king = state.kingSquare(attack ? alt(move.player()) : move.player());
return abs(move.to().x() - king.x());
}
// [0,9]
static int makeOldX(bool attack, const NumEffectState& state, Move move)
{
if (move.isDrop())
return 9;
const Square king = state.kingSquare(attack ? alt(move.player()) : move.player());
return abs(move.from().x() - king.x());
}
bool match(const NumEffectState& state, Move move, const RatingEnv&) const
{
if (ptype != move.ptype())
return false;
return makeX(attack, state, move) == x && makeOldX(attack, state, move) == old_x;
}
static int index(bool attack, const NumEffectState& state, Move move)
{
const int x = makeX(attack, state, move), old_x = makeOldX(attack, state, move);
const int ptype = move.ptype() - PTYPE_PIECE_MIN;
return (x*10+old_x)*(PTYPE_MAX+1 - PTYPE_PIECE_MIN) + ptype;
}
};
class RelativeKingY : public Feature
{
int y, old_y;
Ptype ptype;
bool attack;
public:
static const std::string name(int y, int old_y, bool /*attack*/, Ptype ptype);
RelativeKingY(int iy, int ioy, bool a, Ptype p)
: Feature(name(iy,ioy,a,p)), y(iy), old_y(ioy), ptype(p), attack(a) {}
// [-8,8]
static int makeY(bool attack, const NumEffectState& state, Move move)
{
const Square king = state.kingSquare(attack ? alt(move.player()) : move.player());
int diff = move.to().y() - king.y();
if (move.player() == WHITE)
diff = -diff;
return diff;
}
// [-8,9]
static int makeOldY(bool attack, const NumEffectState& state, Move move)
{
if (move.isDrop())
return 9;
const Square king = state.kingSquare(attack ? alt(move.player()) : move.player());
int diff = move.from().y() - king.y();
if (move.player() == WHITE)
diff = -diff;
return diff;
}
bool match(const NumEffectState& state, Move move, const RatingEnv&) const
{
if (ptype != move.ptype())
return false;
return makeY(attack, state, move) == y && makeOldY(attack, state, move) == old_y;
}
static int index(bool attack, const NumEffectState& state, Move move)
{
const int y = makeY(attack, state, move) + 8;
const int old_y = makeOldY(attack, state, move) + 8;
const int ptype = move.ptype() - PTYPE_PIECE_MIN;
return (y*18+old_y)*(PTYPE_MAX+1 - PTYPE_PIECE_MIN) + ptype;
}
};
class SquareX : public Feature, DropPtype
{
int x;
static const std::string name(int x);
public:
SquareX(int ix, Ptype ptype, bool drop)
: Feature(name(ix)+DropPtype::name(ptype, drop)), DropPtype(ptype, drop), x(ix) {}
static int makeX(Move move)
{
int mx = move.to().x();
if (mx > 5)
mx = 10-mx;
return mx;
}
bool match(const NumEffectState& , Move move, const RatingEnv&) const
{
if (! DropPtype::match(move))
return false;
return x == makeX(move);
}
};
class SquareY : public Feature, DropPtype
{
int y;
static const std::string name(int y);
public:
SquareY(int iy, Ptype ptype, bool drop)
: Feature(name(iy)+DropPtype::name(ptype, drop)), DropPtype(ptype, drop), y(iy) {}
static int makeY(Move move)
{
int my = move.to().y();
if (move.player() == WHITE)
my = 10 - my;
return my;
}
bool match(const NumEffectState&, Move move, const RatingEnv&) const
{
if (! DropPtype::match(move))
return false;
return y == makeY(move);
}
};
}
}
#endif /* FEATURE_POSITION_H */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
basic_type.h
osl::Move
圧縮していない moveの表現 .
Definition basic_type.h:1052
osl::Move::ptype
Ptype ptype() const
Definition basic_type.h:1155
osl::Move::player
Player player() const
Definition basic_type.h:1195
osl::Move::isDrop
bool isDrop() const
Definition basic_type.h:1150
osl::Move::to
const Square to() const
Definition basic_type.h:1132
osl::Move::from
const Square from() const
Definition basic_type.h:1125
osl::NumEffectState
利きを持つ局面
Definition numEffectState.h:34
osl::SimpleState::kingSquare
Square kingSquare() const
Definition simpleState.h:94
osl::Square
Definition basic_type.h:532
osl::Square::y
int y() const
将棋としてのY座標を返す.
Definition basic_type.h:567
osl::Square::x
int x() const
将棋としてのX座標を返す.
Definition basic_type.h:563
osl::rating::Feature::Feature
Feature(const std::string &name)
Definition rating/feature.h:18
osl::rating::Feature::name
const std::string & name() const
Definition rating/feature.h:24
osl::rating::RatingEnv
Definition ratingEnv.h:16
osl::rating::RelativeKingX::ptype
Ptype ptype
Definition square.h:46
osl::rating::RelativeKingX::x
int x
Definition square.h:45
osl::rating::RelativeKingX::match
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
Definition square.h:66
osl::rating::RelativeKingX::name
static const std::string name(int x, int old_x, bool, Ptype)
Definition square.cc:8
osl::rating::RelativeKingX::attack
bool attack
Definition square.h:47
osl::rating::RelativeKingX::RelativeKingX
RelativeKingX(int ix, int iox, bool a, Ptype p)
Definition square.h:50
osl::rating::RelativeKingX::makeOldX
static int makeOldX(bool attack, const NumEffectState &state, Move move)
Definition square.h:59
osl::rating::RelativeKingX::index
static int index(bool attack, const NumEffectState &state, Move move)
Definition square.h:72
osl::rating::RelativeKingX::makeX
static int makeX(bool attack, const NumEffectState &state, Move move)
Definition square.h:53
osl::rating::RelativeKingX::old_x
int old_x
Definition square.h:45
osl::rating::RelativeKingY::makeOldY
static int makeOldY(bool attack, const NumEffectState &state, Move move)
Definition square.h:99
osl::rating::RelativeKingY::attack
bool attack
Definition square.h:84
osl::rating::RelativeKingY::old_y
int old_y
Definition square.h:82
osl::rating::RelativeKingY::ptype
Ptype ptype
Definition square.h:83
osl::rating::RelativeKingY::makeY
static int makeY(bool attack, const NumEffectState &state, Move move)
Definition square.h:90
osl::rating::RelativeKingY::match
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
Definition square.h:109
osl::rating::RelativeKingY::RelativeKingY
RelativeKingY(int iy, int ioy, bool a, Ptype p)
Definition square.h:87
osl::rating::RelativeKingY::y
int y
Definition square.h:82
osl::rating::RelativeKingY::index
static int index(bool attack, const NumEffectState &state, Move move)
Definition square.h:115
osl::rating::RelativeKingY::name
static const std::string name(int y, int old_y, bool, Ptype ptype)
Definition square.cc:21
osl::rating::SquareX::match
bool match(const NumEffectState &, Move move, const RatingEnv &) const
Definition square.h:138
osl::rating::SquareX::x
int x
Definition square.h:126
osl::rating::SquareX::makeX
static int makeX(Move move)
Definition square.h:131
osl::rating::SquareX::SquareX
SquareX(int ix, Ptype ptype, bool drop)
Definition square.h:129
osl::rating::SquareX::name
static const std::string name(int x)
Definition square.cc:33
osl::rating::SquareY::makeY
static int makeY(Move move)
Definition square.h:153
osl::rating::SquareY::SquareY
SquareY(int iy, Ptype ptype, bool drop)
Definition square.h:151
osl::rating::SquareY::y
int y
Definition square.h:148
osl::rating::SquareY::name
static const std::string name(int y)
Definition square.cc:39
osl::rating::SquareY::match
bool match(const NumEffectState &, Move move, const RatingEnv &) const
Definition square.h:160
osl::rating
Definition construct.h:18
osl
Definition additionalEffect.h:6
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
osl::PTYPE_PIECE_MIN
@ PTYPE_PIECE_MIN
Definition basic_type.h:104
osl::PTYPE_MAX
@ PTYPE_MAX
Definition basic_type.h:105
osl::KING
@ KING
Definition basic_type.h:93
osl::PTYPE_BASIC_MIN
@ PTYPE_BASIC_MIN
Definition basic_type.h:103
osl::Ptype_Table
const PtypeTable Ptype_Table
Definition tables.cc:97
osl::WHITE
@ WHITE
Definition basic_type.h:10
osl::isBasic
bool isBasic(Ptype ptype)
ptypeが基本型(promoteしていない)かのチェック
Definition basic_type.h:128
osl::alt
constexpr Player alt(Player player)
Definition basic_type.h:13
ptypeTable.h
feature.h
osl::rating::DropPtype::drop
bool drop
Definition square.h:18
osl::rating::DropPtype::match
bool match(Move m) const
Definition square.h:22
osl::rating::DropPtype::ptype
Ptype ptype
Definition square.h:17
osl::rating::DropPtype::name
static std::string name(Ptype ptype, bool drop)
Definition square.h:26
osl::rating::DropPtype::UNIT
@ UNIT
Definition square.h:30
osl::rating::DropPtype::index
static int index(Move move)
Definition square.h:31
osl::rating::DropPtype::DropPtype
DropPtype(Ptype p, bool d)
Definition square.h:19