liberty8Table.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
liberty8Table.cc
Go to the documentation of this file.
1 #include "osl/effect/liberty8Table.h"
2 #include "osl/boardTable.h"
3 #include "osl/stl/vector.h"
4 #include <iostream>
5 #include <iomanip>
6 #include <algorithm>
7
8 namespace osl
9 {
10 namespace effect
11 {
15 static bool hasShortMove(Ptype ptype,int dx, int dy,int dx0, int dy0){
16 if (dx<-8 || 8<dx || dy<-8 || 8<dy) return false;
17 // 1マス離れたところにも利きを持ちうるか?
18 const Offset32 long_offset = Offset32(dx,dy);
19 const EffectContent effect
20 =Ptype_Table.getEffect(newPtypeO(BLACK,ptype),
21 long_offset);
22 const Offset offset=Board_Table.getShortOffset(long_offset);
23 if (effect.hasUnblockableEffect()) {
24 assert(! offset.zero());
25 int dx1=dx+offset.dx();
26 int dy1=dy+offset.dy();
27 // shortにして良い
28 if (abs(dx0-dx1)>1 || abs(dy0-dy1)>1) return true;
29 //
30 if (dx1<-8 || 8<dx1 || dy1<-8 || 8<dy1) return true;
31 const EffectContent effect=Ptype_Table.getEffect(newPtypeO(BLACK,ptype),
32 Offset32(dx1,dy1));
33 return ! effect.hasBlockableEffect();
34 }
35 else if (effect.hasBlockableEffect())
36 {
37 assert(! offset.zero());
38 if (dx0==offset.dx() && dy0==offset.dy())
39 return true;
40 }
41 return false;
42 }
48 static bool hasLongMove(Ptype ptype,int dx, int dy,int dx0,int dy0){
49 if (dx<-8 || 8<dx || dy<-8 || 8<dy) return false;
50 if (hasShortMove(ptype,dx,dy,dx0,dy0)) return false;
51 EffectContent effect=Ptype_Table.getEffect(newPtypeO(BLACK,ptype),
52 Offset32(dx,dy));
53 return effect.hasEffect();
54 }
60 static unsigned char shortMaskOf(Ptype ptype,int dx,int dy){
61 unsigned int mask=0xff;
65 if (dx==0 && dy==0)
66 return mask;
67 for (int i=0;i<8;i++){
68 Direction dir=static_cast<Direction>(i);
69 if (hasShortMove(ptype,dx+Board_Table.getDxForBlack(dir),
70 dy+Board_Table.getDyForBlack(dir),
71 dx,dy))
72 mask&= ~(1<<i);
73 }
74 return mask;
75 }
81 static unsigned int directionOf(int dx, int dy){
82 Direction dir=Board_Table.getLongDirection<BLACK>(Offset32(-dx,-dy));
83 assert(isLong(dir));
84 return 1<<(longToShort(dir));
85 }
91 LongEffect8 Liberty8Table::longEffectOf(Ptype ptype,int dx,int dy){
92 assert(Ptype_Table.hasLongMove(ptype));
93 LongEffect8 ret;
97 if (dx==0 && dy==0)
98 return ret;
99 if ((ptype==PROOK || ptype==ROOK) && abs(dx)==1 && abs(dy)==1){
100 LongEffect8 ret1;
101 ret1.setOffset(Offset::ZERO());
102 ret1.setMask(0,directionOf(0,dy));
103 ret1.setMask(1,directionOf(-dx,dy));
104 Offset32 offset32=Offset32(dx,dy);
105 int index=offset32.index();
106 longEffect2[index]=ret1;
107
108 ret.setOffset(Offset(-dx,0));
109 ret.setMask(0,directionOf(dx,0));
110 ret.setMask(1,directionOf(dx,-dy));
111 return ret;
112 }
113 typedef std::pair<Offset,unsigned char> OffsetMask;
114 typedef std::pair<int,OffsetMask> LenOffsetMask;
115 typedef vector<LenOffsetMask> LenOffsetMasks;
116
117 LenOffsetMasks lenOffsetMasks;
118 for (size_t i=0;i<8;i++){
119 Direction dir=static_cast<Direction>(i);
120 int dx1=Board_Table.getDxForBlack(dir);
121 int dy1=Board_Table.getDyForBlack(dir);
122 int dx2=dx+dx1;
123 int dy2=dy+dy1;
124 if (hasLongMove(ptype,dx2,dy2,dx,dy)){
125 // マンハッタン距離
126 int len=abs(dx2)+abs(dy2);
127 Offset offset=Offset(dx1,dy1);
128 unsigned char mask=(1<<i);
129 lenOffsetMasks.push_back(LenOffsetMask(len,OffsetMask(offset,mask)));
130 }
131 }
132 if (lenOffsetMasks.size()>0){
133 std::sort(lenOffsetMasks.begin(),lenOffsetMasks.end());
134 ret.setOffset(lenOffsetMasks[0].second.first);
135 for (size_t i=0;i<lenOffsetMasks.size();i++){
136 ret.setMask(i,lenOffsetMasks[i].second.second);
137 }
138 }
139 return ret;
140 }
141 Liberty8Table::Liberty8Table(){
142 {
143 #ifndef NDEBUG
144 const Offset32 long_offset = Offset32(-1,8);
145 EffectContent const_effect=Ptype_Table.getEffect(newPtypeO(BLACK,ROOK),long_offset);
146 assert(! const_effect.hasEffect());
147 #endif
148
149 assert(Ptype_Table.getEffect(newPtypeO(BLACK,PPAWN),
150 Offset32(Square(7,1), Square(8,1)))
151 == EffectContent::DIRECT());
152 }
153 for (int i=PTYPE_MAX;i>=PTYPE_PIECE_MIN;i--){
154 Ptype ptype=static_cast<Ptype>(i);
158 shortMask[i].fill();
159 for (int dx= -8;dx<=8;dx++){
160 for (int dy= -8;dy<=8;dy++){
161 Offset32 offset32=Offset32(dx,dy);
162 int index=offset32.index();
163 shortMask[i][index]=shortMaskOf(ptype,dx,dy);
164 if (Ptype_Table.hasLongMove(ptype)){
165 longEffect[i][index]=longEffectOf(ptype,dx,dy);
166 }
167 }
168 }
169 }
170 }
171 #ifndef MINIMAL
172 std::ostream& operator<<(std::ostream& os,LongEffect8 const& longEffect){
173 os << "LongEffect(" << longEffect.getOffset() << ",[";
174 for (int i=0;i<3;i++){
175 if (longEffect.getMask(i)==0)break;
176 os << std::setbase(16) << "0x" << longEffect.getMask(i) << std::setbase(10) << ",";
177 }
178 return os << "])";
179 }
180 #endif
181 } // namespace effect
182 } // namespace osl
183 // ;;; Local Variables:
184 // ;;; mode:c++
185 // ;;; c-basic-offset:2
186 // ;;; End: