neighboring8Direct.h
Go to the documentation of this file.
/* neighboring8Direct.h
*/
#ifndef EFFECT_UTIL_NEIGHBORING8DIRECT_H
#define EFFECT_UTIL_NEIGHBORING8DIRECT_H
#include "osl/numEffectState.h"
namespace osl
{
namespace effect_util
{
class Neighboring8Direct
{
class Table
{
struct Entry
{
bool has_unblockable_effect;
Offset nearest;
Entry() : has_unblockable_effect(false), nearest(Offset::ZERO())
{
}
};
CArray2d<Entry,PTYPEO_SIZE,Offset32::SIZE> table;
friend class Neighboring8Direct;
void init(Player);
public:
bool hasEffect(const NumEffectState& state,
PtypeO ptypeo, Square from,
Square target) const
{
assert(from.isOnBoard());
assert(target.isOnBoard());
const Offset32 offset32 = Offset32(target, from);
const Entry& e = table[ptypeOIndex(ptypeo)][offset32.index()];
if (e.has_unblockable_effect)
return true;
if (e.nearest.zero())
return false;
assert(Ptype_Table.hasLongMove(getPtype(ptypeo)));
const Square nearest = from+e.nearest;
if (nearest.isEdge())
{
return false;
}
return state.isEmptyBetween(from, nearest, false);
}
bool hasEffectOrAdditional(const NumEffectState& state,
PtypeO ptypeo, Square from,
Square target) const
{
const Offset32 offset32 = Offset32(target, from);
const Entry& e = table[ptypeOIndex(ptypeo)][offset32.index()];
if (e.has_unblockable_effect)
return true;
if (e.nearest.zero())
return false;
assert(Ptype_Table.hasLongMove(getPtype(ptypeo)));
const Square nearest = from+e.nearest;
if (nearest.isEdge())
{
return false;
}
Offset offset=Board_Table.getShortOffset(Offset32(nearest,from));
assert(! offset.zero());
Square pos=from+offset;
Piece p = state.pieceAt(pos);
for (; p.isEmpty(); pos+=offset, p=state.pieceAt(pos)) {
if (pos==nearest)
return true;
}
assert(p.isPiece());
if (pos == nearest || state.hasEffectByPiece(p, nearest))
return true;
const Player attack = getOwner(ptypeo);
if (target != state.kingSquare(alt(attack)))
return false;
// new pin?
const Direction dir = longToShort(Board_Table.getLongDirection(attack,Offset32(nearest, from)));
return pos == state.kingMobilityOfPlayer(alt(attack), dir);
}
Square findNearest(const NumEffectState& state,
PtypeO ptypeo, Square from,
Square target) const
{
const Offset32 offset32 = Offset32(target, from);
const Entry& e = table[ptypeOIndex(ptypeo)][offset32.index()];
if (e.has_unblockable_effect)
return from;
if (e.nearest.zero())
return Square::STAND();
assert(Ptype_Table.hasLongMove(getPtype(ptypeo)));
const Square nearest = from+e.nearest;
if (!nearest.isEdge() && state.isEmptyBetween(from, nearest, false))
return nearest;
return Square::STAND();
}
};
// tables.ccに入れればconstに出来る
static Table table;
public:
static bool hasEffect(const NumEffectState& state,
PtypeO ptypeo, Square from,
Square target)
{
return table.hasEffect(state, ptypeo, from, target);
}
static bool hasEffectOrAdditional(const NumEffectState& state,
PtypeO ptypeo, Square from,
Square target)
{
return table.hasEffectOrAdditional(state, ptypeo, from, target);
}
static Square findNearest(const NumEffectState& state,
PtypeO ptypeo, Square from,
Square target)
{
return table.findNearest(state, ptypeo, from, target);
}
private:
static bool hasEffectFromTo(const NumEffectState& state,
PtypeO ptypeo, Square from,
Square target, Direction d);
public:
static bool hasEffectNaive(const NumEffectState& state,
PtypeO ptypeo, Square from,
Square target);
static void init();
};
} // namespace effect_util
using effect_util::Neighboring8Direct;
} // namespace osl
#endif /* EFFECT_UTIL_NEIGHBORING8DIRECT_H */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
osl::Neighboring8Direct::init
static void init()
Definition neighboring8Direct.cc:15
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::kingMobilityOfPlayer
Square kingMobilityOfPlayer(Player p, Direction d) const
玉がd方向にどこまで動けるかを返す
Definition numEffectState.h:192
osl::Offset32Base::index
unsigned int index() const
Definition offset32.h:40
osl::Offset
座標の差分
Definition basic_type.h:430
osl::Offset::zero
bool zero() const
Definition basic_type.h:502
osl::Piece
駒.
Definition basic_type.h:788
osl::Piece::isEmpty
bool isEmpty() const
Definition basic_type.h:913
osl::Piece::isPiece
bool isPiece() const
Definition basic_type.h:953
osl::SimpleState::isEmptyBetween
bool isEmptyBetween(Square from, Square to, Offset offset, bool pieceExistsAtTo=false) const
Definition simpleState.h:267
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::STAND
static const Square STAND()
Definition basic_type.h:548
osl::Square::isOnBoard
bool isOnBoard() const
盤面上を表すかどうかの判定． 1<=x() && x()<=9 && 1<=y() && y()<=9 Squareの内部表現に依存する．
Definition basic_type.h:583
osl::Square::isEdge
bool isEdge() const
onBoardから8近傍のオフセットを足した点がedgeかどうかの判定 そこそこ速くなった．
Definition basic_type.h:591
osl::effect_util::Neighboring8Direct::Table
Definition neighboring8Direct.h:18
osl::effect_util::Neighboring8Direct::Table::findNearest
Square findNearest(const NumEffectState &state, PtypeO ptypeo, Square from, Square target) const
Definition neighboring8Direct.h:85
osl::effect_util::Neighboring8Direct::Table::hasEffect
bool hasEffect(const NumEffectState &state, PtypeO ptypeo, Square from, Square target) const
Definition neighboring8Direct.h:31
osl::effect_util::Neighboring8Direct::Table::table
CArray2d< Entry, PTYPEO_SIZE, Offset32::SIZE > table
Definition neighboring8Direct.h:27
osl::effect_util::Neighboring8Direct::Table::hasEffectOrAdditional
bool hasEffectOrAdditional(const NumEffectState &state, PtypeO ptypeo, Square from, Square target) const
Definition neighboring8Direct.h:51
osl::effect_util::Neighboring8Direct::table
static Table table
Definition neighboring8Direct.h:103
osl::effect_util::Neighboring8Direct::init
static void init()
Definition neighboring8Direct.cc:15
osl::effect_util::Neighboring8Direct::hasEffectFromTo
static bool hasEffectFromTo(const NumEffectState &state, PtypeO ptypeo, Square from, Square target, Direction d)
Definition neighboring8Direct.cc:92
osl::effect_util::Neighboring8Direct::findNearest
static Square findNearest(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
Definition neighboring8Direct.h:124
osl::effect_util::Neighboring8Direct::hasEffectOrAdditional
static bool hasEffectOrAdditional(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
ptypeo の駒がfromからtargetの8近傍に直接の利きを持つか そのような駒への追加/影利きになっている
Definition neighboring8Direct.h:118
osl::effect_util::Neighboring8Direct::hasEffect
static bool hasEffect(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
ptypeo の駒がfromからtargetの8近傍に直接の利きを持つか
Definition neighboring8Direct.h:108
osl::effect_util::Neighboring8Direct::hasEffectNaive
static bool hasEffectNaive(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
Definition neighboring8Direct.cc:101
Neighboring8Direct
Definition neighboring8Direct.cc:9
osl::effect_util
Definition additionalEffect.h:8
osl
Definition additionalEffect.h:6
osl::Ptype_Table
const PtypeTable Ptype_Table
Definition tables.cc:97
osl::CArray2d
CArray< CArray< T, Capacity2 >, Capacity1 > CArray2d
Definition container.h:95
osl::getPtype
Ptype getPtype(PtypeO ptypeO)
Definition basic_type.h:217
osl::longToShort
constexpr Direction longToShort(Direction d)
Definition basic_type.h:380
osl::Board_Table
const BoardTable Board_Table
Definition tables.cc:95
osl::getOwner
Player getOwner(PtypeO ptypeO)
Definition basic_type.h:256
osl::Direction
Direction
Definition basic_type.h:310
osl::ptypeOIndex
unsigned int ptypeOIndex(PtypeO ptypeo)
Definition basic_type.h:205
osl::Offset32
Offset32Base< 8, 9 > Offset32
Definition offset32.h:63
osl::Player
Player
Definition basic_type.h:8
osl::PtypeO
PtypeO
Player + Ptype [-15, 15] PtypeO の O は Owner の O.
Definition basic_type.h:199
osl::alt
constexpr Player alt(Player player)
Definition basic_type.h:13
numEffectState.h
osl::effect_util::Neighboring8Direct::Table::Entry
Definition neighboring8Direct.h:20
osl::effect_util::Neighboring8Direct::Table::Entry::nearest
Offset nearest
Definition neighboring8Direct.h:22
osl::effect_util::Neighboring8Direct::Table::Entry::Entry
Entry()
Definition neighboring8Direct.h:23
osl::effect_util::Neighboring8Direct::Table::Entry::has_unblockable_effect
bool has_unblockable_effect
Definition neighboring8Direct.h:21