-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattack.c
78 lines (67 loc) · 2.34 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
#include "include/definitions.h"
#include "include/validate.h"
const int knightDirection[8] = { -8, -19, -21, -12, 8, 19, 21, 12 };
const int rookDirection[4] = { -1, -10, 1, 10 };
const int bishopDirection[4] = { -9, -11, 11, 9 };
const int kingDirection[8] = { -1, -10, 1, 10, -9, -11, 11, 9 };
int isSquareAttacked(const int square, const int boardSide, const board *position) {
int piece;
int index;
int tmpSquare;
int direction;
ASSERT(squareOnBoard(square));
ASSERT(boardSideValid(boardSide));
ASSERT(checkBoard(position));
if(boardSide == WHITE) {
if(position -> pieces[square - 11] == whitePwn || position -> pieces[square - 9] == whitePwn) {
return TRUE;
}
}else {
if(position -> pieces[square + 11] == blackPwn || position -> pieces[square + 9] == blackPwn) {
return TRUE;
}
}
for(index = 0; index < 8; index++) {
piece = position -> pieces[square + knightDirection[index]];
if(piece != OFFBOARD && isKn(piece) && pieceColor[piece] == boardSide) {
return TRUE;
}
}
for(index = 0; index < 4; index++) {
direction = rookDirection[index];
tmpSquare = square + direction;
piece = position -> pieces[tmpSquare];
while(piece != OFFBOARD) {
if(piece != EMPTY_SQR) {
if(isRQ(piece) && pieceColor[piece] == boardSide) {
return TRUE;
}
break;
}
tmpSquare += direction;
piece = position -> pieces[tmpSquare];
}
}
for(index = 0; index < 4; index++) {
direction = bishopDirection[index];
tmpSquare = square + direction;
piece = position -> pieces[tmpSquare];
while(piece != OFFBOARD) {
if(piece != EMPTY_SQR) {
if(isBQ(piece) && pieceColor[piece] == boardSide) {
return TRUE;
}
break;
}
tmpSquare += direction;
piece = position -> pieces[tmpSquare];
}
}
for(index = 0; index < 8; index++) {
piece = position -> pieces[square + kingDirection[index]];
if(piece != OFFBOARD && isKi(piece) && pieceColor[piece] == boardSide) {
return TRUE;
}
}
return FALSE;
}