boardTable.h Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
boardTable.h
Go to the documentation of this file.
1 /* directionTable.h
2 */
3 #ifndef OSL_BOARD_TABLE_H
4 #define OSL_BOARD_TABLE_H
5
6 #include "osl/direction.h"
7 #include "osl/misc/carray.h"
8 #include "osl/offset32.h"
9
10 namespace osl
11 {
12 class BoardTable
13 {
14 CArray<Direction,Offset32::SIZE> directions;
15 // 上位はoffsetが，下位3bitはdirectionが入る．
16 CArray<signed char,Offset::ONBOARD_OFFSET_SIZE> short8Offset;
17 CArray<unsigned char,Offset::ONBOARD_OFFSET_SIZE> short8Dir;
18 CArray<Offset, Offset32::SIZE> short_offsets;
19 CArray<Offset, Offset32::SIZE> short_offsets_not_knight;
20 #ifndef MINIMAL
21 CArray<int,Offset32Wide::SIZE> space_counts;
22 #endif
23 public:
24 static const CArray<Offset, DIRECTION_SIZE> offsets;
25 static const CArray<int, DIRECTION_SIZE> dxs;
26 static const CArray<int, DIRECTION_SIZE> dys;
27 private:
28 template<Direction Dir>
29 void setDirections();
30 template<Direction Dir>
31 void setKnightDirections();
32 void init();
33 public:
37 const Offset getOffsetForBlack(Direction dir) const{
38 return offsets[static_cast<int>(dir)];
39 }
40 int getDxForBlack(Direction dir) const{
41 return dxs[static_cast<int>(dir)];
42 }
43 int getDyForBlack(Direction dir) const{
44 return dys[static_cast<int>(dir)];
45 }
46 template<Player P>
47 const Offset getOffset(Direction dir) const{
48 return getOffsetForBlack(dir)*playerToMul(P);
49 }
50 const Offset getOffset(Player pl,Direction dir) const{
51 if (pl==BLACK)
52 return getOffset<BLACK>(dir);
53 else
54 return getOffset<WHITE>(dir);
55 }
56
61 const Square nextSquare(Player P, Square pos, Direction dr) const
62 {
63 assert(pos.isOnBoard());
64 const Offset offset = getOffset(P, dr);
65 return pos + offset;
66 }
67
68 BoardTable();
70 template <Player P>
71 Direction getLongDirection(Offset32 offset32) const
72 {
73 assert(offset32.isValid());
74 const Offset32 blackOffset32 = offset32.blackOffset32<P>();
75 Direction ret=directions[blackOffset32.index()];
76 assert(isValid(ret));
77 return ret;
78 }
79 Direction getLongDirection(Player P, Offset32 offset32) const
80 {
81 if (P == BLACK)
82 return getLongDirection<BLACK>(offset32);
83 else
84 return getLongDirection<WHITE>(offset32);
85 }
87 template <Player P>
88 Direction getLongDirection(Square from, Square to) const
89 {
90 return getLongDirection<P>(Offset32(to,from));
91 }
92 #ifndef MINIMAL
93
99 int spaceCounts(Square from,Square to) const
100 {
101 Offset32Wide offset32(from,to);
102 return space_counts[offset32.index()];
103 }
104 #endif
105
110 const Offset getShortOffset(Offset32 offset32) const{
111 assert(offset32.isValid());
112 return short_offsets[offset32.index()];
113 }
119 const Offset getShortOffsetNotKnight(Offset32 offset32) const{
120 assert(offset32.isValid());
121 return short_offsets_not_knight[offset32.index()];
122 }
126 Offset getShort8OffsetUnsafe(Square from,Square to) const{
127 int i=(int)(to.uintValue())-(int)(from.uintValue())-Offset::ONBOARD_OFFSET_MIN;
128 return Offset::makeDirect(short8Offset[i]);
129 }
133 template<Player P>
134 Direction getShort8Unsafe(Square from,Square to) const{
135 if(P==BLACK)
136 return static_cast<Direction>(short8Dir[(int)(to.uintValue())-(int)(from.uintValue())-Offset::ONBOARD_OFFSET_MIN]);
137 else
138 return static_cast<Direction>(short8Dir[(int)(from.uintValue())-(int)(to.uintValue())-Offset::ONBOARD_OFFSET_MIN]);
139 }
140 Direction getShort8Unsafe(Player P, Square from,Square to) const{
141 if(P==BLACK)
142 return getShort8Unsafe<BLACK>(from, to);
143 else
144 return getShort8Unsafe<WHITE>(from, to);
145 }
146 template<Player P>
147 Direction getShort8(Square from,Square to) const{
148 assert(from.isOnBoard() && to.isOnBoard());
149 assert(from.x()==to.x() || from.y()==to.y() ||
150 abs(from.x()-to.x())==abs(from.y()-to.y()));
151 return getShort8Unsafe<P>(from,to);
152 }
153
154 template<Player P>
155 Direction getShort8(Square from,Square to,Offset& o) const{
156 assert(from.isOnBoard() && to.isOnBoard());
157 assert(from.x()==to.x() || from.y()==to.y() ||
158 abs(from.x()-to.x())==abs(from.y()-to.y()));
159 int i=(int)(to.uintValue())-(int)(from.uintValue())-Offset::ONBOARD_OFFSET_MIN;
160 o=Offset::makeDirect(short8Offset[i]);
161 Direction d=static_cast<Direction>(short8Dir[i]);
162 if(P==BLACK)
163 return d;
164 else
165 return inverse(d);
166 }
172 bool isBetween(Square t,Square p0,Square p1) const
173 {
174 int i1=(int)(t.uintValue())-(int)(p0.uintValue())-Offset::ONBOARD_OFFSET_MIN;
175 int i2=(int)(p1.uintValue())-(int)(t.uintValue())-Offset::ONBOARD_OFFSET_MIN;
176 assert(short8Dir[i1]!=DIRECTION_INVALID_VALUE || short8Dir[i2]!=DIRECTION_INVALID_VALUE);
177 return short8Dir[i1]==short8Dir[i2];
178 }
179 bool isBetweenSafe(Square t,Square p0,Square p1) const
180 {
181 if (getShortOffsetNotKnight(Offset32(t, p0)).zero())
182 return false;
183 return isBetween(t, p0, p1);
184 }
185 };
186
187 extern const BoardTable Board_Table;
188 } // namespace osl
189
190
191 #endif /* OSL_BOARD_TABLE_H */
192 // ;;; Local Variables:
193 // ;;; mode:c++
194 // ;;; c-basic-offset:2
195 // ;;; End: