neighboring8Direct.cc
Go to the documentation of this file.
/* neighboring8Direct.cc
*/
#include "osl/effect_util/neighboring8Direct.h"
#include "osl/oslConfig.h"
osl::effect_util::Neighboring8Direct::Table osl::effect_util::Neighboring8Direct::table;
namespace
{
namespace Neighboring8Direct {
osl::SetUpRegister _initializer([](){ osl::effect_util::Neighboring8Direct::init(); });
}
}
void osl::effect_util::Neighboring8Direct::
init()
{
table.init(BLACK);
table.init(WHITE);
}
void osl::effect_util::Neighboring8Direct::
Table::init(const Player player)
{
for (int p=PTYPE_PIECE_MIN; p<=PTYPE_MAX; ++p)
{
const Ptype ptype = static_cast<Ptype>(p);
assert(isPiece(ptype));
const PtypeO ptypeo = newPtypeO(player, ptype);
const int mask = Ptype_Table.getMoveMask(ptype);
for (int d=DIRECTION_MIN; d<=DIRECTION_MAX; ++d)
{
const Direction direction = static_cast<Direction>(d);
if (! (mask & (1<<direction)))
continue;
const Offset offset = Board_Table.getOffset(player, direction);
assert(! offset.zero());
const int x = offset.dx();
const int y = offset.dy();
for (int dy=-1; dy<=1; ++dy)
{
for (int dx=-1; dx<=1; ++dx)
{
const Offset32 offset32 = Offset32(x+dx, y+dy);
table[ptypeOIndex(ptypeo)][offset32.index()].
has_unblockable_effect = true;
}
}
if (isLong(direction))
{
assert(abs(x)<=1);
assert(abs(y)<=1);
for (int i=1; i<8; ++i)
{
const int long_x = x*i;
const int long_y = y*i;
const int target_x = x*(i+1);
const int target_y = y*(i+1);
const Offset32 offset32 = Offset32(target_x, target_y);
Entry& e = table[ptypeOIndex(ptypeo)][offset32.index()];
e.nearest = Offset(long_x, long_y);
}
for (int i=1; i<9; ++i)
{
const int long_x = x*i;
const int long_y = y*i;
for (int dy=-1; dy<=1; ++dy)
{
const int target_y = long_y+dy;
if ((target_y < -8) || (8 < target_y))
continue;
for (int dx=-1; dx<=1; ++dx)
{
const int target_x = long_x+dx;
if ((target_x < -8) || (8 < target_x))
continue;
const Offset32 offset32 = Offset32(target_x, target_y);
Entry& e = table[ptypeOIndex(ptypeo)][offset32.index()];
// 近いところ優先
if (e.nearest.zero())
{
e.nearest = Offset(long_x, long_y);
}
}
}
}
}
}
}
}
bool osl::effect_util::Neighboring8Direct::
hasEffectFromTo(const NumEffectState& state, PtypeO ptypeo, Square from,
Square target, Direction d)
{
target += Board_Table.getOffsetForBlack(d); // 8 近傍全て試すなら手番による符合変換は不要
return target.isOnBoard()
&& state.hasEffectIf(ptypeo, from, target);
}
bool osl::effect_util::Neighboring8Direct::
hasEffectNaive(const NumEffectState& state, PtypeO ptypeo, Square from,
Square target)
{
const Ptype ptype = getPtype(ptypeo);
if (! Ptype_Table.hasLongMove(ptype))
{
if (abs(from.y() - target.y()) > 3) // knight だけ3
return false;
if (abs(from.x() - target.x()) > 2)
return false;
}
else if (ptype == LANCE)
{
if (abs(from.x() - target.x()) > 1)
return false;
}
// naive な実装
return hasEffectFromTo(state, ptypeo, from, target, UL)
|| hasEffectFromTo(state, ptypeo, from, target, U)
|| hasEffectFromTo(state, ptypeo, from, target, UR)
|| hasEffectFromTo(state, ptypeo, from, target, L)
|| hasEffectFromTo(state, ptypeo, from, target, R)
|| hasEffectFromTo(state, ptypeo, from, target, DL)
|| hasEffectFromTo(state, ptypeo, from, target, D)
|| hasEffectFromTo(state, ptypeo, from, target, DR);
}
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
_initializer
static osl::SetUpRegister _initializer([](){ osl::Centering3x3::table.init();})
osl::NumEffectState
利きを持つ局面
Definition numEffectState.h:34
osl::NumEffectState::hasEffectIf
bool hasEffectIf(PtypeO ptypeo, Square attacker, Square target) const
attackerにptypeoの駒がいると仮定した場合にtargetに利きがあるかどうか を stateをupdateしないで確かめる.
Definition numEffectState.h:465
osl::Offset32Base::index
unsigned int index() const
Definition offset32.h:40
osl::Offset
座標の差分
Definition basic_type.h:430
osl::Offset::dx
int dx() const
Offsetから一般に dxは求まらないので, ここでの入力は12近傍のみとする
Definition basic_type.cc:119
osl::Offset::dy
int dy() const
Offsetから一般に dyは求まらないので, ここでの入力は12近傍のみとする
Definition basic_type.cc:146
osl::Offset::zero
bool zero() const
Definition basic_type.h:502
osl::Square
Definition basic_type.h:532
osl::Square::y
int y() const
将棋としてのY座標を返す.
Definition basic_type.h:567
osl::Square::isOnBoard
bool isOnBoard() const
盤面上を表すかどうかの判定． 1<=x() && x()<=9 && 1<=y() && y()<=9 Squareの内部表現に依存する．
Definition basic_type.h:583
osl::Square::x
int x() const
将棋としてのX座標を返す.
Definition basic_type.h:563
osl::effect_util::Neighboring8Direct::Table
Definition neighboring8Direct.h:18
osl::effect_util::Neighboring8Direct::Table::init
void init(Player)
Definition neighboring8Direct.cc:22
osl::effect_util::Neighboring8Direct::Table::table
CArray2d< Entry, PTYPEO_SIZE, Offset32::SIZE > table
Definition neighboring8Direct.h:27
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::hasEffectNaive
static bool hasEffectNaive(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
Definition neighboring8Direct.cc:101
Neighboring8Direct
Definition neighboring8Direct.cc:9
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::LANCE
@ LANCE
Definition basic_type.h:96
osl::Ptype_Table
const PtypeTable Ptype_Table
Definition tables.cc:97
osl::getPtype
Ptype getPtype(PtypeO ptypeO)
Definition basic_type.h:217
osl::Board_Table
const BoardTable Board_Table
Definition tables.cc:95
osl::Direction
Direction
Definition basic_type.h:310
osl::R
@ R
Definition basic_type.h:317
osl::D
@ D
Definition basic_type.h:319
osl::DIRECTION_MAX
@ DIRECTION_MAX
Definition basic_type.h:337
osl::UL
@ UL
Definition basic_type.h:313
osl::DIRECTION_MIN
@ DIRECTION_MIN
Definition basic_type.h:334
osl::DR
@ DR
Definition basic_type.h:320
osl::U
@ U
Definition basic_type.h:314
osl::L
@ L
Definition basic_type.h:316
osl::UR
@ UR
Definition basic_type.h:315
osl::DL
@ DL
Definition basic_type.h:318
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::WHITE
@ WHITE
Definition basic_type.h:10
osl::BLACK
@ BLACK
Definition basic_type.h:9
osl::isPiece
constexpr bool isPiece(Ptype ptype)
ptypeが空白やEDGEでないかのチェック
Definition basic_type.h:120
osl::PtypeO
PtypeO
Player + Ptype [-15, 15] PtypeO の O は Owner の O.
Definition basic_type.h:199
osl::isLong
constexpr bool isLong(Direction d)
Definition basic_type.h:350
osl::newPtypeO
PtypeO newPtypeO(Player player, Ptype ptype)
Definition basic_type.h:211
neighboring8Direct.h
oslConfig.h
osl::SetUpRegister
Definition oslConfig.h:128
osl::effect_util::Neighboring8Direct::Table::Entry
Definition neighboring8Direct.h:20
osl::effect_util::Neighboring8Direct::Table::Entry::nearest
Offset nearest
Definition neighboring8Direct.h:22