-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattack.cpp
103 lines (87 loc) · 2.14 KB
/
attack.cpp
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
#include <iostream>
#include "defs.h"
// Direction Arrays:
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) // side = side that is attacking!
{
ASSERT(SqOnBoard(sq));
ASSERT(SideValid(side));
ASSERT(CheckBoard(pos));
// pawns
if (side == WHITE)
{
if (pos->board[sq - 11] == wP || pos->board[sq - 9] == wP)
{
return TRUE;
}
}
else
{
if (pos->board[sq + 11] == bP || pos->board[sq + 9] == bP)
{
return TRUE;
}
}
// knights
for (int index = 0; index < 8; ++index)
{
int pce = pos->board[sq + KnDir[index]];
if (pce != 120 && IsKn(pce) && PieceCol[pce] == side)
{
return TRUE;
}
}
// rooks, queens
for (int index = 0; index < 4; ++index)
{
int dir = RkDir[index];
int t_sq = sq + dir;
int pce = pos->board[t_sq];
while (pce != 120)
{
if (pce != EMPTY)
{
if (IsRQ(pce) && PieceCol[pce] == side)
{
return TRUE;
}
break;
}
t_sq += dir;
pce = pos->board[t_sq];
}
}
// bishops, queens
for (int index = 0; index < 4; ++index)
{
int dir = BiDir[index];
int t_sq = sq + dir;
int pce = pos->board[t_sq];
while (pce != 120)
{
if (pce != EMPTY)
{
if (IsBQ(pce) && PieceCol[pce] == side)
{
return TRUE;
}
break;
}
t_sq += dir;
pce = pos->board[t_sq];
}
}
// kings
for (int index = 0; index < 8; ++index)
{
int pce = pos->board[sq + KiDir[index]];
if (pce != 120 && IsKi(pce) && PieceCol[pce] == side)
{
return TRUE;
}
}
return FALSE;
}