All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ptypeTable.h
Go to the documentation of this file.
1 /* ptypeTable.h
2  */
3 #ifndef OSL_PTYPETABLE_H
4 #define OSL_PTYPETABLE_H
5 
6 #include "osl/config.h"
7 #include "osl/ptype.h"
8 #include "osl/ptypeTraits.h"
9 #include "osl/effectContent.h"
10 #include "osl/direction.h"
11 #include "osl/square.h"
12 #include "osl/misc/carray.h"
13 #include "osl/misc/carray2d.h"
14 #include "osl/offset32.h"
15 
16 namespace osl
17 {
18  class PtypeTable
19  {
20  private:
21  CArray<mask_t, PTYPE_SIZE> numMaskLows;
22  CArray<int, PTYPE_SIZE> numIndices;
23  CArray<const char *, PTYPE_SIZE> names;
24  CArray<const char *, PTYPE_SIZE> csaNames;
25  CArray<bool, PTYPE_SIZE> betterToPromote;
26  CArray<int, PTYPE_SIZE> moveMasks;
27  CArray<int, PTYPE_SIZE> indexMins;
28  CArray<int, PTYPE_SIZE> indexLimits;
29 
30  CArray2d<int, 2, PTYPE_SIZE> canDropLimit;
31  // これらの2次元配列は2^nにそろえておいた方が速い.
32  CArray2d<EffectContent,PTYPEO_SIZE,Offset32::SIZE> effectTable;
33  CArray2d<EffectContent,PTYPEO_SIZE,Offset32::SIZE> effectTableNotLongU;
34  CArray2d<unsigned int, 2, SHORT_DIRECTION_SIZE> shortMoveMask;
35 
36  template<Ptype T> void initPtypeSub(Int2Type<false> isBasic);
37  template<Ptype T> void initPtypeSub(Int2Type<true> isBasic);
38  template<Ptype T> void initPtype();
39  public:
40  PtypeTable();
41  private:
42  void init();
43  public:
44  unsigned int getShortMoveMask(Player p,PtypeO ptypeo,Direction dir) const
45  {
46  return shortMoveMask[playerToIndex(p)][static_cast<int>(dir)] &
47  (1<<(ptypeo-PTYPEO_MIN));
48  }
49  mask_t getMaskLow(Ptype ptype) const
50  {
51  return numMaskLows[ptype];
52  }
53 #if OSL_WORDSIZE == 64
54  int getIndex(Ptype) const
55  {
56  return 0;
57  }
58 #elif OSL_WORDSIZE == 32
59  int getIndex(Ptype ptype) const
60  {
61  return numIndices[ptype];
62  }
63 #endif
64 
67  bool hasLongMove(Ptype ptype) const
68  {
69  return getIndexMin(unpromote(ptype))>=32;
70  }
71  bool isBetterToPromote(Ptype ptype) const
72  {
73  return betterToPromote[ptype];
74  }
75  int getCanDropLimit(Player player,Ptype ptype) const
76  {
77  assert(isValid(ptype) && !isPromoted(ptype));
78  return canDropLimit[playerToIndex(player)][ptype];
79  }
80 
81  private:
82  bool canDropTo(Ptype ptype, Square pos, Int2Type<BLACK>) const
83  {
84  return pos.y() >= getCanDropLimit(BLACK,ptype);
85  }
86  bool canDropTo(Ptype ptype, Square pos, Int2Type<WHITE>) const
87  {
88  return pos.y() <= getCanDropLimit(WHITE,ptype);
89  }
90  public:
91  bool canDropTo(Player pl, Ptype ptype, Square pos) const
92  {
93  if (pl == BLACK)
94  return canDropTo(ptype, pos, Int2Type<BLACK>());
95  else
96  return canDropTo(ptype, pos, Int2Type<WHITE>());
97  }
98 
99  const char *getName(Ptype ptype) const
100  {
101  return names[ptype];
102  }
103  const char *getCsaName(Ptype ptype) const
104  {
105  return csaNames[ptype];
106  }
107  int getMoveMask(Ptype ptype) const
108  {
109  return moveMasks[ptype];
110  }
111  int getIndexMin(Ptype ptype) const
112  {
113  assert(isBasic(ptype));
114  return indexMins[ptype];
115  }
116  int getIndexLimit(Ptype ptype) const
117  {
118  assert(isBasic(ptype));
119  return indexLimits[ptype];
120  }
121  static int getKingIndex(Player p)
122  {
123  assert(isValid(p));
124  if (p==BLACK)
126  else
128  }
135  const EffectContent getEffect(PtypeO ptypeo,Square from, Square to) const
136  {
137  assert(from.isOnBoard() && to.isOnBoard());
138  return getEffect(ptypeo,Offset32(to,from));
139  }
140  const EffectContent& getEffect(PtypeO ptypeo,Offset32 offset32) const
141  {
142  assert(isValidPtypeO(ptypeo));
143  return effectTable[ptypeo-PTYPEO_MIN][offset32.index()];
144  }
145  private:
147  {
148  assert(isValidPtypeO(ptypeo));
149  const int i1 = ptypeo-PTYPEO_MIN;
150  const int i2 = offset32.index();
151  return effectTable[i1][i2];
152  }
153  public:
155  const EffectContent
156  getEffectNotLongU(PtypeO ptypeo, Square from, Square to) const
157  {
158  assert(isValidPtypeO(ptypeo));
159  assert(from.isOnBoard() && to.isOnBoard());
160  Offset32 offset32=Offset32(to,from);
161  return effectTableNotLongU[ptypeo-PTYPEO_MIN][offset32.index()];
162  }
163  bool hasUnblockableEffect(PtypeO attacker, Square from, Square to) const
164  {
165  const EffectContent effect = getEffect(attacker, from, to);
166  return effect.hasUnblockableEffect();
167  }
168  };
169 
170  extern const PtypeTable Ptype_Table;
171 
172 } // namespace osl
173 
174 
175 #endif /* OSL_PTYPETABLE_H */
176 // ;;; Local Variables:
177 // ;;; mode:c++
178 // ;;; c-basic-offset:2
179 // ;;; End: