-
Notifications
You must be signed in to change notification settings - Fork 0
/
attack.c
150 lines (126 loc) · 3.86 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "ark.h"
// Direction Arrays for different pieces
const int KnDir[] = {+8, +12, +19, +21, -8, -12, -19, -21};
const int RkDir[] = {-1, -10, 1, 10};
const int BiDir[] = {-9, -11, 11, 9};
const int KiDir[] = {-9, -11, 11, 9, -1, -10, 1, 10};
// Checking Arrays for Double Piece Attacking the same square
// const int
// Function to find whether a particular square is attacked by the given side
int isSqAttacked(const int sq, const int side, const S_BOARD *board){
ASSERT(SqOnBoard(sq));
ASSERT(SideValid(side));
ASSERT(CheckBoard(board));
// Pawns
if(side == WHITE){
if(board->pieces[sq-9] == wP || board->pieces[sq-11] == wP)
return TRUE;
}else{
if(board->pieces[sq+9] == bP || board->pieces[sq+11] == bP)
return TRUE;
}
// Knight
for(int i = 0; i < 8; i++){
int piece = board->pieces[sq + KnDir[i]];
if(piece != OFF_BOARD && IsKn(piece) && PieceCol[piece] == side){
return TRUE;
}
}
// King
for(int i = 0; i < 8; i++){
int piece = board->pieces[sq + KiDir[i]];
if(piece != OFF_BOARD && IsKi(piece) && PieceCol[piece] == side){
return TRUE;
}
}
// Rooks and Queens
for(int i = 0; i < 4; i++){
int t_sq = sq;
t_sq += RkDir[i];
int piece = board->pieces[t_sq];
while(piece != OFF_BOARD){
if(piece != EMPTY){
if(IsRQ(piece) && PieceCol[piece] == side){
return TRUE;
}
break;
}
t_sq += RkDir[i];
piece = board->pieces[t_sq];
}
}
// Bishop and Queens
for(int i = 0; i < 4; i++){
int t_sq = sq;
t_sq += BiDir[i];
int piece = board->pieces[t_sq];
while(piece != OFF_BOARD){
if(piece != EMPTY){
if(IsBQ(piece) && PieceCol[piece] == side){
return TRUE;
}
break;
}
t_sq += BiDir[i];
piece = board->pieces[t_sq];
}
}
return FALSE;
}
int isSqDoublePiece(const int gpiece, const int g_sq, const int sq, const S_BOARD *board){
if(gpiece == wP || gpiece == bP || gpiece == wK || gpiece == bK || gpiece == EMPTY){
return FALSE;
}
if(IsKn(gpiece)){
for(int i = 0; i < 8; i++){
int piece = board->pieces[sq + KnDir[i]];
if(piece != OFF_BOARD && IsKn(piece) && PieceCol[piece] == board->side){
return TRUE;
}
}
return FALSE;
}
if(IsRQ(gpiece)){
for(int i = 0; i < 4; i++){
int t_sq = sq;
t_sq += RkDir[i];
int piece = board->pieces[t_sq];
while(piece != OFF_BOARD)
{
if(piece != EMPTY){
if(IsRQ(piece) && PieceCol[piece] == board->side){
if(piece == gpiece && t_sq != g_sq){
return TRUE;
}
}
break;
}
t_sq += RkDir[i];
piece = board->pieces[t_sq];
}
}
return FALSE;
}
if(IsBQ(gpiece)){
for(int i = 0; i < 4; i++){
int t_sq = sq;
t_sq += BiDir[i];
int piece = board->pieces[t_sq];
while(piece != OFF_BOARD)
{
if(piece != EMPTY){
if(IsBQ(piece) && PieceCol[piece] == board->side){
if(piece == gpiece && t_sq != g_sq){
return TRUE;
}
}
break;
}
t_sq += BiDir[i];
piece = board->pieces[t_sq];
}
}
return FALSE;
}
return FALSE;
}