All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 #include "osl/direction.h"
8 
9 namespace osl
10 {
11  namespace effect_util
12  {
17  {
18  class Table
19  {
20  struct Entry
21  {
25  {
26  }
27  };
28  CArray2d<Entry,PTYPEO_SIZE,Offset32::SIZE> table;
29  void init(Player);
30  public:
31  Table();
32  bool hasEffect(const NumEffectState& state,
33  PtypeO ptypeo, Square from,
34  Square target) const
35  {
36  assert(from.isOnBoard());
37  assert(target.isOnBoard());
38  const Offset32 offset32 = Offset32(target, from);
39  const Entry& e = table[ptypeOIndex(ptypeo)][offset32.index()];
41  return true;
42  if (e.nearest.zero())
43  return false;
44  assert(Ptype_Table.hasLongMove(getPtype(ptypeo)));
45  const Square nearest = from+e.nearest;
46  if (nearest.isEdge())
47  {
48  return false;
49  }
50  return state.isEmptyBetween(from, nearest, false);
51  }
52  bool hasEffectOrAdditional(const NumEffectState& state,
53  PtypeO ptypeo, Square from,
54  Square target) const
55  {
56  const Offset32 offset32 = Offset32(target, from);
57  const Entry& e = table[ptypeOIndex(ptypeo)][offset32.index()];
59  return true;
60  if (e.nearest.zero())
61  return false;
62  assert(Ptype_Table.hasLongMove(getPtype(ptypeo)));
63  const Square nearest = from+e.nearest;
64  if (nearest.isEdge())
65  {
66  return false;
67  }
68  Offset offset=Board_Table.getShortOffset(Offset32(nearest,from));
69  assert(! offset.zero());
70  Square pos=from+offset;
71  Piece p = state.pieceAt(pos);
72  for (; p.isEmpty(); pos+=offset, p=state.pieceAt(pos)) {
73  if (pos==nearest)
74  return true;
75  }
76  assert(p.isPiece());
77  if (pos == nearest || state.hasEffectByPiece(p, nearest))
78  return true;
79  const Player attack = getOwner(ptypeo);
80  if (target != state.kingSquare(alt(attack)))
81  return false;
82  // new pin?
83  const Direction dir = longToShort(Board_Table.getLongDirection(attack,Offset32(nearest, from)));
84  return pos == state.kingMobilityOfPlayer(alt(attack), dir);
85  }
86  Square findNearest(const NumEffectState& state,
87  PtypeO ptypeo, Square from,
88  Square target) const
89  {
90  const Offset32 offset32 = Offset32(target, from);
91  const Entry& e = table[ptypeOIndex(ptypeo)][offset32.index()];
93  return from;
94  if (e.nearest.zero())
95  return Square::STAND();
96  assert(Ptype_Table.hasLongMove(getPtype(ptypeo)));
97  const Square nearest = from+e.nearest;
98  if (!nearest.isEdge() && state.isEmptyBetween(from, nearest, false))
99  return nearest;
100  return Square::STAND();
101  }
102  };
103  static const 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  };
139 
140  } // namespace effect_util
141  using effect_util::Neighboring8Direct;
142 } // namespace osl
143 
144 #endif /* EFFECT_UTIL_NEIGHBORING8DIRECT_H */
145 // ;;; Local Variables:
146 // ;;; mode:c++
147 // ;;; c-basic-offset:2
148 // ;;; End: