neighboring8Direct.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
neighboring8Direct.cc
Go to the documentation of this file.
1 /* neighboring8Direct.cc
2 */
3 #include "osl/effect_util/neighboring8Direct.h"
4
5 osl::effect_util::Neighboring8Direct::
6 Table::Table()
7 {
8 init(BLACK);
9 init(WHITE);
10 }
11
12 void osl::effect_util::Neighboring8Direct::
13 Table::init(const Player player)
14 {
15 for (int p=PTYPE_PIECE_MIN; p<=PTYPE_MAX; ++p)
16 {
17 const Ptype ptype = static_cast<Ptype>(p);
18 assert(isPiece(ptype));
19 const PtypeO ptypeo = newPtypeO(player, ptype);
20 const int mask = Ptype_Table.getMoveMask(ptype);
21 for (int d=DIRECTION_MIN; d<=DIRECTION_MAX; ++d)
22 {
23 const Direction direction = static_cast<Direction>(d);
24 if (! (mask & (1<<direction)))
25 continue;
26 const Offset offset = Board_Table.getOffset(player, direction);
27 assert(! offset.zero());
28 const int x = offset.dx();
29 const int y = offset.dy();
30 for (int dy=-1; dy<=1; ++dy)
31 {
32 for (int dx=-1; dx<=1; ++dx)
33 {
34 const Offset32 offset32 = Offset32(x+dx, y+dy);
35 table[ptypeOIndex(ptypeo)][offset32.index()].
36 has_unblockable_effect = true;
37 }
38 }
39 if (isLong(direction))
40 {
41 assert(abs(x)<=1);
42 assert(abs(y)<=1);
43 for (int i=1; i<8; ++i)
44 {
45 const int long_x = x*i;
46 const int long_y = y*i;
47 const int target_x = x*(i+1);
48 const int target_y = y*(i+1);
49 const Offset32 offset32 = Offset32(target_x, target_y);
50 Entry& e = table[ptypeOIndex(ptypeo)][offset32.index()];
51 e.nearest = Offset(long_x, long_y);
52 }
53 for (int i=1; i<9; ++i)
54 {
55 const int long_x = x*i;
56 const int long_y = y*i;
57 for (int dy=-1; dy<=1; ++dy)
58 {
59 const int target_y = long_y+dy;
60 if ((target_y < -8) || (8 < target_y))
61 continue;
62 for (int dx=-1; dx<=1; ++dx)
63 {
64 const int target_x = long_x+dx;
65 if ((target_x < -8) || (8 < target_x))
66 continue;
67 const Offset32 offset32 = Offset32(target_x, target_y);
68 Entry& e = table[ptypeOIndex(ptypeo)][offset32.index()];
69 // 近いところ優先
70 if (e.nearest.zero())
71 {
72 e.nearest = Offset(long_x, long_y);
73 }
74 }
75 }
76 }
77 }
78 }
79 }
80 }
81
82 bool osl::effect_util::Neighboring8Direct::
83 hasEffectFromTo(const NumEffectState& state, PtypeO ptypeo, Square from,
84 Square target, Direction d)
85 {
86 target += Board_Table.getOffsetForBlack(d); // 8 近傍全て試すなら手番による符合変換は不要
87 return target.isOnBoard()
88 && state.hasEffectIf(ptypeo, from, target);
89 }
90
91 bool osl::effect_util::Neighboring8Direct::
92 hasEffectNaive(const NumEffectState& state, PtypeO ptypeo, Square from,
93 Square target)
94 {
95 const Ptype ptype = getPtype(ptypeo);
96 if (! Ptype_Table.hasLongMove(ptype))
97 {
98 if (abs(from.y() - target.y()) > 3) // knight だけ3
99 return false;
100 if (abs(from.x() - target.x()) > 2)
101 return false;
102 }
103 else if (ptype == LANCE)
104 {
105 if (abs(from.x() - target.x()) > 1)
106 return false;
107 }
108
109 // naive な実装
110 return hasEffectFromTo(state, ptypeo, from, target, UL)
111 || hasEffectFromTo(state, ptypeo, from, target, U)
112 || hasEffectFromTo(state, ptypeo, from, target, UR)
113 || hasEffectFromTo(state, ptypeo, from, target, L)
114 || hasEffectFromTo(state, ptypeo, from, target, R)
115 || hasEffectFromTo(state, ptypeo, from, target, DL)
116 || hasEffectFromTo(state, ptypeo, from, target, D)
117 || hasEffectFromTo(state, ptypeo, from, target, DR);
118 }
119
120 // ;;; Local Variables:
121 // ;;; mode:c++
122 // ;;; c-basic-offset:2
123 // ;;; End: