forked from mprewarski/DiabloChessEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattack.c
281 lines (254 loc) · 7.82 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
Diablo chess engine, Copyright 2006, Marcus Prewarski
This program is free software. It may be copied or modified under the terms
of the GNU General Public License version 2 as published by the Free Software
Foundation. This program is distributed without any warranties whatsoever.
See the file COPYING included with the distribution for details of the GNU general
public license.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <signal.h>
#include <errno.h>
#include <assert.h>
#include "diablo.h"
#include "attack.h"
//
// Is the side in check
//
int in_check(int side)
{
return color_attacks_square(side ^ 1, KSQ(side));
}
//
// Is the move a check.
// This function isn't completely accurate. It fails in at least one case, where a castling
// move gives a check but this is such a rare case I'm not worrying about it for now.
//
// Note - this function was could use cleaning up to be made faster.
//
int isa_check(int move)
{
int ks = KSQ(board.xside);
int t = TO(move);
int f = FROM(move);
int p;
int vec;
int i;
if (move & PROMOTION) {
p = promoted[(move >> 16) & 0xf];
}
else {
p = Square(f);
}
// check from the moved piece
if (attack_opportunity[ks - t].type & threattype[p]) {
// printf("move %x is the right type %x \n", move, threattab[ks][t].type);
switch(p & 7) {
case PAWN:
case KNIGHT:
case KING:
return 1;
case BISHOP:
case ROOK:
case QUEEN:
vec = (int)attack_opportunity[t - ks].vector; // reverse the vector
// printf("vec %d ks %x t %x\n", vec, ks, t);
for (i = t + vec; (Square(i) == EMPTY) || (i == f); i += vec) {
}
if (Square(i) == (KING | (board.xside << 3)))
return 1;
break;
}
}
// discovered check
if (attack_opportunity[ks - f].vector == attack_opportunity[ks - t].vector)
return 0;
if (attack_opportunity[ks - f].type & BISHOP_THREAT) {
vec = attack_opportunity[ks - f].vector;
// printf("vec %d\n", vec);
for (i = ks + vec; (Square(i) == EMPTY) || (i == f); i += vec) {
}
if ((Square(i) == (BISHOP | (board.side << 3))) ||
(Square(i) == (QUEEN | (board.side << 3))))
return 1;
}
if (attack_opportunity[ks - f].type & ROOK_THREAT) {
vec = attack_opportunity[ks - f].vector;
// printf("vec %d\n", vec);
for (i = ks + vec; (Square(i) == EMPTY) || (i == f) ; i += vec) {
}
if ((Square(i) == (ROOK | (board.side << 3))) ||
(Square(i) == (QUEEN | (board.side << 3))))
return 1;
}
return 0;
}
//
// Does the side(color) attack a square?
// for most pieces, use the attack_opportunity table to see if the piece may attack
// the square. For sliding pieces, use the vectore to step from square to the piece.
//
int color_attacks_square(int color, int sq)
{
int from;
int vector;
int i;
for (from = PL_FIRST(KNIGHT | (color << 3)); from < 128; from = PL_NEXT(from)) {
if (attack_opportunity[from - sq].type == KNIGHT_THREAT)
return 1;
}
for (from = PL_FIRST(BISHOP | (color << 3)); from < 128; from = PL_NEXT(from)) {
if (attack_opportunity[sq - from].type & BISHOP_THREAT) {
vector = attack_opportunity[sq - from].vector;
for (i = sq + vector; (Square(i) == EMPTY); i += vector);
if (Square(i) == (BISHOP | (color<<3)))
return 1;
}
}
for (from = PL_FIRST(ROOK | (color << 3)); from < 128; from = PL_NEXT(from)) {
if (attack_opportunity[sq - from].type & ROOK_THREAT) {
vector = attack_opportunity[sq - from].vector;
for (i = sq + vector; (Square(i) == EMPTY); i += vector);
if (Square(i) == (ROOK | (color<<3)))
return 1;
}
}
for (from = PL_FIRST(QUEEN | (color << 3)); from < 128; from = PL_NEXT(from)) {
if (attack_opportunity[sq - from].type & QUEEN_THREAT) {
vector = attack_opportunity[sq - from].vector;
for (i = sq + vector; (Square(i) == EMPTY); i += vector);
if (Square(i) == (QUEEN | (color<<3)))
return 1;
}
}
if (color == BLACK) {
if (Square(sq+15) == BPAWN) return(1);
if (Square(sq+17) == BPAWN) return(1);
}
else {
if (Square(sq-15) == WPAWN) return(1);
if (Square(sq-17) == WPAWN) return(1);
}
from = KSQ(color);
if (distance[abs(from - sq)] <= 1)
return 1;
return 0;
}
unsigned char check_tab[128];
//
// Generates a check table used by the generate_evasions function. And also counts the number of
// checkers.
// If there is only one checker then any non king move to a non zero position in
// the check table is an evasion whether it is a capture or moving a piece in front
// of the check. If there is more than one checker then the king must be moved to
// a non check square.
//
// Note: looking at this code now it seems extremely inefficient. Did I write this before
// had settled on attack tables? Seems like could use the attack table info better
// and use the attack_opportunity table to better fill out the check_tab.
//
int find_checks()
{
int *mvec;
int s, f, i;
int side = board.side;
int xside = board.xside;
int checkers = 0;
memset(check_tab, 0, sizeof(check_tab));
s = KSQ(side);
// knights
for (mvec = knight_move_vec; *mvec; mvec++) {
f = s + *mvec;
if (Square(f) == (KNIGHT | (xside << 3))) {
check_tab[f]++;
checkers++;
}
}
// rooks or queen
for (mvec = rook_move_vec; *mvec; mvec++) {
for (f = s + *mvec; Square(f) == EMPTY; f += *mvec);
if ((Square(f) == (ROOK | (xside << 3))) ||
(Square(f) == (QUEEN | (xside << 3))) ) {
checkers++;
for (i = s + *mvec; i != f; i += *mvec) {
check_tab[i]++;
}
check_tab[i]++;
}
}
// bishops or queens
for (mvec = bishop_move_vec; *mvec; mvec++) {
for (f = s + *mvec; Square(f) == EMPTY; f += *mvec);
if ((Square(f) == (BISHOP | (xside << 3))) ||
(Square(f) == (QUEEN | (xside << 3))) ) {
checkers++;
for (i = s + *mvec; i != f; i += *mvec) {
check_tab[i]++;
}
check_tab[i]++;
}
}
// pawn
if (side == WHITE) {
if (Square(s+15) == BPAWN) {
checkers++;
check_tab[s+15]++;
}
if (Square(s+17) == BPAWN) {
checkers++;
check_tab[s+17]++;
}
}
else {
if (Square(s-15) == WPAWN) {
checkers++;
check_tab[s-15]++;
}
if (Square(s-17) == WPAWN) {
checkers++;
check_tab[s-17]++;
}
}
return checkers;
}
//
//
//
//
int hung_value(int side)
{
int piece;
int value = 0;
int xside = side ^ 1;
int s;
piece = QUEEN | (side << 3);
for (s = PL_FIRST(piece); s < 128; s = PL_NEXT(s)) {
value += SWAP(QUEEN, attack_tab[xside][s], attack_tab[side][s]);
// printf("Queen on square %d value %d \n", s, value);
}
piece = ROOK | (side << 3);
for (s = PL_FIRST(piece); s < 128; s = PL_NEXT(s)) {
value += SWAP(ROOK, attack_tab[xside][s], attack_tab[side][s]);
// printf("Rook on square %d value %d \n", s, value);
}
piece = BISHOP | (side << 3);
for (s = PL_FIRST(piece); s < 128; s = PL_NEXT(s)) {
value += SWAP(BISHOP, attack_tab[xside][s], attack_tab[side][s]);
// printf("Bishop on square %d value %d \n", s, value);
}
piece = KNIGHT | (side << 3);
for (s = PL_FIRST(piece); s < 128; s = PL_NEXT(s)) {
value += SWAP(BISHOP, attack_tab[xside][s], attack_tab[side][s]);
// printf("Knight on square %d value %d \n", s, value);
}
piece = PAWN | (side << 3);
for (s = PL_FIRST(piece); s < 128; s = PL_NEXT(s)) {
value += SWAP(PAWN, attack_tab[xside][s], attack_tab[side][s]);
// printf("Pawn on square %d value %d \n", s, value);
}
return (value*100);
}