My Project
neighboring8Direct.h
Go to the documentation of this file.
1/* neighboring8Direct.h
2 */
3#ifndef EFFECT_UTIL_NEIGHBORING8DIRECT_H
4#define EFFECT_UTIL_NEIGHBORING8DIRECT_H
5
7
8namespace osl
9{
10 namespace effect_util
11 {
16 {
17 class Table
18 {
19 struct Entry
20 {
24 {
25 }
26 };
28 friend class Neighboring8Direct;
29 void init(Player);
30 public:
31 bool hasEffect(const NumEffectState& state,
32 PtypeO ptypeo, Square from,
33 Square target) const
34 {
35 assert(from.isOnBoard());
36 assert(target.isOnBoard());
37 const Offset32 offset32 = Offset32(target, from);
38 const Entry& e = table[ptypeOIndex(ptypeo)][offset32.index()];
40 return true;
41 if (e.nearest.zero())
42 return false;
43 assert(Ptype_Table.hasLongMove(getPtype(ptypeo)));
44 const Square nearest = from+e.nearest;
45 if (nearest.isEdge())
46 {
47 return false;
48 }
49 return state.isEmptyBetween(from, nearest, false);
50 }
52 PtypeO ptypeo, Square from,
53 Square target) const
54 {
55 const Offset32 offset32 = Offset32(target, from);
56 const Entry& e = table[ptypeOIndex(ptypeo)][offset32.index()];
58 return true;
59 if (e.nearest.zero())
60 return false;
61 assert(Ptype_Table.hasLongMove(getPtype(ptypeo)));
62 const Square nearest = from+e.nearest;
63 if (nearest.isEdge())
64 {
65 return false;
66 }
67 Offset offset=Board_Table.getShortOffset(Offset32(nearest,from));
68 assert(! offset.zero());
69 Square pos=from+offset;
70 Piece p = state.pieceAt(pos);
71 for (; p.isEmpty(); pos+=offset, p=state.pieceAt(pos)) {
72 if (pos==nearest)
73 return true;
74 }
75 assert(p.isPiece());
76 if (pos == nearest || state.hasEffectByPiece(p, nearest))
77 return true;
78 const Player attack = getOwner(ptypeo);
79 if (target != state.kingSquare(alt(attack)))
80 return false;
81 // new pin?
82 const Direction dir = longToShort(Board_Table.getLongDirection(attack,Offset32(nearest, from)));
83 return pos == state.kingMobilityOfPlayer(alt(attack), dir);
84 }
86 PtypeO ptypeo, Square from,
87 Square target) const
88 {
89 const Offset32 offset32 = Offset32(target, from);
90 const Entry& e = table[ptypeOIndex(ptypeo)][offset32.index()];
92 return from;
93 if (e.nearest.zero())
94 return Square::STAND();
95 assert(Ptype_Table.hasLongMove(getPtype(ptypeo)));
96 const Square nearest = from+e.nearest;
97 if (!nearest.isEdge() && state.isEmptyBetween(from, nearest, false))
98 return nearest;
99 return Square::STAND();
100 }
101 };
102 // tables.ccに入れればconstに出来る
103 static Table table;
104 public:
108 static bool hasEffect(const NumEffectState& state,
109 PtypeO ptypeo, Square from,
110 Square target)
111 {
112 return table.hasEffect(state, ptypeo, from, target);
113 }
118 static bool hasEffectOrAdditional(const NumEffectState& state,
119 PtypeO ptypeo, Square from,
120 Square target)
121 {
122 return table.hasEffectOrAdditional(state, ptypeo, from, target);
123 }
124 static Square findNearest(const NumEffectState& state,
125 PtypeO ptypeo, Square from,
126 Square target)
127 {
128 return table.findNearest(state, ptypeo, from, target);
129 }
130 private:
131 static bool hasEffectFromTo(const NumEffectState& state,
132 PtypeO ptypeo, Square from,
133 Square target, Direction d);
134 public:
135 static bool hasEffectNaive(const NumEffectState& state,
136 PtypeO ptypeo, Square from,
137 Square target);
138 static void init();
139 };
140
141 } // namespace effect_util
142 using effect_util::Neighboring8Direct;
143} // namespace osl
144
145#endif /* EFFECT_UTIL_NEIGHBORING8DIRECT_H */
146// ;;; Local Variables:
147// ;;; mode:c++
148// ;;; c-basic-offset:2
149// ;;; End:
const Offset getShortOffset(Offset32 offset32) const
Longの利きの可能性のあるoffsetの場合は, 反復に使う offsetを Shortの利きのoffsetの場合はそれ自身を返す.
Definition boardTable.h:110
Direction getLongDirection(Offset32 offset32) const
Definition boardTable.h:71
利きを持つ局面
bool hasEffectByPiece(Piece attack, Square target) const
駒attack が target に利きを持つか (旧hasEffectToと統合)
Square kingMobilityOfPlayer(Player p, Direction d) const
玉がd方向にどこまで動けるかを返す
差が uniqになるような座標の差分.
Definition offset32.h:17
unsigned int index() const
Definition offset32.h:40
座標の差分
Definition basic_type.h:430
bool zero() const
Definition basic_type.h:502
bool isEmpty() const
Definition basic_type.h:913
bool isPiece() const
Definition basic_type.h:953
bool hasLongMove(Ptype ptype) const
遅くて良い?
Definition ptypeTable.h:54
bool isEmptyBetween(Square from, Square to, Offset offset, bool pieceExistsAtTo=false) const
Square kingSquare() const
Definition simpleState.h:94
const Piece pieceAt(Square sq) const
static const Square STAND()
Definition basic_type.h:548
bool isOnBoard() const
盤面上を表すかどうかの判定. 1<=x() && x()<=9 && 1<=y() && y()<=9 Squareの内部表現に依存する.
Definition basic_type.h:583
bool isEdge() const
onBoardから8近傍のオフセットを足した点がedgeかどうかの判定 そこそこ速くなった.
Definition basic_type.h:591
Square findNearest(const NumEffectState &state, PtypeO ptypeo, Square from, Square target) const
bool hasEffect(const NumEffectState &state, PtypeO ptypeo, Square from, Square target) const
CArray2d< Entry, PTYPEO_SIZE, Offset32::SIZE > table
bool hasEffectOrAdditional(const NumEffectState &state, PtypeO ptypeo, Square from, Square target) const
static bool hasEffectFromTo(const NumEffectState &state, PtypeO ptypeo, Square from, Square target, Direction d)
static Square findNearest(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
static bool hasEffectOrAdditional(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
ptypeo の駒がfromからtargetの8近傍に直接の利きを持つか そのような駒への追加/影利きになっている
static bool hasEffect(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
ptypeo の駒がfromからtargetの8近傍に直接の利きを持つか
static bool hasEffectNaive(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
const PtypeTable Ptype_Table
Definition tables.cc:97
Ptype getPtype(PtypeO ptypeO)
Definition basic_type.h:217
constexpr Direction longToShort(Direction d)
Definition basic_type.h:380
const BoardTable Board_Table
Definition tables.cc:95
Player getOwner(PtypeO ptypeO)
Definition basic_type.h:256
Direction
Definition basic_type.h:310
unsigned int ptypeOIndex(PtypeO ptypeo)
Definition basic_type.h:205
Offset32Base< 8, 9 > Offset32
Definition offset32.h:63
Player
Definition basic_type.h:8
PtypeO
Player + Ptype [-15, 15] PtypeO の O は Owner の O.
Definition basic_type.h:199
constexpr Player alt(Player player)
Definition basic_type.h:13