-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathleval.c
427 lines (349 loc) · 9.9 KB
/
leval.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
/*
Sjeng - a chess variants playing program
Copyright (C) 2000-2001 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: leval.c
Purpose: functions for evaluating positions in losers chess
*/
#include "sjeng.h"
#include "extvars.h"
#include "protos.h"
static int lcentral[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,-15,-15,-15,-15,-15,-15,-20,0,0,
0,0,-15,0,3,5,5,3,0,-15,0,0,
0,0,-15,0,15,15,15,15,0,-15,0,0,
0,0,-15,0,15,30,30,15,0,-15,0,0,
0,0,-15,0,15,30,30,15,0,-15,0,0,
0,0,-15,0,15,15,15,15,0,-15,0,0,
0,0,-15,0,3,5,5,3,0,-15,0,0,
0,0,-20,-15,-15,-15,-15,-15,-15,-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 int l_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;
}
static int l_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;
}
static int l_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;
}
static int l_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;
}
static int l_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;
}
long int losers_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;
int i, a, j;
long int score = 0;
int in_cache;
int wp = 0, bp = 0;
int wks, bks;
int wpassp = 0, bpassp = 0;
int wpawns = 0, bpawns = 0;
in_cache = 0;
checkECache(&score, &in_cache);
if(in_cache)
{
if (white_to_move == 1) return score;
return -score;
}
/* 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++;
switch (board[i]) {
case (wpawn):
wp++;
wpawns++;
score += lcentral[i];
score += l_pawn_mobility(i) << 2;
score += (rank(i) - 2) * 8;
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 -= 8*(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 += 25 + 10*(rank(i)-2);
if (rank(i) == 7) score += 50;
wpassp++;
/* 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 += 24;
}
}
/* check for pawn islands */
if (!pawns[1][pawn_file-1])
score -= 5;
break;
case (bpawn):
bp++;
bpawns++;
score -= lcentral[i];
score -= l_pawn_mobility(i) << 2;
score -= (7 - rank(i)) * 8;
isolated = FALSE;
backwards = FALSE;
/* 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 += 8*(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 -= 25 + 10*(7-rank(i));
if (rank(i) == 2) score -= 50;
bpassp++;
/* 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 -= 24;
}
}
if (!pawns[0][pawn_file-1])
score += 5;
break;
case (wrook):
wp++;
score += l_rook_mobility(i) << 2;
score += lcentral[i];
break;
case (brook):
bp++;
score -= l_rook_mobility(i) << 2;
score -= lcentral[i];
break;
case (wbishop):
wp++;
score += l_bishop_mobility(i) << 2;
score += lcentral[i];
break;
case (bbishop):
bp++;
score -= l_bishop_mobility(i) << 2;
score -= lcentral[i];
break;
case (wknight):
wp++;
score += lcentral[i] << 1;
score += l_knight_mobility(i) << 2;
break;
case (bknight):
bp++;
score -= lcentral[i] << 1;
score -= l_knight_mobility(i) << 2;
break;
case (wqueen):
wp++;
score += l_bishop_mobility(i) << 1;
score += l_rook_mobility(i) << 1;
score += lcentral[i];
break;
case (bqueen):
bp++;
score -= l_bishop_mobility(i) << 1;
score -= l_rook_mobility(i) << 1;
score -= lcentral[i];
break;
case (wking):
/* being in center is BAD */
wks = lcentral[i] << 1;
score += l_king_mobility(i);
break;
case (bking):
/* being in center is BAD */
bks = lcentral[i] << 1;
score -= l_king_mobility(i);
break;
}
}
if (wp + bp > 10)
{
score -= wks - bks;
}
if (abs(Material) <= 900)
{
score += Material;
}
else
{
/* one side has a huge advantage, which could
* become problematic */
/* only apply this to self, we assume somebody
* else can handle this just fine */
/* I would swear the colors are reversed here,
* but it works this way and not otherwise :) */
/* disable this if we have passers...else they'll
just sit on last rank */
if (Material > 0 && comp_color == !WHITE && !wpassp)
{
score += 1800 - Material;
}
else if (Material < 0 && comp_color == !BLACK && !bpassp)
{
score += -(1800 + Material);
}
else
{
score += Material;
}
}
if (!wpawns) score += 200;
else if (!bpawns) score -= 200;
if (!wp) score = INF;
else if (!bp) score = -INF;
storeECache(score);
/* adjust for color: */
if (white_to_move == 1) {
return score;
}
else {
return -score;
}
}