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 
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:
bool hasEffectIf(PtypeO ptypeo, Square attacker, Square target) const
attackerにptypeoの駒がいると仮定した場合にtargetに利きがあるかどうか を stateをupdateしないで確かめる...
bool zero() const
Definition: basic_type.h:502
int getMoveMask(Ptype ptype) const
Definition: ptypeTable.h:84
座標の差分
Definition: basic_type.h:429
Ptype getPtype(PtypeO ptypeO)
Definition: basic_type.h:217
差が uniqになるような座標の差分.
Definition: offset32.h:16
int y() const
将棋としてのY座標を返す.
Definition: basic_type.h:567
static osl::SetUpRegister _initializer([](){ osl::Centering3x3::table.init();})
int dy() const
Offsetから一般に dyは求まらないので, ここでの入力は12近傍のみとする
Definition: basic_type.cc:146
int x() const
将棋としてのX座標を返す.
Definition: basic_type.h:563
Offset32Base< 8, 9 > Offset32
Definition: offset32.h:63
PtypeO newPtypeO(Player player, Ptype ptype)
Definition: basic_type.h:211
Ptype
駒の種類を4ビットでコード化する
Definition: basic_type.h:83
const Offset getOffsetForBlack(Direction dir) const
黒にとってのoffsetを返す
Definition: boardTable.h:37
const PtypeTable Ptype_Table
Definition: tables.cc:97
bool isOnBoard() const
盤面上を表すかどうかの判定. 1<=x() && x()<=9 && 1<=y() && y()<=9 Squareの内部表現に依存する. ...
Definition: basic_type.h:583
bool hasLongMove(Ptype ptype) const
遅くて良い?
Definition: ptypeTable.h:54
unsigned int index() const
Definition: offset32.h:40
利きを持つ局面
PtypeO
Player + Ptype [-15, 15] PtypeO の O は Owner の O.
Definition: basic_type.h:199
static bool hasEffectFromTo(const NumEffectState &state, PtypeO ptypeo, Square from, Square target, Direction d)
Direction
Definition: basic_type.h:310
static bool hasEffectNaive(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
constexpr bool isPiece(Ptype ptype)
ptypeが空白やEDGEでないかのチェック
Definition: basic_type.h:120
constexpr bool isLong(Direction d)
Definition: basic_type.h:350
Player
Definition: basic_type.h:8
const Offset getOffset(Direction dir) const
Definition: boardTable.h:47
int dx() const
Offsetから一般に dxは求まらないので, ここでの入力は12近傍のみとする
Definition: basic_type.cc:119
unsigned int ptypeOIndex(PtypeO ptypeo)
Definition: basic_type.h:205
const BoardTable Board_Table
Definition: tables.cc:95