Code: Select all
// It used 0x88 board with A1 = 0 and H8 = 119
int is_square_attacked(int from,int side)
{
int to, vector_index, piece;
for(piece = PIECE_PAWN; piece <= PIECE_KING; piece++)
{
if(piece == PIECE_PAWN)
{
to = ((side) ? (from - 15) : (from + 15));
if(IS_SQUARE(to) && is_piece(to,side,piece)) return 1;
to = ((side) ? (from - 17) : (from + 17));
if(IS_SQUARE(to) && is_piece(to,side,piece)) return 1;
}
else
{
for(vector_index = 0; vector_index < 8; vector_index++)
{
if(!vectors[piece][vector_index]) break;
to = from;
while(1)
{
to = to + vectors[piece][vector_index];
if(!IS_SQUARE(to)) break;
if(!is_empty(to))
{
if(is_piece(to,side,piece)) return 1;
break;
}
if(!slide[piece]) break;
}
}
}
}
return 0;
}
Code: Select all
// Some of the attack mask of the pieces
#define WP_MASK 1
#define BP_MASK 2
#define N_MASK 4
#define K_MASK 8
#define B_MASK 16
#define R_MASK 32
#define Q_MASK 64
// attack table data structure
typedef struct {
uint8 may_attack;
int8 step;
} attack_data_t;
//attack table
attack_data_t AttackData_[256];
attack_data_t *AttackData = AttackData_+128;
//attack table is precalculated
static void init_attack_data(void) {
int sq, piece, tosq;
int *ptr;
for(sq=0; sq<256; sq++)
AttackData_[sq].may_attack = AttackData_[sq].step = 0;
for(sq=A1; sq<=H8; sq++)
for(piece=WP; piece<=BK; piece++)
for(ptr = Directions[piece]; *ptr; ptr++)
for(tosq=sq+(*ptr); Board[tosq]!=OUTSIDE; tosq+=(*ptr)) {
AttackData[sq-tosq].step = *ptr;
AttackData[sq-tosq].may_attack |= PieceMask[piece];
if(!SLIDER(piece)) break;
}
}
//the main square_attacked function
int is_attacked(int square, int side) {
int sq, tosq, piece, step;
attack_data_t *a = AttackData-square;
for(sq=KSQ(side); sq!=PL_END; sq=PL_NEXT(sq)) {
if(sq>H8) continue;
piece = Board[sq];
if(PieceMask[piece]&a[sq].may_attack) {
if(!SLIDER(piece)) return 1;
step = a[sq].step;
for(tosq=sq+step; Board[tosq]==EMPTY&&tosq!=square; tosq+=step);
if(tosq==square) return 1;
}
}
return 0;
}
Question1: Why AttackData is offset by 128
Code: Select all
attack_data_t *AttackData = AttackData_+128;
Code: Select all
attack_data_t *a = AttackData-square;
PS: For Now I donot want to use bitboard,I know there are magics bitboard which find attack map very quickly,but i donot want it learn now.They are out of scope of my mind.I had also seen qperft by hgm but i could not understand.