-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathseval.c
568 lines (474 loc) · 11.8 KB
/
seval.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
Sjeng - a chess variants playing program
Copyright (C) 2000 Gian-Carlo Pascutto
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
File: seval.c
Purpose: functions for evaluating positions (suicide chess)
*/
#include "sjeng.h"
#include "extvars.h"
#include "protos.h"
static int scentral[144] = {
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,-20,-10,-10,-10,-10,-10,-10,-20,0,0,
0,0,-10,0,3,5,5,3,0,-10,0,0,
0,0,-10,2,15,15,15,15,2,-10,0,0,
0,0,-10,7,15,25,25,15,7,-10,0,0,
0,0,-10,7,15,25,25,15,7,-10,0,0,
0,0,-10,2,15,15,15,15,2,-10,0,0,
0,0,-10,0,3,5,5,3,0,-10,0,0,
0,0,-20,-10,-10,-10,-10,-10,-10,-20,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0};
static const int rook_o[4] = {12, -12, 1, -1};
static const int bishop_o[4] = {11, -11, 13, -13};
static const int knight_o[8] = {10, -10, 14, -14, 23, -23, 25, -25};
static int s_bishop_mobility(int square)
{
register int l;
register int m = 0;
for (l = square-13; board[l] == npiece; l-=13)
m++;
for (l = square-11; board[l] == npiece; l-=11)
m++;
for (l = square+11; board[l] == npiece; l+=11)
m++;
for (l = square+13; board[l] == npiece; l+=13)
m++;
return m << 2;
}
static int s_rook_mobility(int square)
{
register int l;
register int m = 0;
for (l = square-12; board[l] == npiece; l-=12)
m++;
for (l = square-1; board[l] == npiece; l-=1)
m++;
for (l = square+1; board[l] == npiece; l+=1)
m++;
for (l = square+12; board[l] == npiece; l+=12)
m++;
return m << 2;
}
static int s_knight_mobility(int square)
{
static const int knight_o[8] = {10, -10, 14, -14, 23, -23, 25, -25};
register int d, m = 0;
for (d = 0; d < 8; d++)
{
if (board[square + knight_o[d]] == npiece) m++;
}
return m << 2;
}
static int s_pawn_mobility(int square)
{
register int m = 0;
if (board[square] == wpawn)
{
if (board[square + 12] == npiece) m++;
}
else
{
if (board[square - 12] == npiece) m++;
}
return m << 3;
}
static int s_king_mobility(int square)
{
static const int king_o[8] = {13, 12, 11, 1, -1, -11, -12, -13};
register int d, m = 0;
for (d = 0; d < 8; d++)
{
if (board[square + king_o[d]] == npiece) m++;
}
return m << 2;
}
static int black_saccers(int square)
{
register int ndir, a_sq;
register int basq, i;
register int f = FALSE;
/* for white pawn on square, any black
* pieces that can sac themselves to it? */
/* check for case where is_attacked fails
because pawns dont move to their attacks */
if (board[square + 24] == bpawn ||
board[square + 22] == bpawn ||
board[square + 26] == bpawn)
{
return 0;
}
/* ok, now check pawn moves */
if ((rank(square) < 6)
&& (board[square + 25] == bpawn
||
board[square + 23] == bpawn))
{
f = TRUE;
}
else if (rank(square) == 4 &&
(board[square + 35] == bpawn ||
board[square + 37] == bpawn))
{
f = TRUE;
}
if (!f)
{
f = (is_attacked(square + 11, 0) ? 1 : 0);
}
if (!f)
{
f = (is_attacked(square + 13, 0) ? 2 : 0);
}
if (!f)
{
return 0;
}
else
{
/* black can sac, but is the saccer defended ? */
if (f == 1)
{
if (calc_attackers(square + 11, 0) > 1)
{
/* yep */
return 0;
}
else
{
/* nope */
return 30;
}
}
else
{
if (calc_attackers(square + 13, 0) > 1)
{
return 0;
}
else
{
return 30;
}
}
}
}
static int white_saccers(int square)
{
/* for black pawn on square, any black
* pieces that can sac themselves to it? */
register int ndir, a_sq;
register int basq, i;
register int f = FALSE;
/* for white pawn on square, any black
* pieces that can sac themselves to it? */
/* check for case where is_attacked fails
because pawns dont move to their attacks */
if (board[square - 24] == wpawn ||
board[square - 22] == wpawn ||
board[square - 26] == wpawn)
{
return 0;
}
/* ok, now check pawn moves */
if ((rank(square) > 3)
&& (board[square - 25] == wpawn
||
board[square - 23] == wpawn))
{
f = TRUE;
}
else if (rank(square) == 5 &&
(board[square - 35] == wpawn ||
board[square - 37] == wpawn))
{
f = TRUE;
}
if (!f)
{
f = (is_attacked(square - 11, 1) ? 1 : 0);
}
if (!f)
{
f = (is_attacked(square - 13, 1) ? 2 : 0);
}
if (!f)
{
return 0;
}
else
{
/* black can sac, but is the saccer defended ? */
if (f == 1)
{
if (calc_attackers(square - 11, 1) > 1)
{
/* yep */
return 0;
}
else
{
/* nope */
return 30;
}
}
else
{
if (calc_attackers(square - 13, 1) > 1)
{
return 0;
}
else
{
return 30;
}
}
}
}
long int suicide_eval (void) {
/* select the appropriate eval() routine: */
return (suicide_mid_eval ());
}
long int suicide_mid_eval (void) {
/* return a score for the current middlegame position: */
int srank, pawn_file, pawns[2][11], white_back_pawn[11], black_back_pawn[11];
int isolated, backwards, i, a, j;
long int score = 0;
int in_cache;
int wb = 0, bb = 0, wbc = 0, bbc = 0;
int wk = 0, bk = 0, wr = 0, br = 0;
int wn = 0, bn = 0, wp = 0, bp = 0;
in_cache = 0;
checkECache(&score, &in_cache);
if(in_cache)
{
if (white_to_move == 1) return score;
return -score;
}
score = Material;
/* initialize the pawns array, (files offset by one to use dummy files in
order to easier determine isolated status) and also initialize the
arrays keeping track of the rank of the most backward pawn: */
memset (pawns, 0, sizeof (pawns));
for (i = 0; i < 11; i++) {
white_back_pawn[i] = 7;
black_back_pawn[i] = 2;
}
for (j = 1, a = 1; (a <= piece_count); j++) {
i = pieces[j];
if (!i)
continue;
else
a++;
assert((i > 0) && (i < 145));
pawn_file = file (i)+1;
srank = rank (i);
if (board[i] == wpawn) {
pawns[1][pawn_file]++;
if (srank < white_back_pawn[pawn_file]) {
white_back_pawn[pawn_file] = srank;
}
}
else if (board[i] == bpawn) {
pawns[0][pawn_file]++;
if (srank > black_back_pawn[pawn_file]) {
black_back_pawn[pawn_file] = srank;
}
}
}
/* loop through the board, adding material value, as well as positional
bonuses for all pieces encountered: */
for (j = 1, a = 1; (a <= piece_count); j++) {
i = pieces[j];
if (!i)
continue;
else
a++;
pawn_file = file (i)+1;
srank = rank (i);
switch (board[i]) {
case (wpawn):
score += scentral[i];
score += s_pawn_mobility(i);
score -= black_saccers(i);
wp++;
isolated = FALSE;
backwards = FALSE;
/* check for backwards pawns: */
if (white_back_pawn[pawn_file+1] > srank
&& white_back_pawn[pawn_file-1] > srank) {
score -= 8;
backwards = TRUE;
/* check to see if it is furthermore isolated: */
if (!pawns[1][pawn_file+1] && !pawns[1][pawn_file-1]) {
score -= 12;
isolated = TRUE;
}
}
/* give weak, exposed pawns a penalty (not as much as in the midgame,
since there may be no pieces to take advantage of it): */
if (!pawns[0][pawn_file]) {
if (backwards) score -= 5;
if (isolated) score -= 8;
}
/* give doubled, trippled, etc.. pawns a penalty (bigger in the
endgame, since they will become big targets): */
if (pawns[1][pawn_file] > 1)
score -= 15*(pawns[1][pawn_file]-1);
/* give bonuses for passed pawns (bigger in the endgame since passed
pawns are what wins the endgame): */
if (!pawns[0][pawn_file] && srank >= black_back_pawn[pawn_file-1] &&
srank >= black_back_pawn[pawn_file+1]) {
score += 30 + 3*(rank(i)-2);
/* outside passer ? */
if (file(i) == 1 || file(i) == 8)
score += 4 + 2*(rank(i)-2);
/* give an extra bonus if a connected, passed pawn: */
if (!isolated)
{
score += 6;
}
}
/* check for pawn islands */
if (!pawns[1][pawn_file-1])
score -= 20;
break;
case (bpawn):
score -= scentral[i];
score -= s_pawn_mobility(i);
score += white_saccers(i);
isolated = FALSE;
backwards = FALSE;
bp++;
/* in general, bonuses/penalties in the endgame evaluation will be
higher, since pawn structure becomes more important for the
creation of passed pawns */
/* check for backwards pawns: */
if (black_back_pawn[pawn_file+1] < srank
&& black_back_pawn[pawn_file-1] < srank) {
score += 8;
backwards = TRUE;
/* check to see if it is furthermore isolated: */
if (!pawns[0][pawn_file+1] && !pawns[0][pawn_file-1]) {
score += 12;
isolated = TRUE;
}
}
/* give weak, exposed pawns a penalty (not as much as in the midgame,
since there may be no pieces to take advantage of it): */
if (!pawns[1][pawn_file]) {
if (backwards) score += 5;
if (isolated) score += 8;
}
/* give doubled, trippled, etc.. pawns a penalty (bigger in the
endgame, since they will become big targets): */
if (pawns[0][pawn_file] > 1)
score += 15*(pawns[0][pawn_file]-1);
/* give bonuses for passed pawns (bigger in the endgame since passed
pawns are what wins the endgame): */
if (!pawns[1][pawn_file] && srank <= white_back_pawn[pawn_file-1] &&
srank <= white_back_pawn[pawn_file+1]) {
score -= 30 + 3*(7-rank(i));
/* outside passer ? */
if (file(i) == 1 || file(i) == 8)
score -= 4 + 2*(7-rank(i));
/* give an extra bonus if a connected, passed pawn: */
if (!isolated)
{
score -= 6;
}
}
if (!pawns[0][pawn_file-1])
score += 20;
break;
case (wrook):
score += scentral[i];
score += s_rook_mobility(i);
wr++;
break;
case (brook):
score -= scentral[i];
score -= s_rook_mobility(i);
br++;
break;
case (wbishop):
score += scentral[i];
score += s_bishop_mobility(i);
if (wb)
{
if (sqcolor[i] != wbc)
wb = 99;
}
wb++;
wbc = sqcolor[i];
break;
case (bbishop):
score -= scentral[i];
score -= s_bishop_mobility(i);
if (bb)
{
/* two bishops, check for same color */
if (sqcolor[i] != bbc)
bb = 99;
}
bb++;
bbc = sqcolor[i];
break;
case (wknight):
score += scentral[i];
score += s_knight_mobility(i);
wn++;
break;
case (bknight):
score -= scentral[i];
score -= s_knight_mobility(i);
bn++;
break;
case (wqueen):
score += scentral[i];
score += s_rook_mobility(i);
score += s_bishop_mobility(i);
break;
case (bqueen):
score -= scentral[i];
score -= s_rook_mobility(i);
score -= s_bishop_mobility(i);
break;
case (wking):
score += scentral[i] >> 1;
score += s_king_mobility(i);
wk++;
break;
case (bking):
score -= scentral[i] >> 1;
score -= s_king_mobility(i);
bk++;
break;
}
}
/* opposite color bishops */
if ((wb < 99) && (bb < 99) && (wbc != bbc) && (piece_count < 32))
{
score = (int)((float)score * (float)((float)piece_count / 32.0));
}
storeECache(score);
/* adjust for color: */
if (white_to_move == 1) {
return score;
}
else {
return -score;
}
}