rating/feature.h
Go to the documentation of this file.
/* feature.h
*/
#ifndef _FEATURE_H
#define _FEATURE_H
#include "osl/rating/ratingEnv.h"
#include "osl/numEffectState.h"
#include <string>
namespace osl
{
namespace rating
{
class Feature
{
std::string my_name;
public:
Feature(const std::string& name) : my_name(name)
{
}
virtual ~Feature();
virtual bool match(const NumEffectState& state, Move, const RatingEnv&) const =0;
virtual bool effectiveInCheck() const { return false; }
const std::string& name() const { return my_name; }
};
class TakeBack : public Feature
{
public:
TakeBack() : Feature("TakeBack")
{
}
bool match(const NumEffectState&, Move move, const RatingEnv& env) const
{
return env.history.hasLastMove() && move.to() == env.history.lastMove().to();
}
virtual bool effectiveInCheck() const { return true; }
};
class TakeBack2 : public Feature
{
public:
TakeBack2() : Feature("TakeBack2")
{
}
bool match(const NumEffectState&, Move move, const RatingEnv& env) const
{
return env.history.hasLastMove(2)
&& move.to() == env.history.lastMove().to()
&& move.to() == env.history.lastMove(2).to();
}
bool effectiveInCheck() const { return true; }
};
class Check : public Feature
{
int property;
public:
Check(int p);
static bool openLong(const NumEffectState& state, Move move)
{
if (move.isDrop())
return false;
return state.hasEffectByPtype<LANCE>(move.player(), move.from())
|| state.hasEffectByPtype<BISHOP>(move.player(), move.from())
|| state.hasEffectByPtype<ROOK>(move.player(), move.from());
}
bool match(const NumEffectState& state, Move move, const RatingEnv&) const;
static const CArray<const char*,4> check_property;
bool effectiveInCheck() const { return true; }
};
class SendOff : public Feature
{
bool capture;
public:
SendOff(bool c) : Feature("SO"), capture(c) {}
bool match(const NumEffectState&, Move move, const RatingEnv& env) const
{
return env.sendoffs.isMember(move.to()) && (move.capturePtype() !=PTYPE_EMPTY) == capture;
}
};
class Block : public Feature
{
int self, opponent;
public:
static const std::string name(int self, int opponent);
Block(int s, int o) : Feature(name(s, o)), self(s), opponent(o) {}
static int count(const NumEffectState& state, Square position, Player player)
{
return (state.findAttackAt<LANCE>(player, position).ptype() == LANCE)
+ state.hasEffectByPtype<BISHOP>(player, position)
+ state.hasEffectByPtype<ROOK>(player, position);
}
bool match(const NumEffectState& state, Move move, const RatingEnv&) const
{
return count(state, move.to(), state.turn()) == self
&& count(state, move.to(), alt(state.turn())) == opponent;
}
bool effectiveInCheck() const { return true; }
};
// { none, tate, naname, both }
struct CountOpen
{
static int index(const NumEffectState& state, Player player, Square from)
{
if (from.isPieceStand())
return -1;
const bool vertical = state.hasEffectByPtype<LANCE>(player, from)
|| state.hasEffectByPtype<ROOK>(player, from);
const bool diagonal = state.hasEffectByPtype<BISHOP>(player, from);
return diagonal*2+vertical;
}
};
// { none, tate, naname, both } * { none, tate, naname, both }
class Open : public Feature
{
int property;
public:
Open(int p) : Feature(name(p)), property(p) {}
static int index(const NumEffectState& state, Move move)
{
if (move.isDrop())
return -1;
return CountOpen::index(state, move.player(), move.from())*4
+ CountOpen::index(state, alt(move.player()), move.from());
}
bool match(const NumEffectState& state, Move move, const RatingEnv&) const
{
return index(state, move) == property;
}
static const std::string name(int property);
bool effectiveInCheck() const { return true; }
};
class Chase : public Feature
{
public:
enum OpponentType { CAPTURE, DROP, ESCAPE, OTHER, };
private:
Ptype self, target;
bool drop;
OpponentType opponent_type;
public:
Chase(Ptype s, Ptype t, bool d, OpponentType o)
: Feature(name(s,t,d,o)), self(s), target(t), drop(d), opponent_type(o) {}
bool match(const NumEffectState& state, Move move, const RatingEnv& env) const
{
const Move last_move = env.history.lastMove();
if (! last_move.isNormal())
return false;
if (! (move.ptype() == self && last_move.ptype() == target
&& drop == move.isDrop()))
return false;
switch (opponent_type) {
case CAPTURE:
if (last_move.capturePtype() == PTYPE_EMPTY)
return false;
break;
case DROP:
if (! last_move.isDrop())
return false;
break;
case ESCAPE:
if (last_move.isDrop() || last_move.capturePtype() != PTYPE_EMPTY
|| ! state.hasEffectAt(state.turn(), last_move.from()))
return false;
break;
case OTHER:
if (last_move.isDrop() || last_move.capturePtype() != PTYPE_EMPTY)
return false;
break;
}
return state.hasEffectIf
(move.ptypeO(), move.to(), last_move.to());
}
static const std::string name(Ptype, Ptype, bool, OpponentType);
};
class ImmediateAddSupport : public Feature
{
struct Test;
Ptype self, attack;
public:
ImmediateAddSupport(Ptype self, Ptype attack);
bool match(const NumEffectState& state, Move move, const RatingEnv& env) const;
bool effectiveInCheck() const { return true; }
static int index(const NumEffectState& state, Move move, const RatingEnv& env);
};
class RookDefense : public Feature
{
public:
RookDefense() : Feature("RookDefense")
{
}
bool match(const NumEffectState& state, Move move, const RatingEnv& env) const
{
if (move.isDrop() || env.progress.value() > 8)
return false;
Piece rook1 = state.pieceOf(PtypeTraits<ROOK>::indexMin);
Piece rook2 = state.pieceOf(PtypeTraits<ROOK>::indexMin + 1);
if (move.from() == rook2.square())
std::swap(rook1, rook2);
if (move.from() != rook1.square()
|| rook2.square().isPieceStand()
|| rook2.owner() == move.player()
|| rook2.square().x() != move.to().x())
return false;
return (move.to().y() - rook2.square().y())*sign(move.player()) > 0;
}
};
class BadLance : public Feature
{
bool has_effect;
public:
explicit BadLance(bool h) : Feature(h ? "StrongBadLance" : "WeakBadLance"), has_effect(h)
{
}
static bool basicMatch(const NumEffectState& state, Move move, Square front)
{
if (! (move.isDrop() && move.ptype() == LANCE))
return false;
return state.pieceOnBoard(front).isEmpty()
&& state.hasPieceOnStand<PAWN>(alt(move.player()))
&& !state.isPawnMaskSet(alt(move.player()), front.x());
}
bool match(const NumEffectState& state, Move move, const RatingEnv&) const
{
const Square front = Board_Table.nextSquare(move.player(), move.to(), U);
return basicMatch(state, move, front)
&& ((!has_effect) ^ state.hasEffectAt(alt(move.player()), front));
}
};
class PawnAttack : public Feature
{
public:
PawnAttack() : Feature("PA")
{
}
bool match(const NumEffectState&, Move move, const RatingEnv& env) const
{
if (! (move.isDrop() && move.ptype() == PAWN))
return false;
const Move last_move = env.history.lastMove();
if (! last_move.isNormal() || last_move.capturePtype() == PTYPE_EMPTY)
return false;
return last_move.capturePtype() == PAWN && last_move.to().x() == move.to().x();
}
};
}
}
#endif /* _FEATURE_H */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
osl::CArray
Definition container.h:20
osl::Move
圧縮していない moveの表現 .
Definition basic_type.h:1052
osl::Move::ptypeO
PtypeO ptypeO() const
移動後のPtype, i.e., 成る手だった場合成った後
Definition basic_type.h:1162
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::capturePtype
Ptype capturePtype() const
Definition basic_type.h:1180
osl::Move::isNormal
bool isNormal() const
INVALID でも PASS でもない.
Definition basic_type.h:1088
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::NumEffectState::hasEffectAt
bool hasEffectAt(Square target) const
対象とするマスにあるプレイヤーの利きがあるかどうか.
Definition numEffectState.h:324
osl::NumEffectState::hasEffectIf
bool hasEffectIf(PtypeO ptypeo, Square attacker, Square target) const
attackerにptypeoの駒がいると仮定した場合にtargetに利きがあるかどうか を stateをupdateしないで確かめる.
Definition numEffectState.h:465
osl::NumEffectState::findAttackAt
const Piece findAttackAt(Player attack, Square target) const
return a piece s.t.
Definition numEffectState.h:503
osl::NumEffectState::hasEffectByPtype
bool hasEffectByPtype(Player attack, Square target) const
target に ptype の利きがあるか?
Definition numEffectState.h:355
osl::Piece
駒.
Definition basic_type.h:788
osl::Piece::square
const Square square() const
Definition basic_type.h:832
osl::Piece::isEmpty
bool isEmpty() const
Definition basic_type.h:913
osl::Piece::owner
Player owner() const
Definition basic_type.h:963
osl::SimpleState::pieceOnBoard
const Piece pieceOnBoard(Square sq) const
Definition simpleState.h:170
osl::SimpleState::hasPieceOnStand
bool hasPieceOnStand(Player player, Ptype ptype) const
Definition simpleState.h:191
osl::SimpleState::turn
Player turn() const
Definition simpleState.h:220
osl::SimpleState::pieceOf
const Piece pieceOf(int num) const
Definition simpleState.h:76
osl::SimpleState::isPawnMaskSet
bool isPawnMaskSet(Player player, int x) const
Definition simpleState.h:146
osl::Square
Definition basic_type.h:532
osl::Square::isPieceStand
bool isPieceStand() const
Definition basic_type.h:576
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::container::MoveStack::hasLastMove
bool hasLastMove(size_t last=1) const
Definition moveStack.h:27
osl::container::MoveStack::lastMove
const Move lastMove(size_t last=1) const
Definition moveStack.h:28
osl::progress::ProgressN::value
int value() const
Definition progress.h:22
osl::rating::BadLance::BadLance
BadLance(bool h)
Definition rating/feature.h:221
osl::rating::BadLance::match
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
Definition rating/feature.h:232
osl::rating::BadLance::has_effect
bool has_effect
Definition rating/feature.h:219
osl::rating::BadLance::basicMatch
static bool basicMatch(const NumEffectState &state, Move move, Square front)
Definition rating/feature.h:224
osl::rating::Block::Block
Block(int s, int o)
Definition rating/feature.h:90
osl::rating::Block::effectiveInCheck
bool effectiveInCheck() const
Definition rating/feature.h:102
osl::rating::Block::opponent
int opponent
Definition rating/feature.h:87
osl::rating::Block::name
static const std::string name(int self, int opponent)
Definition feature.cc:29
osl::rating::Block::count
static int count(const NumEffectState &state, Square position, Player player)
Definition rating/feature.h:91
osl::rating::Block::match
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
Definition rating/feature.h:97
osl::rating::Block::self
int self
Definition rating/feature.h:87
osl::rating::Chase::name
static const std::string name(Ptype, Ptype, bool, OpponentType)
Definition feature.cc:138
osl::rating::Chase::OpponentType
OpponentType
Definition rating/feature.h:142
osl::rating::Chase::DROP
@ DROP
Definition rating/feature.h:142
osl::rating::Chase::OTHER
@ OTHER
Definition rating/feature.h:142
osl::rating::Chase::CAPTURE
@ CAPTURE
Definition rating/feature.h:142
osl::rating::Chase::ESCAPE
@ ESCAPE
Definition rating/feature.h:142
osl::rating::Chase::drop
bool drop
Definition rating/feature.h:145
osl::rating::Chase::Chase
Chase(Ptype s, Ptype t, bool d, OpponentType o)
Definition rating/feature.h:148
osl::rating::Chase::match
bool match(const NumEffectState &state, Move move, const RatingEnv &env) const
Definition rating/feature.h:150
osl::rating::Chase::self
Ptype self
Definition rating/feature.h:144
osl::rating::Chase::target
Ptype target
Definition rating/feature.h:144
osl::rating::Chase::opponent_type
OpponentType opponent_type
Definition rating/feature.h:146
osl::rating::Check::Check
Check(int p)
Definition feature.cc:12
osl::rating::Check::openLong
static bool openLong(const NumEffectState &state, Move move)
Definition rating/feature.h:61
osl::rating::Check::match
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
Definition feature.cc:16
osl::rating::Check::effectiveInCheck
bool effectiveInCheck() const
Definition rating/feature.h:71
osl::rating::Check::check_property
static const CArray< const char *, 4 > check_property
Definition rating/feature.h:11
osl::rating::Check::property
int property
Definition rating/feature.h:58
osl::rating::Feature::my_name
std::string my_name
Definition rating/feature.h:16
osl::rating::Feature::~Feature
virtual ~Feature()
Definition feature.cc:7
osl::rating::Feature::Feature
Feature(const std::string &name)
Definition rating/feature.h:18
osl::rating::Feature::effectiveInCheck
virtual bool effectiveInCheck() const
Definition rating/feature.h:23
osl::rating::Feature::name
const std::string & name() const
Definition rating/feature.h:24
osl::rating::Feature::match
virtual bool match(const NumEffectState &state, Move, const RatingEnv &) const =0
osl::rating::ImmediateAddSupport::effectiveInCheck
bool effectiveInCheck() const
Definition rating/feature.h:190
osl::rating::ImmediateAddSupport::ImmediateAddSupport
ImmediateAddSupport(Ptype self, Ptype attack)
Definition feature.cc:63
osl::rating::ImmediateAddSupport::attack
Ptype attack
Definition rating/feature.h:186
osl::rating::ImmediateAddSupport::index
static int index(const NumEffectState &state, Move move, const RatingEnv &env)
Definition feature.cc:89
osl::rating::ImmediateAddSupport::match
bool match(const NumEffectState &state, Move move, const RatingEnv &env) const
Definition feature.cc:70
osl::rating::ImmediateAddSupport::self
Ptype self
Definition rating/feature.h:186
osl::rating::Open::Open
Open(int p)
Definition rating/feature.h:123
osl::rating::Open::match
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
Definition rating/feature.h:131
osl::rating::Open::index
static int index(const NumEffectState &state, Move move)
Definition rating/feature.h:124
osl::rating::Open::name
static const std::string name(int property)
Definition feature.cc:36
osl::rating::Open::effectiveInCheck
bool effectiveInCheck() const
Definition rating/feature.h:136
osl::rating::Open::property
int property
Definition rating/feature.h:121
osl::rating::PawnAttack::PawnAttack
PawnAttack()
Definition rating/feature.h:243
osl::rating::PawnAttack::match
bool match(const NumEffectState &, Move move, const RatingEnv &env) const
Definition rating/feature.h:246
osl::rating::RatingEnv
Definition ratingEnv.h:16
osl::rating::RatingEnv::history
MoveStack history
Definition ratingEnv.h:18
osl::rating::RatingEnv::sendoffs
Square8 sendoffs
Definition ratingEnv.h:19
osl::rating::RatingEnv::progress
Progress16 progress
Definition ratingEnv.h:22
osl::rating::RookDefense::match
bool match(const NumEffectState &state, Move move, const RatingEnv &env) const
Definition rating/feature.h:200
osl::rating::RookDefense::RookDefense
RookDefense()
Definition rating/feature.h:197
osl::rating::SendOff::match
bool match(const NumEffectState &, Move move, const RatingEnv &env) const
Definition rating/feature.h:79
osl::rating::SendOff::capture
bool capture
Definition rating/feature.h:76
osl::rating::SendOff::SendOff
SendOff(bool c)
Definition rating/feature.h:78
osl::rating::TakeBack2::TakeBack2
TakeBack2()
Definition rating/feature.h:43
osl::rating::TakeBack2::match
bool match(const NumEffectState &, Move move, const RatingEnv &env) const
Definition rating/feature.h:46
osl::rating::TakeBack2::effectiveInCheck
bool effectiveInCheck() const
Definition rating/feature.h:52
osl::rating::TakeBack::TakeBack
TakeBack()
Definition rating/feature.h:30
osl::rating::TakeBack::match
bool match(const NumEffectState &, Move move, const RatingEnv &env) const
Definition rating/feature.h:33
osl::rating::TakeBack::effectiveInCheck
virtual bool effectiveInCheck() const
Definition rating/feature.h:37
osl::rating
Definition construct.h:18
osl
Definition additionalEffect.h:6
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
osl::ROOK
@ ROOK
Definition basic_type.h:100
osl::BISHOP
@ BISHOP
Definition basic_type.h:99
osl::PAWN
@ PAWN
Definition basic_type.h:95
osl::PTYPE_EMPTY
@ PTYPE_EMPTY
Definition basic_type.h:85
osl::LANCE
@ LANCE
Definition basic_type.h:96
osl::Board_Table
const BoardTable Board_Table
Definition tables.cc:95
osl::U
@ U
Definition basic_type.h:314
osl::sign
constexpr int sign(Player player)
Definition basic_type.h:23
osl::Player
Player
Definition basic_type.h:8
osl::alt
constexpr Player alt(Player player)
Definition basic_type.h:13
numEffectState.h
ratingEnv.h
osl::PtypeTraits
Definition ptypeTraits.h:12
osl::container::Square8::isMember
bool isMember(Square position) const
Definition square8.h:22
osl::rating::CountOpen
Definition rating/feature.h:107
osl::rating::CountOpen::index
static int index(const NumEffectState &state, Player player, Square from)
Definition rating/feature.h:108
osl::rating::ImmediateAddSupport::Test
Definition feature.cc:44