forked from nescitus/cpw-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attacks.cpp
96 lines (77 loc) · 2.14 KB
/
attacks.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
#include "stdafx.h"
#include "0x88_math.h"
int isAttacked( char byColor, S8 sq ) {
/* pawns */
if ( byColor == WHITE && b.pawn_ctrl[WHITE][sq] )
return 1;
if ( byColor == BLACK && b.pawn_ctrl[BLACK][sq])
return 1;
/* knights */
if ( leaperAttack( byColor, sq, KNIGHT ) )
return 1;
/* kings */
if ( leaperAttack( byColor, sq, KING ) )
return 1;
/* straight line sliders */
if ( straightAttack( byColor, sq, NORTH )
|| straightAttack( byColor, sq, SOUTH )
|| straightAttack( byColor, sq, EAST )
|| straightAttack( byColor, sq, WEST ) )
return 1;
/* diagonal sliders */
if ( diagAttack( byColor, sq, NE )
|| diagAttack( byColor, sq, SE )
|| diagAttack( byColor, sq, NW )
|| diagAttack( byColor, sq, SW ) )
return 1;
return 0;
}
int leaperAttack( char byColor, S8 sq, char byPiece ) {
S8 nextSq;
for ( int dir = 0; dir < 8; dir++ ) {
nextSq = sq + vector[byPiece][dir];
if ( IS_SQ(nextSq)
&& isPiece( byColor, byPiece, nextSq ) )
return 1;
}
return 0;
}
int straightAttack(char byColor, S8 sq, int vect) {
int nextSq = sq + vect;
while ( IS_SQ(nextSq) ) {
if (b.color[nextSq] != COLOR_EMPTY ) {
if ( ( b.color[nextSq] == byColor )
&& ( b.pieces[nextSq] == ROOK || b.pieces[nextSq] == QUEEN ) )
return 1;
return 0;
}
nextSq = nextSq + vect;
}
return 0;
}
int diagAttack(int byColor, S8 sq, int vect) {
int nextSq = sq + vect;
while ( IS_SQ( nextSq ) ) {
if (b.color[ nextSq ] != COLOR_EMPTY ) {
if ( ( b.color[nextSq] == byColor )
&& ( b.pieces[nextSq] == BISHOP || b.pieces[nextSq] == QUEEN ) )
return 1;
return 0;
}
nextSq = nextSq + vect;
}
return 0;
}
int bishAttack(int byColor, S8 sq, int vect) {
int nextSq = sq + vect;
while (IS_SQ(nextSq)) {
if (b.color[nextSq] != COLOR_EMPTY) {
if (b.color[nextSq] == byColor
&& b.pieces[nextSq] == BISHOP)
return 1;
return 0;
}
nextSq = nextSq + vect;
}
return 0;
}