std/osl/rating/feature/king8.h
Go to the documentation of this file.
/* king8.h
*/
#ifndef RATING_KING8_H
#define RATING_KING8_H
#include "osl/rating/feature.h"
#include "osl/rating/feature/countEffect2.h"
#include "osl/effect_util/neighboring8Direct.h"
namespace osl
{
namespace rating
{
class AttackKing8 : public Feature, CountEffect2
{
Ptype self, target;
bool same;
static const std::string name(Ptype self, Ptype target, bool same);
public:
AttackKing8(Ptype s, Ptype t, bool ss, int attack, int defense)
: Feature(name(s, t, ss)+CountEffect2::name(attack, defense)), CountEffect2(attack, defense),
self(s), target(t), same(ss)
{
}
bool match(const NumEffectState& state, Move move, const RatingEnv& env) const
{
if (move.ptype() != self)
return false;
const Square position
= Neighboring8Direct::findNearest(state, move.ptypeO(), move.to(), state.kingSquare(alt(state.turn())));
if (position.isPieceStand() || position == move.from())
return false;
if (! move.isDrop() && state.hasEffectByPiece(state.pieceOnBoard(move.from()), position))
return false;
const Piece p = state.pieceAt(position);
if (p.ptype() != target)
return false;
if (! CountEffect2::match(state, position, env))
return false;
if (!isPiece(target))
return true;
return same == (p.owner() == move.player());
}
};
class DefenseKing8 : public Feature
{
Ptype self;
bool drop;
int danger;
public:
static const std::string name(Ptype self, bool drop, int danger);
DefenseKing8(Ptype s, bool d, int dan)
: Feature(name(s,d,dan)), self(s), drop(d), danger(dan)
{
}
static int count(const NumEffectState& state)
{
const Player attack = alt(state.turn());
const Square king = state.kingSquare(alt(attack));
int count = 0;
for (int dx=-1; dx<=1; ++dx) {
for (int dy=-1; dy<=1; ++dy) {
if (dx == 0 && dy ==0)
continue;
Square p = king + Offset(dx,dy);
if (! state.pieceAt(p).isEdge()
&& state.hasEffectAt(attack, p))
++count;
}
}
if (king.x() == 1 || king.x() == 9)
++count;
return std::min(3, count);
}
static bool blocking(const NumEffectState& state, Square king, Square to)
{
const Player attacker = alt(state.turn());
Piece attack = state.findAttackAt<BISHOP>(attacker, to);
if (attack.isPiece()
&& Neighboring8Direct::hasEffect(state, newPtypeO(attacker, BISHOP), attack.square(), king))
return true;
attack = state.findAttackAt<ROOK>(attacker, to);
if (attack.isPiece()
&& Neighboring8Direct::hasEffect(state, newPtypeO(attacker, ROOK), attack.square(), king))
return true;
attack = state.findAttackAt<LANCE>(attacker, to);
return attack.isPiece() && attack.ptype() == LANCE
&& Neighboring8Direct::hasEffect(state, newPtypeO(attacker, LANCE), attack.square(), king);
}
static bool matchDrop(const NumEffectState& state, Move move)
{
if (! move.isDrop())
return false;
const Square king = state.kingSquare(state.turn());
if (king.isNeighboring8(move.to())
|| Neighboring8Direct::hasEffect(state, move.ptypeO(), move.to(), king))
return true;
return blocking(state, king, move.to());
}
static bool matchMove(const NumEffectState& state, Move move)
{
if (move.isDrop())
return false;
if (move.ptype() == KING)
return true;
const Square king = state.kingSquare(state.turn());
if (king.isNeighboring8(move.to())
|| king.isNeighboring8(move.from())
|| Neighboring8Direct::hasEffect(state, move.ptypeO(), move.to(), king))
return true;
return blocking(state, king, move.to());
}
bool match(const NumEffectState& state, Move move, const RatingEnv&) const
{
if (move.ptype() != self)
return false;
if (count(state) != danger)
return false;
if (drop)
return matchDrop(state, move);
return matchMove(state, move);
}
};
}
}
#endif /* RATING_KING8_H */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
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::to
const Square to() const
Definition basic_type.h:1132
osl::Move::from
const Square from() const
Definition basic_type.h:1125
osl::Neighboring8Direct::findNearest
static Square findNearest(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
Definition neighboring8Direct.h:124
osl::Neighboring8Direct::hasEffect
static bool hasEffect(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
ptypeo の駒がfromからtargetの8近傍に直接の利きを持つか
Definition neighboring8Direct.h:108
osl::NumEffectState
利きを持つ局面
Definition numEffectState.h:34
osl::NumEffectState::hasEffectByPiece
bool hasEffectByPiece(Piece attack, Square target) const
駒attack が target に利きを持つか (旧hasEffectToと統合)
Definition numEffectState.h:450
osl::NumEffectState::hasEffectAt
bool hasEffectAt(Square target) const
対象とするマスにあるプレイヤーの利きがあるかどうか.
Definition numEffectState.h:324
osl::NumEffectState::findAttackAt
const Piece findAttackAt(Player attack, Square target) const
return a piece s.t.
Definition numEffectState.h:503
osl::Offset
座標の差分
Definition basic_type.h:430
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::isEdge
bool isEdge() const
Definition basic_type.h:919
osl::Piece::owner
Player owner() const
Definition basic_type.h:963
osl::Piece::isPiece
bool isPiece() const
Definition basic_type.h:953
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::kingSquare
Square kingSquare() const
Definition simpleState.h:94
osl::SimpleState::pieceAt
const Piece pieceAt(Square sq) const
Definition simpleState.h:167
osl::Square
Definition basic_type.h:532
osl::Square::isPieceStand
bool isPieceStand() const
Definition basic_type.h:576
osl::Square::isNeighboring8
bool isNeighboring8(Square to) const
Definition basic_type.cc:202
osl::Square::x
int x() const
将棋としてのX座標を返す.
Definition basic_type.h:563
osl::rating::AttackKing8::target
Ptype target
Definition std/osl/rating/feature/king8.h:16
osl::rating::AttackKing8::match
bool match(const NumEffectState &state, Move move, const RatingEnv &env) const
Definition std/osl/rating/feature/king8.h:25
osl::rating::AttackKing8::self
Ptype self
Definition std/osl/rating/feature/king8.h:16
osl::rating::AttackKing8::AttackKing8
AttackKing8(Ptype s, Ptype t, bool ss, int attack, int defense)
Definition std/osl/rating/feature/king8.h:20
osl::rating::AttackKing8::same
bool same
Definition std/osl/rating/feature/king8.h:17
osl::rating::AttackKing8::name
static const std::string name(Ptype self, Ptype target, bool same)
Definition king8_.cc:5
osl::rating::DefenseKing8::match
bool match(const NumEffectState &state, Move move, const RatingEnv &) const
Definition std/osl/rating/feature/king8.h:114
osl::rating::DefenseKing8::matchDrop
static bool matchDrop(const NumEffectState &state, Move move)
Definition std/osl/rating/feature/king8.h:91
osl::rating::DefenseKing8::name
static const std::string name(Ptype self, bool drop, int danger)
Definition king8_.cc:11
osl::rating::DefenseKing8::blocking
static bool blocking(const NumEffectState &state, Square king, Square to)
Definition std/osl/rating/feature/king8.h:76
osl::rating::DefenseKing8::DefenseKing8
DefenseKing8(Ptype s, bool d, int dan)
Definition std/osl/rating/feature/king8.h:53
osl::rating::DefenseKing8::matchMove
static bool matchMove(const NumEffectState &state, Move move)
Definition std/osl/rating/feature/king8.h:101
osl::rating::DefenseKing8::drop
bool drop
Definition std/osl/rating/feature/king8.h:49
osl::rating::DefenseKing8::danger
int danger
Definition std/osl/rating/feature/king8.h:50
osl::rating::DefenseKing8::self
Ptype self
Definition std/osl/rating/feature/king8.h:48
osl::rating::DefenseKing8::count
static int count(const NumEffectState &state)
Definition std/osl/rating/feature/king8.h:57
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
countEffect2.h
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::KING
@ KING
Definition basic_type.h:93
osl::LANCE
@ LANCE
Definition basic_type.h:96
osl::Player
Player
Definition basic_type.h:8
osl::isPiece
constexpr bool isPiece(Ptype ptype)
ptypeが空白やEDGEでないかのチェック
Definition basic_type.h:120
osl::alt
constexpr Player alt(Player player)
Definition basic_type.h:13
osl::newPtypeO
PtypeO newPtypeO(Player player, Ptype ptype)
Definition basic_type.h:211
neighboring8Direct.h
feature.h
osl::rating::CountEffect2::CountEffect2
CountEffect2(int a, int d)
Definition countEffect2.h:18
osl::rating::CountEffect2::match
bool match(const NumEffectState &state, Square position, const RatingEnv &env) const
Definition countEffect2.h:37
osl::rating::CountEffect2::attack
int attack
Definition countEffect2.h:17
osl::rating::CountEffect2::defense
int defense
Definition countEffect2.h:17