My Project
neighboring8Direct.cc
Go to the documentation of this file.
1 /* neighboring8Direct.cc
2  */
4 #include "osl/oslConfig.h"
6 
7 namespace
8 {
9  namespace Neighboring8Direct {
11  }
12 }
13 
15 init()
16 {
17  table.init(BLACK);
18  table.init(WHITE);
19 }
20 
22 Table::init(const Player player)
23 {
24  for (int p=PTYPE_PIECE_MIN; p<=PTYPE_MAX; ++p)
25  {
26  const Ptype ptype = static_cast<Ptype>(p);
27  assert(isPiece(ptype));
28  const PtypeO ptypeo = newPtypeO(player, ptype);
29  const int mask = Ptype_Table.getMoveMask(ptype);
30  for (int d=DIRECTION_MIN; d<=DIRECTION_MAX; ++d)
31  {
32  const Direction direction = static_cast<Direction>(d);
33  if (! (mask & (1<<direction)))
34  continue;
35  const Offset offset = Board_Table.getOffset(player, direction);
36  assert(! offset.zero());
37  const int x = offset.dx();
38  const int y = offset.dy();
39  for (int dy=-1; dy<=1; ++dy)
40  {
41  for (int dx=-1; dx<=1; ++dx)
42  {
43  const Offset32 offset32 = Offset32(x+dx, y+dy);
44  table[ptypeOIndex(ptypeo)][offset32.index()].
45  has_unblockable_effect = true;
46  }
47  }
48  if (isLong(direction))
49  {
50  assert(abs(x)<=1);
51  assert(abs(y)<=1);
52  for (int i=1; i<8; ++i)
53  {
54  const int long_x = x*i;
55  const int long_y = y*i;
56  const int target_x = x*(i+1);
57  const int target_y = y*(i+1);
58  const Offset32 offset32 = Offset32(target_x, target_y);
59  Entry& e = table[ptypeOIndex(ptypeo)][offset32.index()];
60  e.nearest = Offset(long_x, long_y);
61  }
62  for (int i=1; i<9; ++i)
63  {
64  const int long_x = x*i;
65  const int long_y = y*i;
66  for (int dy=-1; dy<=1; ++dy)
67  {
68  const int target_y = long_y+dy;
69  if ((target_y < -8) || (8 < target_y))
70  continue;
71  for (int dx=-1; dx<=1; ++dx)
72  {
73  const int target_x = long_x+dx;
74  if ((target_x < -8) || (8 < target_x))
75  continue;
76  const Offset32 offset32 = Offset32(target_x, target_y);
77  Entry& e = table[ptypeOIndex(ptypeo)][offset32.index()];
78  // 近いところ優先
79  if (e.nearest.zero())
80  {
81  e.nearest = Offset(long_x, long_y);
82  }
83  }
84  }
85  }
86  }
87  }
88  }
89 }
90 
92 hasEffectFromTo(const NumEffectState& state, PtypeO ptypeo, Square from,
93  Square target, Direction d)
94 {
95  target += Board_Table.getOffsetForBlack(d); // 8 近傍全て試すなら手番による符合変換は不要
96  return target.isOnBoard()
97  && state.hasEffectIf(ptypeo, from, target);
98 }
99 
101 hasEffectNaive(const NumEffectState& state, PtypeO ptypeo, Square from,
102  Square target)
103 {
104  const Ptype ptype = getPtype(ptypeo);
105  if (! Ptype_Table.hasLongMove(ptype))
106  {
107  if (abs(from.y() - target.y()) > 3) // knight だけ3
108  return false;
109  if (abs(from.x() - target.x()) > 2)
110  return false;
111  }
112  else if (ptype == LANCE)
113  {
114  if (abs(from.x() - target.x()) > 1)
115  return false;
116  }
117 
118  // naive な実装
119  return hasEffectFromTo(state, ptypeo, from, target, UL)
120  || hasEffectFromTo(state, ptypeo, from, target, U)
121  || hasEffectFromTo(state, ptypeo, from, target, UR)
122  || hasEffectFromTo(state, ptypeo, from, target, L)
123  || hasEffectFromTo(state, ptypeo, from, target, R)
124  || hasEffectFromTo(state, ptypeo, from, target, DL)
125  || hasEffectFromTo(state, ptypeo, from, target, D)
126  || hasEffectFromTo(state, ptypeo, from, target, DR);
127 }
128 
129 // ;;; Local Variables:
130 // ;;; mode:c++
131 // ;;; c-basic-offset:2
132 // ;;; End:
osl::UR
@ UR
Definition: basic_type.h:315
osl::PTYPE_PIECE_MIN
@ PTYPE_PIECE_MIN
Definition: basic_type.h:104
osl::Square
Definition: basic_type.h:532
osl::WHITE
@ WHITE
Definition: basic_type.h:10
osl::Square::isOnBoard
bool isOnBoard() const
盤面上を表すかどうかの判定. 1<=x() && x()<=9 && 1<=y() && y()<=9 Squareの内部表現に依存する.
Definition: basic_type.h:583
osl::SetUpRegister
Definition: oslConfig.h:128
osl::Board_Table
const BoardTable Board_Table
Definition: tables.cc:95
osl::R
@ R
Definition: basic_type.h:317
Neighboring8Direct
Definition: neighboring8Direct.cc:9
osl::Offset::dx
int dx() const
Offsetから一般に dxは求まらないので, ここでの入力は12近傍のみとする
Definition: basic_type.cc:119
neighboring8Direct.h
osl::Offset32
Offset32Base< 8, 9 > Offset32
Definition: offset32.h:63
_initializer
static osl::SetUpRegister _initializer([](){ osl::Centering3x3::table.init();})
osl::getPtype
Ptype getPtype(PtypeO ptypeO)
Definition: basic_type.h:217
osl::PtypeTable::hasLongMove
bool hasLongMove(Ptype ptype) const
遅くて良い?
Definition: ptypeTable.h:54
osl::effect_util::Neighboring8Direct::Table::Entry
Definition: neighboring8Direct.h:20
osl::effect_util::Neighboring8Direct::hasEffectFromTo
static bool hasEffectFromTo(const NumEffectState &state, PtypeO ptypeo, Square from, Square target, Direction d)
Definition: neighboring8Direct.cc:92
osl::newPtypeO
PtypeO newPtypeO(Player player, Ptype ptype)
Definition: basic_type.h:211
osl::Offset
座標の差分
Definition: basic_type.h:430
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition: basic_type.h:84
osl::DIRECTION_MAX
@ DIRECTION_MAX
Definition: basic_type.h:337
osl::LANCE
@ LANCE
Definition: basic_type.h:96
osl::NumEffectState::hasEffectIf
bool hasEffectIf(PtypeO ptypeo, Square attacker, Square target) const
attackerにptypeoの駒がいると仮定した場合にtargetに利きがあるかどうか を stateをupdateしないで確かめる.
Definition: numEffectState.h:465
osl::DIRECTION_MIN
@ DIRECTION_MIN
Definition: basic_type.h:334
osl::D
@ D
Definition: basic_type.h:319
osl::effect_util::Neighboring8Direct::Table::Entry::nearest
Offset nearest
Definition: neighboring8Direct.h:22
osl::Ptype_Table
const PtypeTable Ptype_Table
Definition: tables.cc:97
osl::Offset::zero
bool zero() const
Definition: basic_type.h:502
osl::PtypeTable::getMoveMask
int getMoveMask(Ptype ptype) const
Definition: ptypeTable.h:84
osl::effect_util::Neighboring8Direct::hasEffectNaive
static bool hasEffectNaive(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
Definition: neighboring8Direct.cc:101
osl::DR
@ DR
Definition: basic_type.h:320
osl::PTYPE_MAX
@ PTYPE_MAX
Definition: basic_type.h:105
osl::L
@ L
Definition: basic_type.h:316
osl::Offset32Base
差が uniqになるような座標の差分.
Definition: offset32.h:17
osl::effect_util::Neighboring8Direct::Table::init
void init(Player)
Definition: neighboring8Direct.cc:22
oslConfig.h
osl::BoardTable::getOffsetForBlack
const Offset getOffsetForBlack(Direction dir) const
黒にとってのoffsetを返す
Definition: boardTable.h:37
osl::Square::x
int x() const
将棋としてのX座標を返す.
Definition: basic_type.h:563
osl::PtypeO
PtypeO
Player + Ptype [-15, 15] PtypeO の O は Owner の O.
Definition: basic_type.h:199
osl::NumEffectState
利きを持つ局面
Definition: numEffectState.h:34
osl::BoardTable::getOffset
const Offset getOffset(Direction dir) const
Definition: boardTable.h:47
osl::Direction
Direction
Definition: basic_type.h:310
osl::isPiece
constexpr bool isPiece(Ptype ptype)
ptypeが空白やEDGEでないかのチェック
Definition: basic_type.h:120
osl::Square::y
int y() const
将棋としてのY座標を返す.
Definition: basic_type.h:567
osl::isLong
constexpr bool isLong(Direction d)
Definition: basic_type.h:350
osl::effect_util::Neighboring8Direct::table
static Table table
Definition: neighboring8Direct.h:103
osl::Offset32Base::index
unsigned int index() const
Definition: offset32.h:40
osl::BLACK
@ BLACK
Definition: basic_type.h:9
osl::ptypeOIndex
unsigned int ptypeOIndex(PtypeO ptypeo)
Definition: basic_type.h:205
osl::DL
@ DL
Definition: basic_type.h:318
osl::Offset::dy
int dy() const
Offsetから一般に dyは求まらないので, ここでの入力は12近傍のみとする
Definition: basic_type.cc:146
osl::Player
Player
Definition: basic_type.h:8
osl::effect_util::Neighboring8Direct::init
static void init()
Definition: neighboring8Direct.cc:15
osl::UL
@ UL
Definition: basic_type.h:313
osl::U
@ U
Definition: basic_type.h:314
osl::effect_util::Neighboring8Direct::Table
Definition: neighboring8Direct.h:18