My Project
effect5x3Table.cc
Go to the documentation of this file.
1/* effect5x3Table.cc
2 */
3
6#include "osl/basic_type.h"
9#include "osl/oslConfig.h"
10
11#include <iostream>
12
15
16namespace osl
17{
18 namespace progress
19 {
20 namespace
21 {
28 int countShort(PtypeO ptypeO,int center_dx, int center_dy){
29 int ret=0;
30 for(int dy=std::max(-8,center_dy-1);
31 dy<=std::min(8,center_dy+1);dy++)
32 for(int dx=std::max(-8,center_dx-2);
33 dx<=std::min(8,center_dx+2);dx++){
34 EffectContent ec=Ptype_Table.getEffect(ptypeO,Offset32(dx,dy));
35 // unBlockableではなくて,ほんとのshortが欲しい
36 if(ec == EffectContent::DIRECT()){
37 ret+=8;
38 }
39 }
40 return ret;
41 }
48 LongEffect calcLong(Direction d,int center_dx, int center_dy){
49 LongEffect ret;
50 ret.minIndex=0;
51 ret.maxIndex=0;
52 int dy_min=std::max(-8,center_dy-1);
53 int dy_max=std::min(8,center_dy+1);
54 int dx_min=std::max(-8,center_dx-2);
55 int dx_max=std::min(8,center_dx+2);
56 int dx=Board_Table.getDxForBlack(d);
57 int dy=Board_Table.getDyForBlack(d);
58 assert(dx!=0 || dy!=0);
59 int i;
60
61 for(i=1;i<9;i++){
62 if(dx_min<=dx*i && dx*i <=dx_max &&
63 dy_min<=dy*i && dy*i <=dy_max) break;
64 }
65 if(i==9) return ret;
66 ret.minIndex=i;
67 for(;i<9;i++){
68 if(!(dx_min<=dx*i && dx*i <=dx_max &&
69 dy_min<=dy*i && dy*i <=dy_max)) break;
70 }
71 ret.maxIndex=i-1;
72 ret.offset=Offset(dx,dy);
73 assert(!ret.offset.zero());
74 return ret;
75 }
82 LongEffect4 calcLong4(PtypeO ptypeO,int center_dx, int center_dy){
83 LongEffect4 ret;
84 int index=0;
85
86 Player pl=getOwner(ptypeO);
87 for(int i=0;i<8;i++){
88 Direction d=static_cast<Direction>(i);
95 if((Ptype_Table.getMoveMask(getPtype(ptypeO))&
96 dirToMask(shortToLong(d)))!=0){
97 assert(index<4);
98 ret[index]=calcLong(longD,center_dx,center_dy);
99 if(!ret[index].offset.zero())index++;
100 }
101 }
102 return ret;
103 }
104 }
105#ifndef MINIMAL
106 std::ostream& operator<<(std::ostream& os,LongEffect const& longEffect){
107 return os << "(" << longEffect.offset << "," << longEffect.minIndex
108 << "," << longEffect.maxIndex << ")";
109 }
110#endif
111 }
112}
113
115{
123 for(int i=PTYPE_PIECE_MIN;i<=PTYPE_MAX;i++){
124 Ptype ptype=static_cast<Ptype>(i);
125 if(unpromote(ptype)!=ptype)
126 onStand[ptype]=onStand[unpromote(ptype)];
127 }
128}
129
130
132{
133 Player pl=BLACK;
134 for(int i=0;i<2;i++,pl=alt(pl)){
135 for(int j=PTYPE_PIECE_MIN;j<=PTYPE_MAX;j++){
136 PtypeO ptypeO=newPtypeO(pl,static_cast<Ptype>(j));
137 for(int dy= -8;dy<=8;dy++)
138 for(int dx= -8;dx<=8;dx++){
139 Offset32 o32(dx,dy);
140 shortEffect[ptypeOIndex(ptypeO)][o32.index()]=
141 countShort(ptypeO,dx,dy);
142 }
143 }
144 }
145}
147{
148 Player pl=BLACK;
149 for(int i=0;i<2;i++,pl=alt(pl)){
150 for(int j=0;j<8;j++){
151 Direction d=static_cast<Direction>(j);
152 for(int dy= -8;dy<=8;dy++)
153 for(int dx= -8;dx<=8;dx++){
154 Offset32 o32(dx,dy);
155 blockEffect[j][o32.index()]=
156 calcLong(d,dx,dy);
157 }
158 }
159 }
160}
162{
163 Player pl=BLACK;
164 for(int i=0;i<2;i++,pl=alt(pl)){
165 for(int j=PTYPE_PIECE_MIN;j<=PTYPE_MAX;j++){
166 PtypeO ptypeO=newPtypeO(pl,static_cast<Ptype>(j));
167 for(int dy= -8;dy<=8;dy++)
168 for(int dx= -8;dx<=8;dx++){
169 Offset32 o32(dx,dy);
170 longEffect[ptypeOIndex(ptypeO)][o32.index()]=
171 calcLong4(ptypeO,dx,dy);
172 }
173 }
174 }
175}
176
178{
179 // 黒の攻める際のテーブルを作成
180 // 玉は白
181 for(int x=1;x<=9;x++)
182 for(int y=1;y<=9;y++){
183 const Square king(x,y);
184 const Square center = Centering5x3::adjustCenter(king);
185 for(int dx=0;dx<5;dx++){
186 for(int dy=0;dy<3;dy++){
187 const Square p(center.x()+dx-2,center.y()+dy-1);
188 int val=16-std::abs(p.x()-king.x());
189 attackEffect[BLACK][king.index()][dx*3+dy]=val;
190 }
191 }
192 }
193 for(int x=1;x<=9;x++)
194 for(int y=1;y<=9;y++){
195 const Square king(x,y);
196 const Square r_king=king.rotate180();
197 for(int dx=0;dx<5;dx++){
198 for(int dy=0;dy<3;dy++){
199 int r_dx=4-dx,r_dy=2-dy;
200 attackEffect[WHITE][king.index()][dx*3+dy]=
201 attackEffect[BLACK][r_king.index()][r_dx*3+r_dy];
202 }
203 }
204 }
205}
207{
208 // 白が守る際のテーブルを作成
209 // 玉は白
210 for(int x=1;x<=9;x++)
211 for(int y=1;y<=9;y++){
212 const Square king(x,y);
213 const Square center = Centering5x3::adjustCenter(king);
214 for(int dx=0;dx<5;dx++){
215 for(int dy=0;dy<3;dy++){
216 const Square p(center.x()+dx-2,center.y()+dy-1);
217 int val=16-std::abs(p.x()-king.x());
218 defenseEffect[WHITE][king.index()][dx*3+dy]=val;
219 }
220 }
221 }
222 for(int x=1;x<=9;x++)
223 for(int y=1;y<=9;y++){
224 const Square king(x,y);
225 const Square r_king=king.rotate180();
226 for(int dx=0;dx<5;dx++){
227 for(int dy=0;dy<3;dy++){
228 int r_dx=4-dx,r_dy=2-dy;
229 defenseEffect[BLACK][king.index()][dx*3+dy]=
230 defenseEffect[WHITE][r_king.index()][r_dx*3+r_dy];
231 }
232 }
233 }
234}
235
237{
238 setupOnStand();
239 setupShortEffect();
240 setupBlockEffect();
241 setupLongEffect();
242 setupAttackEffect();
243 setupDefenseEffect();
244}
245
246/* ------------------------------------------------------------------------- */
247// ;;; Local Variables:
248// ;;; mode:c++
249// ;;; c-basic-offset:2
250// ;;; End:
int getDxForBlack(Direction dir) const
Definition boardTable.h:40
int getDyForBlack(Direction dir) const
Definition boardTable.h:43
Direction getLongDirection(Offset32 offset32) const
Definition boardTable.h:71
static const EffectContent DIRECT()
差が uniqになるような座標の差分.
Definition offset32.h:17
unsigned int index() const
Definition offset32.h:40
const EffectContent getEffect(PtypeO ptypeo, Square from, Square to) const
fromにいるptypeoがtoに利きを持つか?
Definition ptypeTable.h:112
int getMoveMask(Ptype ptype) const
Definition ptypeTable.h:84
unsigned int index() const
Definition basic_type.h:572
int y() const
将棋としてのY座標を返す.
Definition basic_type.h:567
const Square rotate180() const
Definition basic_type.h:613
int x() const
将棋としてのX座標を返す.
Definition basic_type.h:563
Effect5x3の差分計算で使うテーブル.
CArray< unsigned int, PTYPE_SIZE > onStand
static osl::SetUpRegister _initializer([](){ osl::progress::Effect5x3_Table.init();})
std::ostream & operator<<(std::ostream &os, LongEffect const &longEffect)
CArray< LongEffect, 4 > LongEffect4
長い利きの方向は高々4なので,長さ4のCArrayで表現.
Effect5x3Table Effect5x3_Table
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
@ PTYPE_PIECE_MIN
Definition basic_type.h:104
@ PTYPE_MAX
Definition basic_type.h:105
@ ROOK
Definition basic_type.h:100
@ BISHOP
Definition basic_type.h:99
@ PAWN
Definition basic_type.h:95
@ KNIGHT
Definition basic_type.h:97
@ SILVER
Definition basic_type.h:98
@ GOLD
Definition basic_type.h:94
@ LANCE
Definition basic_type.h:96
const PtypeTable Ptype_Table
Definition tables.cc:97
Ptype getPtype(PtypeO ptypeO)
Definition basic_type.h:217
constexpr int dirToMask(Direction dir)
Definition basic_type.h:393
Ptype unpromote(Ptype ptype)
ptypeがpromote後の型の時に,promote前の型を返す. promoteしていない型の時はそのまま返す
Definition basic_type.h:157
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
constexpr int sign(Player player)
Definition basic_type.h:23
Player
Definition basic_type.h:8
@ WHITE
Definition basic_type.h:10
@ BLACK
Definition basic_type.h:9
PtypeO
Player + Ptype [-15, 15] PtypeO の O は Owner の O.
Definition basic_type.h:199
constexpr Direction shortToLong(Direction d)
引数に longDirを与えてはいけない
Definition basic_type.h:388
constexpr Player alt(Player player)
Definition basic_type.h:13
PtypeO newPtypeO(Player player, Ptype ptype)
Definition basic_type.h:211
static const Square adjustCenter(Square src)
5x3領域への長い利きの始まりと終わり. offset - 利きの方向が0の時は利きが無いことを示す minIndex - 利きが領域に入るindex maxIndex - 利きが領域から出る手前のin...