per test
[rattatechess.git] / track_attacks.cpp
blob4ef421ce00f6c6a2491616c25f99ced086b42148
1 /***************************************************************************
2 evaluate.cpp - description
3 -------------------
4 begin : Tue Nov 01 2005
5 copyright : (C) 2005 by Maurizio Monge
6 email : monge@linuz.sns.it
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #include "board.h"
19 #include <string.h>
21 #if TRACK_ATTACKS
23 // 1 2 4 8 16 32 64 128
24 uint8_t Board::attack_dirs[] = { UP, DOWN, LEFT, RIGHT, (uint8_t)(UP+LEFT), (uint8_t)(DOWN+LEFT),
25 (uint8_t)(DOWN+RIGHT), (uint8_t)(UP+RIGHT) };
27 void
28 Board::print_attacks()
30 for(int y=7;y>=0;y--)
31 for(int x=0;x<16;x++)
33 A88 attacks = x >= 8 ? w_attacks : b_attacks;
34 uint8_t p = POS_XY(x%8,y);
35 if(attacks[p] == 0)
36 printf(".");
37 else if(attacks[p] == 8)
38 printf(">");
39 else if(attacks[p] == 4)
40 printf("<");
41 else if(attacks[p] == 1)
42 printf("^");
43 else if(attacks[p] == 2)
44 printf("v");
45 else if(attacks[p] == 3)
46 printf("|");
47 else if(attacks[p] == 12)
48 printf("-");
49 else if(attacks[p] == 16 || attacks[p] == 64)
50 printf("\\");
51 else if(attacks[p] == 32 || attacks[p] == 128)
52 printf("/");
53 else if(!(attacks[p] & 0x0f))
54 printf("x");
55 else if(!(attacks[p] & 0xf0))
56 printf("+");
57 else
58 printf("*");
60 if(x==7)
61 printf(" ");
62 if(x==15)
63 printf("\n");
67 void
68 Board::check_attacks()
70 struct { DEF2_A88(old_b, old_w); } a;
72 for(int i=0;i<8;i++)
73 for(int j=0;j<8;j++)
75 uint8_t p = POS_XY(i,j);
76 a.old_b[p] = b_attacks[p];
77 a.old_w[p] = w_attacks[p];
80 recalc_attacks();
82 for(int i=0;i<8;i++)
83 for(int j=0;j<8;j++)
85 uint8_t p = POS_XY(i,j);
86 ASSERT(a.old_b[p] == b_attacks[p]);
87 ASSERT(a.old_w[p] == w_attacks[p]);
88 (void)(p);
92 void
93 Board::recalc_attacks()
95 zero_a88(b_attacks);
96 zero_a88(w_attacks);
98 for(int i=0;i<mat_tracking[BB_12].count;i++)
99 add_attacks(BB, mat_tracking[BB_12].pos[i]);
100 for(int i=0;i<mat_tracking[BR_12].count;i++)
101 add_attacks(BR, mat_tracking[BR_12].pos[i]);
102 for(int i=0;i<mat_tracking[BQ_12].count;i++)
103 add_attacks(BQ, mat_tracking[BQ_12].pos[i]);
104 for(int i=0;i<mat_tracking[WB_12].count;i++)
105 add_attacks(WB, mat_tracking[WB_12].pos[i]);
106 for(int i=0;i<mat_tracking[WR_12].count;i++)
107 add_attacks(WR, mat_tracking[WR_12].pos[i]);
108 for(int i=0;i<mat_tracking[WQ_12].count;i++)
109 add_attacks(WQ, mat_tracking[WQ_12].pos[i]);
111 //print_attacks();
114 void
115 Board::add_attacks(uint8_t piece, uint8_t where)
117 A88 data = this->data;
119 /* update the attacks passing through this place */
120 for(int is_white = 0; is_white<2; is_white++)
122 A88 attacks = is_white ? w_attacks : b_attacks;
124 if(!attacks[where])
125 continue;
127 for(int d=0;d<8;d++)
129 uint8_t m = 1<<d;
131 if(!(attacks[where] & m))
132 continue;
134 uint8_t inc = attack_dirs[d];
135 uint8_t currpos = where+inc;
136 m = ~m;
138 while(!OUT_OF_BOARD(currpos))
140 /* clear the "attack from this direction" flag */
141 attacks[currpos] &= m;
143 if(data[currpos])
144 break;
146 currpos += inc;
151 A88 attacks = IS_WHITE(piece) ? w_attacks : b_attacks;
153 /* if ROOK-like movements... */
154 if( (PIECE_OF(piece) & ~BISHOP) == ROOK)
156 for(int d=0;d<4;d++)
158 uint8_t m = 1<<d;
159 uint8_t inc = attack_dirs[d];
160 uint8_t currpos = where+inc;
162 while(!OUT_OF_BOARD(currpos))
164 /* set the "attack from this direction" flag */
165 attacks[currpos] |= m;
167 if(data[currpos])
168 break;
170 currpos += inc;
175 /* if BISHOP-like movements... */
176 if( (PIECE_OF(piece) & ~ROOK) == BISHOP)
178 for(int d=4;d<8;d++)
180 uint8_t m = 1<<d;
181 uint8_t inc = attack_dirs[d];
182 uint8_t currpos = where+inc;
184 while(!OUT_OF_BOARD(currpos))
186 /* set the "attack from this direction" flag */
187 attacks[currpos] |= m;
189 if(data[currpos])
190 break;
192 currpos += inc;
198 void
199 Board::del_attacks(uint8_t piece, uint8_t where)
201 A88 data = this->data;
202 A88 attacks = IS_WHITE(piece) ? w_attacks : b_attacks;
204 /* if ROOK-like movements... */
205 if( (PIECE_OF(piece) & ~BISHOP) == ROOK)
207 for(int d=0;d<4;d++)
209 uint8_t m = ~(1<<d);
210 uint8_t inc = attack_dirs[d];
211 uint8_t currpos = where+inc;
213 while(!OUT_OF_BOARD(currpos))
215 /* clear the "attack from this direction" flag */
216 attacks[currpos] &= m;
218 if(data[currpos])
219 break;
221 currpos += inc;
226 /* if BISHOP-like movements... */
227 if( (PIECE_OF(piece) & ~ROOK) == BISHOP)
229 for(int d=4;d<8;d++)
231 uint8_t m = ~(1<<d);
232 uint8_t inc = attack_dirs[d];
233 uint8_t currpos = where+inc;
235 while(!OUT_OF_BOARD(currpos))
237 /* clear the "attack from this direction" flag */
238 attacks[currpos] &= m;
240 if(data[currpos])
241 break;
243 currpos += inc;
248 /* update the attacks passing through this place */
249 for(int is_white = 0; is_white<2; is_white++)
251 A88 attacks = is_white ? w_attacks : b_attacks;
253 if(!attacks[where])
254 continue;
256 for(int d=0;d<8;d++)
258 uint8_t m = 1<<d;
260 if(!(attacks[where] & m))
261 continue;
263 uint8_t inc = attack_dirs[d];
264 uint8_t currpos = where+inc;
266 while(!OUT_OF_BOARD(currpos))
268 /* clear the "attack from this direction" flag */
269 attacks[currpos] |= m;
271 if(data[currpos])
272 break;
274 currpos += inc;
280 void
281 Board::replace_attacks(uint8_t piece1, uint8_t piece2, uint8_t where)
283 A88 attacks = IS_WHITE(piece1) ? w_attacks : b_attacks;
285 /* if ROOK-like movements... */
286 if( (PIECE_OF(piece1) & ~BISHOP) == ROOK)
288 for(int d=0;d<4;d++)
290 uint8_t m = ~(1<<d);
291 uint8_t inc = attack_dirs[d];
292 uint8_t currpos = where+inc;
294 while(!OUT_OF_BOARD(currpos))
296 /* clear the "attack from this direction" flag */
297 attacks[currpos] &= m;
299 if(data[currpos])
300 break;
302 currpos += inc;
307 /* if BISHOP-like movements... */
308 if( (PIECE_OF(piece1) & ~ROOK) == BISHOP)
310 for(int d=4;d<8;d++)
312 uint8_t m = ~(1<<d);
313 uint8_t inc = attack_dirs[d];
314 uint8_t currpos = where+inc;
316 while(!OUT_OF_BOARD(currpos))
318 /* clear the "attack from this direction" flag */
319 attacks[currpos] &= m;
321 if(data[currpos])
322 break;
324 currpos += inc;
330 attacks = IS_WHITE(piece2) ? w_attacks : b_attacks;
332 /* if ROOK-like movements... */
333 if( (PIECE_OF(piece2) & ~BISHOP) == ROOK)
335 for(int d=0;d<4;d++)
337 uint8_t m = 1<<d;
338 uint8_t inc = attack_dirs[d];
339 uint8_t currpos = where+inc;
341 while(!OUT_OF_BOARD(currpos))
343 /* set the "attack from this direction" flag */
344 attacks[currpos] |= m;
346 if(data[currpos])
347 break;
349 currpos += inc;
354 /* if BISHOP-like movements... */
355 if( (PIECE_OF(piece2) & ~ROOK) == BISHOP)
357 for(int d=4;d<8;d++)
359 uint8_t m = 1<<d;
360 uint8_t inc = attack_dirs[d];
361 uint8_t currpos = where+inc;
363 while(!OUT_OF_BOARD(currpos))
365 /* set the "attack from this direction" flag */
366 attacks[currpos] |= m;
368 if(data[currpos])
369 break;
371 currpos += inc;
377 #endif //TRACK_ATTACKS