feature.cc
Go to the documentation of this file.
/* feature.cc
*/
#include "osl/rating/feature.h"
#include "osl/simpleState.tcc"
#include <sstream>
osl::rating::Feature::~Feature()
{
}
const osl::CArray<const char*,4> osl::rating::Check::check_property = {{ "Di", "DO", "OC", "Bo" }};
osl::rating::Check::Check(int p) : Feature(check_property[p]), property(p)
{
}
bool osl::rating::Check::match(const NumEffectState& state, Move move, const RatingEnv&) const
{
if (property == 0 || property == 1) {
if (! (state.isDirectCheck(move) && ! state.isOpenCheck(move)))
return false;
return property == openLong(state, move);
}
if (property == 2)
return ! state.isDirectCheck(move) && state.isOpenCheck(move);
return state.isDirectCheck(move) && state.isOpenCheck(move);
}
const std::string osl::rating::
Block::name(int self, int opponent)
{
std::ostringstream os;
os << "B" << self << opponent;
return os.str();
}
const std::string osl::rating::Open::name(int property)
{
std::ostringstream os;
os << "Open" << property / 4 << property % 4;
return os.str();
}
struct osl::rating::ImmediateAddSupport::Test
{
bool *result;
const NumEffectState& state;
Move move;
Test(bool *r, const NumEffectState& s, Move m)
: result(r), state(s), move(m)
{
}
template <Player P>
void doAction(Piece /*piece*/, Square last_attack)
{
const Piece attacked = state.pieceAt(last_attack);
if (attacked.isOnBoardByOwner(state.turn())
&& state.hasEffectIf(move.ptypeO(), move.to(), last_attack))
*result = true;
}
};
osl::rating::
ImmediateAddSupport::ImmediateAddSupport(Ptype s, Ptype a)
: Feature(std::string(Ptype_Table.getCsaName(s))+":"+Ptype_Table.getCsaName(a)),
self(s), attack(a)
{
}
bool osl::rating::
ImmediateAddSupport::match(const NumEffectState& state, Move move, const RatingEnv& env) const
{
if (move.ptype() != self)
return false;
const Move last_move=env.history.lastMove();
if (! last_move.isNormal())
return false;
if (last_move.ptype() != attack)
return false;
const Square last_to = last_move.to();
if (last_to==move.to())
return false; // TakeBack は除く
bool result = false;
Test action(&result, state, move);
state.forEachEffectOfPiece(state.pieceOnBoard(last_to), action);
return result;
}
int osl::rating::
ImmediateAddSupport::index(const NumEffectState& state, Move move, const RatingEnv& env)
{
const Move last_move=env.history.lastMove();
if (! last_move.isNormal())
return -1;
const Square last_to = last_move.to();
if (last_to==move.to())
return -1; // TakeBack は除く
bool result = false;
const Piece last_piece = state.pieceOnBoard(last_to);
// BoardMask state.changedEffects(alt(Turn))?
if (! Ptype_Table.hasLongMove(last_piece.ptype()))
{
Test action(&result, state, move);
state.forEachEffectOfPiece(last_piece, action);
}
else
{
const Player Turn = state.turn();
PieceMask pieces = state.piecesOnBoard(Turn) & state.effectedMask(alt(Turn));
mask_t m = pieces.getMask(0);
while (m.any()) {
const Piece p = state.pieceOf(m.takeOneBit());
if (state.hasEffectByPiece(last_piece, p.square())
&& state.hasEffectIf(move.ptypeO(), move.to(), p.square())) {
result = true;
break;
}
}
#if OSL_WORDSIZE == 32
if (! result) {
m = pieces.getMask(1);
while (m.any()) {
const Piece p = state.pieceOf(m.takeOneBit()+32);
if (state.hasEffectByPiece(last_piece, p.square())
&& state.hasEffectIf(move.ptypeO(), move.to(), p.square())) {
result = true;
break;
}
}
}
#endif
}
if (! result)
return -1;
return (move.ptype() - PTYPE_PIECE_MIN) * (PTYPE_MAX+1 - PTYPE_PIECE_MIN)
+ last_move.ptype() - PTYPE_PIECE_MIN;
}
const std::string osl::rating::Chase::name(Ptype self, Ptype target, bool drop, OpponentType opponent_type)
{
return std::string(Ptype_Table.getCsaName(self))
+(drop ? "d" : "m")+">"+Ptype_Table.getCsaName(target)
+(opponent_type == CAPTURE ? "c" : (opponent_type == DROP ? "d" : "e"));
}
/* ------------------------------------------------------------------------- */
// ;;; 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::isNormal
bool isNormal() const
INVALID でも PASS でもない.
Definition basic_type.h:1088
osl::Move::to
const Square to() const
Definition basic_type.h:1132
osl::NumEffectState
利きを持つ局面
Definition numEffectState.h:34
osl::NumEffectState::isOpenCheck
bool isOpenCheck(Move move) const
Definition numEffectState.cc:1074
osl::NumEffectState::effectedMask
const PieceMask effectedMask(Player pl) const
pl からの利きが(1つ以上)ある駒一覧
Definition numEffectState.h:130
osl::NumEffectState::hasEffectByPiece
bool hasEffectByPiece(Piece attack, Square target) const
駒attack が target に利きを持つか (旧hasEffectToと統合)
Definition numEffectState.h:450
osl::NumEffectState::isDirectCheck
bool isDirectCheck(Move move) const
Definition numEffectState.cc:1068
osl::NumEffectState::hasEffectIf
bool hasEffectIf(PtypeO ptypeo, Square attacker, Square target) const
attackerにptypeoの駒がいると仮定した場合にtargetに利きがあるかどうか を stateをupdateしないで確かめる.
Definition numEffectState.h:465
osl::NumEffectState::forEachEffectOfPiece
void forEachEffectOfPiece(Square pieceSquare, Action &action) const
pieceSquareにある駒によって利きを受けるすべてのsquare (空白含む)について actionを実行する
Definition numEffectState.h:1225
osl::NumEffectState::piecesOnBoard
const PieceMask & piecesOnBoard(Player p) const
Definition numEffectState.h:63
osl::PieceMask
駒番号のビットセット.
Definition pieceMask.h:21
osl::PieceMask::getMask
const mask_t getMask(int num) const
Definition pieceMask.h:59
osl::Piece
駒.
Definition basic_type.h:788
osl::Piece::ptype
Ptype ptype() const
Definition basic_type.h:821
osl::Piece::square
const Square square() const
Definition basic_type.h:832
osl::Piece::isOnBoardByOwner
bool isOnBoardByOwner() const
piece がプレイヤーPの持ち物でかつボード上にある駒の場合は true.
Definition basic_type.h:852
osl::SimpleState::pieceOnBoard
const Piece pieceOnBoard(Square sq) const
Definition simpleState.h:170
osl::SimpleState::turn
Player turn() const
Definition simpleState.h:220
osl::SimpleState::pieceOf
const Piece pieceOf(int num) const
Definition simpleState.h:76
osl::Square
Definition basic_type.h:532
osl::container::MoveStack::lastMove
const Move lastMove(size_t last=1) const
Definition moveStack.h:28
osl::misc::GeneralMask::any
bool any() const
Definition mask.h:298
osl::misc::GeneralMask::takeOneBit
int takeOneBit()
non-zeroのmaskのsetされているビットをLSBから探し，その番号を返す 副作用としてmaskの対応するビットをクリアする
Definition mask.h:265
osl::rating::Block::opponent
int opponent
Definition rating/feature.h:87
osl::rating::Block::self
int self
Definition rating/feature.h:87
osl::rating::Chase::OpponentType
OpponentType
Definition rating/feature.h:142
osl::rating::Chase::DROP
@ DROP
Definition rating/feature.h:142
osl::rating::Chase::CAPTURE
@ CAPTURE
Definition rating/feature.h:142
osl::rating::Chase::drop
bool drop
Definition rating/feature.h:145
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::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::~Feature
virtual ~Feature()
Definition feature.cc:7
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::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::property
int property
Definition rating/feature.h:121
osl::rating::RatingEnv
Definition ratingEnv.h:16
osl::rating::RatingEnv::history
MoveStack history
Definition ratingEnv.h:18
osl::misc::mask_t
GeneralMask< mask_int_t > mask_t
Definition mask.h:351
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::Ptype_Table
const PtypeTable Ptype_Table
Definition tables.cc:97
osl::Player
Player
Definition basic_type.h:8
osl::alt
constexpr Player alt(Player player)
Definition basic_type.h:13
std
Definition basic_type.h:1353
feature.h
simpleState.tcc
osl::rating::ImmediateAddSupport::Test
Definition feature.cc:44
osl::rating::ImmediateAddSupport::Test::result
bool * result
Definition feature.cc:45
osl::rating::ImmediateAddSupport::Test::state
const NumEffectState & state
Definition feature.cc:46
osl::rating::ImmediateAddSupport::Test::doAction
void doAction(Piece, Square last_attack)
Definition feature.cc:53
osl::rating::ImmediateAddSupport::Test::Test
Test(bool *r, const NumEffectState &s, Move m)
Definition feature.cc:48
osl::rating::ImmediateAddSupport::Test::move
Move move
Definition feature.cc:47