-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattack.c
90 lines (78 loc) · 1.83 KB
/
attack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// attack.c
#include "stdio.h"
#include "defs.h"
const int KnDir[8] = { -8, -19, -21, -12, 8, 19, 21, 12 };
const int RkDir[4] = { -1, -10, 1, 10 };
const int BiDir[4] = { -9, -11, 11, 9 };
const int KiDir[8] = { -1, -10, 1, 10, -9, -11, 11, 9 };
int SqAttacked(const int sq, const int side, const S_BOARD *pos) {
int pce,index,t_sq,dir;
ASSERT(SqOnBoard(sq));
ASSERT(SideValid(side));
ASSERT(CheckBoard(pos));
// pawns
if(side == WHITE) {
if(pos->pieces[sq-11] == wP || pos->pieces[sq-9] == wP) {
return TRUE;
}
} else {
if(pos->pieces[sq+11] == bP || pos->pieces[sq+9] == bP) {
return TRUE;
}
}
// knights
for(index = 0; index < 8; ++index) {
pce = pos->pieces[sq + KnDir[index]];
ASSERT(PceValidEmptyOffbrd(pce));
if(pce != OFFBOARD && IsKn(pce) && PieceCol[pce]==side) {
return TRUE;
}
}
// rooks, queens
for(index = 0; index < 4; ++index) {
dir = RkDir[index];
t_sq = sq + dir;
ASSERT(SqIs120(t_sq));
pce = pos->pieces[t_sq];
ASSERT(PceValidEmptyOffbrd(pce));
while(pce != OFFBOARD) {
if(pce != EMPTY) {
if(IsRQ(pce) && PieceCol[pce] == side) {
return TRUE;
}
break;
}
t_sq += dir;
ASSERT(SqIs120(t_sq));
pce = pos->pieces[t_sq];
}
}
// bishops, queens
for(index = 0; index < 4; ++index) {
dir = BiDir[index];
t_sq = sq + dir;
ASSERT(SqIs120(t_sq));
pce = pos->pieces[t_sq];
ASSERT(PceValidEmptyOffbrd(pce));
while(pce != OFFBOARD) {
if(pce != EMPTY) {
if(IsBQ(pce) && PieceCol[pce] == side) {
return TRUE;
}
break;
}
t_sq += dir;
ASSERT(SqIs120(t_sq));
pce = pos->pieces[t_sq];
}
}
// kings
for(index = 0; index < 8; ++index) {
pce = pos->pieces[sq + KiDir[index]];
ASSERT(PceValidEmptyOffbrd(pce));
if(pce != OFFBOARD && IsKi(pce) && PieceCol[pce]==side) {
return TRUE;
}
}
return FALSE;
}