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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#define _GNU_SOURCE
|
| 2 |
+
|
| 3 |
+
#include <assert.h>
|
| 4 |
+
#include <math.h>
|
| 5 |
+
#include <stdio.h>
|
| 6 |
+
#include <stdlib.h>
|
| 7 |
+
#include <string.h>
|
| 8 |
+
#include <time.h>
|
| 9 |
+
#include <unistd.h>
|
| 10 |
+
|
| 11 |
+
#ifndef _NOEDIT
|
| 12 |
+
#define _NOEDIT (1)
|
| 13 |
+
#endif
|
| 14 |
+
#define _ALLOW_CASTLE (1)
|
| 15 |
+
#define _DEBUG (0)
|
| 16 |
+
#define _GAME_LOST (800)
|
| 17 |
+
#ifndef _MAXINDEX
|
| 18 |
+
#define _MAXINDEX (200)
|
| 19 |
+
#endif
|
| 20 |
+
#define _MAXLEVEL (32)
|
| 21 |
+
#define _FRAMESPERSEC (32)
|
| 22 |
+
#define _NPS (3 << 20)
|
| 23 |
+
#define _SKIPFRAMES (_NPS / _FRAMESPERSEC)
|
| 24 |
+
#define _BRDFILE "start.brd"
|
| 25 |
+
#define _FENFILE "start.fen"
|
| 26 |
+
|
| 27 |
+
#define _ALPHA (-20000) // Adjusted as needed
|
| 28 |
+
#define _BETA (20000)
|
| 29 |
+
#define _OVERDEPTH (1)
|
| 30 |
+
#define _S_DEPTH (4)
|
| 31 |
+
#define _SORT
|
| 32 |
+
#define _PVSEARCH
|
| 33 |
+
#define _SVP
|
| 34 |
+
#define _CAND7
|
| 35 |
+
#undef _CAND250
|
| 36 |
+
#define _CANDCUT (200)
|
| 37 |
+
#undef _Q0BLK // For opening phase, block Queen's moves at node root
|
| 38 |
+
|
| 39 |
+
#ifndef _PIECE_CODES
|
| 40 |
+
#define _PIECE_CODES (1)
|
| 41 |
+
#define _WP (1)
|
| 42 |
+
#define _WN (2)
|
| 43 |
+
#define _WB (3)
|
| 44 |
+
#define _WR (4)
|
| 45 |
+
#define _WQ (5)
|
| 46 |
+
#define _WK (6)
|
| 47 |
+
#define _BP (-1)
|
| 48 |
+
#define _BN (-2)
|
| 49 |
+
#define _BB (-3)
|
| 50 |
+
#define _BR (-4)
|
| 51 |
+
#define _BQ (-5)
|
| 52 |
+
#define _BK (-6)
|
| 53 |
+
#define _WM (7)
|
| 54 |
+
#define _BM (-7)
|
| 55 |
+
#define _UO (0)
|
| 56 |
+
#define _BS (-8)
|
| 57 |
+
#endif
|
| 58 |
+
|
| 59 |
+
typedef signed int s3;
|
| 60 |
+
typedef signed int s4;
|
| 61 |
+
typedef signed int s5;
|
| 62 |
+
typedef signed long long s6;
|
| 63 |
+
typedef unsigned int u3;
|
| 64 |
+
typedef unsigned int u4;
|
| 65 |
+
typedef unsigned int u5;
|
| 66 |
+
typedef unsigned long long u6;
|
| 67 |
+
|
| 68 |
+
typedef u6 NODES;
|
| 69 |
+
typedef double TIME;
|
| 70 |
+
typedef s3 MOVE[6];
|
| 71 |
+
typedef MOVE MOVELIST[_MAXINDEX];
|
| 72 |
+
typedef s3 BOARD[9][8];
|
| 73 |
+
typedef s4 VALUE;
|
| 74 |
+
typedef u4 LEVEL;
|
| 75 |
+
typedef u4 MOVEINDEX;
|
| 76 |
+
|
| 77 |
+
typedef struct {
|
| 78 |
+
int seconds;
|
| 79 |
+
int useconds;
|
| 80 |
+
clock_t clock0;
|
| 81 |
+
clock_t clock1;
|
| 82 |
+
int diffclock;
|
| 83 |
+
} ELAPSED;
|
| 84 |
+
|
| 85 |
+
typedef struct {
|
| 86 |
+
BOARD curr_board;
|
| 87 |
+
BOARD next_board;
|
| 88 |
+
LEVEL bl_len;
|
| 89 |
+
LEVEL depth;
|
| 90 |
+
LEVEL level;
|
| 91 |
+
MOVE best_line[_MAXLEVEL];
|
| 92 |
+
MOVE curr_move;
|
| 93 |
+
MOVEINDEX curr_index;
|
| 94 |
+
MOVEINDEX max_index;
|
| 95 |
+
MOVELIST legal_moves;
|
| 96 |
+
VALUE alpha;
|
| 97 |
+
VALUE best;
|
| 98 |
+
VALUE beta;
|
| 99 |
+
VALUE value;
|
| 100 |
+
VALUE valuelist[_MAXINDEX];
|
| 101 |
+
} TREE;
|
| 102 |
+
|
| 103 |
+
extern ELAPSED elapsed;
|
| 104 |
+
extern LEVEL gdepth;
|
| 105 |
+
extern LEVEL glevel;
|
| 106 |
+
extern TREE *treea;
|
| 107 |
+
extern TREE *treeb;
|
| 108 |
+
extern MOVE best_move;
|
| 109 |
+
extern NODES nodes;
|
| 110 |
+
extern int newpv;
|
| 111 |
+
extern int pvsready;
|
| 112 |
+
extern s4 gmode;
|
| 113 |
+
extern s4 stm;
|
| 114 |
+
|
| 115 |
+
extern void init(ELAPSED *elapsed);
|
| 116 |
+
extern void update(ELAPSED *elapsed);
|
| 117 |
+
extern double dclock(ELAPSED *elapsed);
|
| 118 |
+
extern void addm(s5 y, s5 x, s5 y1, s5 x1, MOVEINDEX *curr_index, MOVELIST movelist);
|
| 119 |
+
extern void addprom(s5 y, s5 x, s5 y1, s5 x1, s5 to, MOVEINDEX *curr_index, MOVELIST movelist);
|
| 120 |
+
extern void analysis(void);
|
| 121 |
+
extern VALUE search(TREE *tree_, LEVEL level, LEVEL depth);
|
| 122 |
+
extern int board_cmp(BOARD src, BOARD dest);
|
| 123 |
+
extern void copy_board(BOARD src, BOARD dest);
|
| 124 |
+
extern void copy_move(MOVE src, MOVE dest);
|
| 125 |
+
extern void castle(BOARD board, s5 y, s5 x, MOVEINDEX *curr_index, MOVELIST movelist);
|
| 126 |
+
extern void warn(const char *msg);
|
| 127 |
+
extern VALUE eval(BOARD board, LEVEL level);
|
| 128 |
+
extern void genP(BOARD board, s5 y, s5 x, MOVEINDEX *curr_index, MOVELIST movelist);
|
| 129 |
+
extern void genN(BOARD board, s5 y, s5 x, MOVEINDEX *curr_index, MOVELIST movelist);
|
| 130 |
+
extern void genB(BOARD board, s5 y, s5 x, MOVEINDEX *curr_index, MOVELIST movelist);
|
| 131 |
+
extern void genR(BOARD board, s5 y, s5 x, MOVEINDEX *curr_index, MOVELIST movelist);
|
| 132 |
+
extern void genQ(BOARD board, s5 y, s5 x, MOVEINDEX *curr_index, MOVELIST movelist);
|
| 133 |
+
extern void genK(BOARD board, s5 y, s5 x, MOVEINDEX *curr_index, MOVELIST movelist, LEVEL depth);
|
| 134 |
+
extern MOVEINDEX gendeep(BOARD board, MOVELIST movelist, LEVEL depth);
|
| 135 |
+
extern MOVEINDEX gen(BOARD board, MOVELIST movelist, LEVEL level);
|
| 136 |
+
extern BOARD *get_init(void);
|
| 137 |
+
extern void load(BOARD start);
|
| 138 |
+
extern s4 in_check(BOARD board);
|
| 139 |
+
extern s4 is_pv(LEVEL level);
|
| 140 |
+
extern void makemove(BOARD src, MOVE move, BOARD dest);
|
| 141 |
+
extern s4 move_cmp(MOVE src, MOVE dest);
|
| 142 |
+
extern void nonslider(BOARD board, s5 y, s5 x, s3 dy, s3 dx, MOVEINDEX *curr_index, MOVELIST movelist);
|
| 143 |
+
extern void show_move(MOVE move, BOARD board, u5 stm, char *buf);
|
| 144 |
+
extern void slider(BOARD board, s5 y, s5 x, s3 dy, s3 dx, MOVEINDEX *curr_index, MOVELIST movelist);
|
| 145 |
+
extern void show_board(BOARD board, FILE *f);
|
| 146 |
+
extern void showCI(VALUE value);
|
| 147 |
+
extern void transpose(BOARD board);
|
| 148 |
+
extern void setup_board(BOARD board);
|
| 149 |
+
extern void parse_fen(BOARD board);
|
| 150 |
+
extern void parse_pgn(void);
|
| 151 |
+
extern void save(BOARD board);
|
| 152 |
+
|
| 153 |
+
const VALUE _ALPHA_DFL = (-20000);
|
| 154 |
+
const VALUE _BETA_DFL = (+20000);
|
| 155 |
+
const VALUE _MAXVALUE = (20000);
|
| 156 |
+
const VALUE _PAWNUNIT = (100);
|
| 157 |
+
const VALUE _THRESHOLD = (15000);
|
| 158 |
+
VALUE _VALUES[6];
|
| 159 |
+
|
| 160 |
+
ELAPSED elapsed;
|
| 161 |
+
LEVEL gdepth;
|
| 162 |
+
LEVEL glevel;
|
| 163 |
+
MOVE best_move;
|
| 164 |
+
NODES nodes;
|
| 165 |
+
TREE *treea;
|
| 166 |
+
TREE *treeb;
|
| 167 |
+
int newpv;
|
| 168 |
+
int pvsready;
|
| 169 |
+
s4 gmode;
|
| 170 |
+
s4 stm;
|
| 171 |
+
|
| 172 |
+
void analysis(void)
|
| 173 |
+
{
|
| 174 |
+
BOARD aux;
|
| 175 |
+
BOARD aux2;
|
| 176 |
+
BOARD start;
|
| 177 |
+
char buf[80];
|
| 178 |
+
LEVEL depth;
|
| 179 |
+
LEVEL i;
|
| 180 |
+
TREE *tree;
|
| 181 |
+
s4 ix = 0;
|
| 182 |
+
#if _NOEDIT == 3
|
| 183 |
+
parse_pgn();
|
| 184 |
+
exit(0);
|
| 185 |
+
#elif _NOEDIT == 2
|
| 186 |
+
parse_fen(start);
|
| 187 |
+
save(start);
|
| 188 |
+
#elif _NOEDIT == 1
|
| 189 |
+
parse_pgn();
|
| 190 |
+
load(start);
|
| 191 |
+
#else
|
| 192 |
+
load(start);
|
| 193 |
+
// setup_board(start);
|
| 194 |
+
// copy_board(*get_init(), start);
|
| 195 |
+
// save(start);
|
| 196 |
+
#endif
|
| 197 |
+
show_board(start, stdout);
|
| 198 |
+
treea = (TREE *) malloc (_MAXLEVEL * sizeof(TREE));
|
| 199 |
+
if (!treea) {
|
| 200 |
+
warn("Out of memory!");
|
| 201 |
+
}
|
| 202 |
+
treeb = (TREE *) malloc(_MAXLEVEL * sizeof(TREE));
|
| 203 |
+
if (!treeb)
|
| 204 |
+
warn("Out of memory");
|
| 205 |
+
init(&elapsed);
|
| 206 |
+
nodes = 0LL;
|
| 207 |
+
pvsready = 0;
|
| 208 |
+
for (depth = _S_DEPTH + 1; depth < _MAXLEVEL; depth++) {
|
| 209 |
+
tree = &treea[0];
|
| 210 |
+
copy_board(start, tree->curr_board);
|
| 211 |
+
tree->level = 0;
|
| 212 |
+
tree->depth = depth + _OVERDEPTH;
|
| 213 |
+
gdepth = tree->depth;
|
| 214 |
+
tree->alpha = _ALPHA;
|
| 215 |
+
tree->beta = _BETA;
|
| 216 |
+
newpv = 0;
|
| 217 |
+
tree->best = search(treea, 0, 1);
|
| 218 |
+
pvsready = 1;
|
| 219 |
+
update(&elapsed);
|
| 220 |
+
double delapsed = dclock(&elapsed);
|
| 221 |
+
copy_board(start, aux);
|
| 222 |
+
fprintf(stdout, "Depth: %u\n", depth);
|
| 223 |
+
fprintf(stdout, "Evaluation: ");
|
| 224 |
+
showCI(tree->best);
|
| 225 |
+
fprintf(stdout, "\nBranching factor: %.2lf\n", pow((double) nodes, (double) 1 / (depth)));
|
| 226 |
+
fprintf(stdout, "Best variation: ");
|
| 227 |
+
for (i = 0; i < tree->bl_len; i++) {
|
| 228 |
+
show_move(tree->best_line[i], aux, (i + stm) % 2, buf);
|
| 229 |
+
makemove(aux, tree->best_line[i], aux2);
|
| 230 |
+
copy_board(aux2, aux);
|
| 231 |
+
fprintf(stdout, "%s ", buf);
|
| 232 |
+
}
|
| 233 |
+
fprintf(stdout, "\n");
|
| 234 |
+
if (tree->bl_len & 1)
|
| 235 |
+
transpose(aux);
|
| 236 |
+
fprintf(stdout, "Elapsed: %.2lf\n", delapsed);
|
| 237 |
+
fprintf(stdout, "NPS: %u\n", (unsigned int) ((double) nodes / delapsed));
|
| 238 |
+
fprintf(stdout, "\n");
|
| 239 |
+
fflush(stdout);
|
| 240 |
+
}
|
| 241 |
+
free(treea);
|
| 242 |
+
free(treeb);
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
VALUE search(TREE *tree_, LEVEL level, LEVEL depth)
|
| 246 |
+
// level means distance from root
|
| 247 |
+
// depth means 1 if treea, 0 if treeb
|
| 248 |
+
{
|
| 249 |
+
BOARD aux;
|
| 250 |
+
BOARD aux2;
|
| 251 |
+
char buf[80];
|
| 252 |
+
LEVEL bl_lev;
|
| 253 |
+
LEVEL i;
|
| 254 |
+
TREE *tree;
|
| 255 |
+
TREE *ntree;
|
| 256 |
+
VALUE value;
|
| 257 |
+
tree = &tree_[level];
|
| 258 |
+
value = eval(tree->curr_board, level);
|
| 259 |
+
if (newpv)
|
| 260 |
+
tree->bl_len = 0;
|
| 261 |
+
if (value < -_THRESHOLD) {
|
| 262 |
+
return (value);
|
| 263 |
+
}
|
| 264 |
+
if (tree->depth == 0) {
|
| 265 |
+
return (value);
|
| 266 |
+
}
|
| 267 |
+
if (tree->depth <= _OVERDEPTH)
|
| 268 |
+
if (value > -(_PAWNUNIT >> 1)) {
|
| 269 |
+
return (value);
|
| 270 |
+
}
|
| 271 |
+
if (depth)
|
| 272 |
+
glevel = level;
|
| 273 |
+
tree->max_index = gen(tree->curr_board, tree->legal_moves, depth);
|
| 274 |
+
if (tree->max_index == 0) {
|
| 275 |
+
return (-_MAXVALUE + level);
|
| 276 |
+
}
|
| 277 |
+
if (newpv)
|
| 278 |
+
tree->bl_len = 1;
|
| 279 |
+
tree->best = -_MAXVALUE;
|
| 280 |
+
for (tree->curr_index = 0; tree->curr_index < tree->max_index; (tree->curr_index)++) {
|
| 281 |
+
ntree = &tree_[level + 1];
|
| 282 |
+
copy_move(tree->legal_moves[tree->curr_index], tree->curr_move);
|
| 283 |
+
makemove(tree->curr_board, tree->curr_move, tree->next_board);
|
| 284 |
+
copy_board(tree->next_board, ntree->curr_board);
|
| 285 |
+
#ifdef _SVP
|
| 286 |
+
if (depth)
|
| 287 |
+
if (level < 1)
|
| 288 |
+
if (tree->curr_index) {
|
| 289 |
+
ntree->level = tree->level + 1;
|
| 290 |
+
ntree->depth = tree->depth - 1;
|
| 291 |
+
ntree->alpha = -(tree->alpha) - 1;
|
| 292 |
+
ntree->beta = -(tree->alpha);
|
| 293 |
+
tree->value = -search(tree_, level + 1, depth);
|
| 294 |
+
if (tree->value <= tree->alpha)
|
| 295 |
+
continue;
|
| 296 |
+
}
|
| 297 |
+
#endif
|
| 298 |
+
ntree->level = tree->level + 1;
|
| 299 |
+
ntree->depth = tree->depth - 1;
|
| 300 |
+
ntree->alpha = -(tree->beta);
|
| 301 |
+
ntree->beta = -(tree->alpha);
|
| 302 |
+
tree->value = -search(tree_, level + 1, depth);
|
| 303 |
+
if (!newpv)
|
| 304 |
+
ntree->bl_len = 0;
|
| 305 |
+
newpv = 1;
|
| 306 |
+
if (tree->value > tree->best) {
|
| 307 |
+
tree->best = tree->value;
|
| 308 |
+
tree->bl_len = ntree->bl_len + 1;
|
| 309 |
+
copy_move(tree->curr_move, tree->best_line[0]);
|
| 310 |
+
if (ntree->bl_len > 0)
|
| 311 |
+
for (bl_lev = 0; bl_lev < ntree->bl_len; bl_lev++)
|
| 312 |
+
copy_move(ntree->best_line[bl_lev], \
|
| 313 |
+
tree->best_line[bl_lev + 1]);
|
| 314 |
+
if (level == 0 && depth == 1 && gmode == 4) {
|
| 315 |
+
update(&elapsed);
|
| 316 |
+
double delapsed = dclock(&elapsed);
|
| 317 |
+
copy_board(treea->curr_board, aux);
|
| 318 |
+
fprintf(stdout, "Depth: %u*\n", treea->depth - _OVERDEPTH);
|
| 319 |
+
fprintf(stdout, "Evaluation: ");
|
| 320 |
+
showCI(treea->best);
|
| 321 |
+
fprintf(stdout, "\nBranching factor: %.2lf\n", pow((double) nodes, (double) 1 / (treea->depth - _OVERDEPTH)));
|
| 322 |
+
fprintf(stdout, "Best variation: ");
|
| 323 |
+
for (i = 0; i < treea->bl_len; i++) {
|
| 324 |
+
show_move(treea->best_line[i], aux, (i + stm) % 2, buf);
|
| 325 |
+
makemove(aux, treea->best_line[i], aux2);
|
| 326 |
+
copy_board(aux2, aux);
|
| 327 |
+
fprintf(stdout, "%s ", buf);
|
| 328 |
+
}
|
| 329 |
+
fprintf(stdout, "\n");
|
| 330 |
+
if (treea->bl_len & 1)
|
| 331 |
+
transpose(aux);
|
| 332 |
+
fprintf(stdout, "Elapsed: %.2lf\n", delapsed);
|
| 333 |
+
fprintf(stdout, "NPS: %u\n", (unsigned int) ((double) nodes / delapsed));
|
| 334 |
+
fprintf(stdout, "\n");
|
| 335 |
+
fflush(stdout);
|
| 336 |
+
}
|
| 337 |
+
if (tree->best > tree->alpha)
|
| 338 |
+
tree->alpha = tree->best;
|
| 339 |
+
if (tree->alpha >= tree->beta)
|
| 340 |
+
return (tree->beta);
|
| 341 |
+
}
|
| 342 |
+
}
|
| 343 |
+
return (tree->best);
|
| 344 |
+
}
|
| 345 |
+
|
| 346 |
+
#define abs(x) ((x > 0) ? (x) : ((-x)))
|
| 347 |
+
#define min(x, y) (((x) < (y)) ? (x) : (y))
|
| 348 |
+
VALUE eval(BOARD board, LEVEL level)
|
| 349 |
+
{
|
| 350 |
+
BOARD aux;
|
| 351 |
+
int ivalue = 0;
|
| 352 |
+
int kings = 0;
|
| 353 |
+
u5 x;
|
| 354 |
+
u5 y;
|
| 355 |
+
VALUE pvalue = 0;
|
| 356 |
+
VALUE value;
|
| 357 |
+
nodes++;
|
| 358 |
+
if ((nodes % _SKIPFRAMES) == 0) {
|
| 359 |
+
update(&elapsed);
|
| 360 |
+
double delapsed = dclock(&elapsed);
|
| 361 |
+
if (delapsed >=21300)
|
| 362 |
+
exit(0);
|
| 363 |
+
}
|
| 364 |
+
for (y = 0; y < 8; y++)
|
| 365 |
+
for (x = 0; x < 8; x++) {
|
| 366 |
+
switch (board[y][x]) {
|
| 367 |
+
case _WP:
|
| 368 |
+
switch (y) {
|
| 369 |
+
case 1:
|
| 370 |
+
case 2:
|
| 371 |
+
case 3:
|
| 372 |
+
ivalue += 100;
|
| 373 |
+
break;
|
| 374 |
+
case 4:
|
| 375 |
+
ivalue += 120;
|
| 376 |
+
break;
|
| 377 |
+
case 5:
|
| 378 |
+
ivalue += 200;
|
| 379 |
+
break;
|
| 380 |
+
case 6:
|
| 381 |
+
ivalue += 400;
|
| 382 |
+
default:;
|
| 383 |
+
}
|
| 384 |
+
break;
|
| 385 |
+
case _WN:
|
| 386 |
+
case _WB:
|
| 387 |
+
case _WR:
|
| 388 |
+
case _WQ:
|
| 389 |
+
ivalue += _VALUES[(u5) board[y][x]];
|
| 390 |
+
break;
|
| 391 |
+
case _BP:
|
| 392 |
+
switch (y) {
|
| 393 |
+
case 6:
|
| 394 |
+
case 5:
|
| 395 |
+
case 4:
|
| 396 |
+
ivalue -= 100;
|
| 397 |
+
break;
|
| 398 |
+
case 3:
|
| 399 |
+
ivalue -= 120;
|
| 400 |
+
break;
|
| 401 |
+
case 2:
|
| 402 |
+
ivalue -= 200;
|
| 403 |
+
break;
|
| 404 |
+
case 1:
|
| 405 |
+
ivalue -= 400;
|
| 406 |
+
break;
|
| 407 |
+
default:;
|
| 408 |
+
}
|
| 409 |
+
break;
|
| 410 |
+
case _BN:
|
| 411 |
+
case _BB:
|
| 412 |
+
case _BR:
|
| 413 |
+
case _BQ:
|
| 414 |
+
ivalue -= _VALUES[(u5) (-board[y][x])];
|
| 415 |
+
break;
|
| 416 |
+
case _WK: kings++; break;
|
| 417 |
+
case _BK: kings--; break;
|
| 418 |
+
default:;
|
| 419 |
+
}
|
| 420 |
+
}
|
| 421 |
+
for (y = 0; y < 8; y++)
|
| 422 |
+
for (x = 0; x < 8; x++) {
|
| 423 |
+
u5 x1 = x;
|
| 424 |
+
u5 y1 = y;
|
| 425 |
+
if (x1 > 3) x1 = 7 - x1;
|
| 426 |
+
if (y1 > 3) y1 = 7 - y1;
|
| 427 |
+
if (board[y][x] < 0)
|
| 428 |
+
pvalue -= (1 + min(x1, y1));
|
| 429 |
+
else if (board[y][x] > 0)
|
| 430 |
+
pvalue += (1 + min(x1, y1));
|
| 431 |
+
}
|
| 432 |
+
if (kings) {
|
| 433 |
+
if (kings > 0)
|
| 434 |
+
return ( _MAXVALUE - level);
|
| 435 |
+
else
|
| 436 |
+
return (-_MAXVALUE + level);
|
| 437 |
+
}
|
| 438 |
+
value = ivalue + pvalue;
|
| 439 |
+
#if 1
|
| 440 |
+
if (treea[level].depth == 1) {
|
| 441 |
+
copy_board(board, aux);
|
| 442 |
+
transpose(aux);
|
| 443 |
+
if (in_check(aux))
|
| 444 |
+
return (_MAXVALUE - (level + 1));
|
| 445 |
+
}
|
| 446 |
+
if (treea[level].depth / 2 == 1) {
|
| 447 |
+
if (in_check(board))
|
| 448 |
+
return (-2000 + value + level);
|
| 449 |
+
if (value > treea[level].alpha) {
|
| 450 |
+
value = value * 10;
|
| 451 |
+
if (value > 1500)
|
| 452 |
+
value = 2000 - level;
|
| 453 |
+
return (value);
|
| 454 |
+
}
|
| 455 |
+
}
|
| 456 |
+
#endif
|
| 457 |
+
value += ((rand() % 7) - 3);
|
| 458 |
+
if (level > 1)
|
| 459 |
+
return (value + (treea[level - 2].max_index - treea[level - 1].max_index));
|
| 460 |
+
return (value);
|
| 461 |
+
}
|
| 462 |
+
|
| 463 |
+
MOVEINDEX gen(BOARD board, MOVELIST movelist, LEVEL depth)
|
| 464 |
+
// depth means 1 if sortable, 0 otherwise
|
| 465 |
+
// FIXME
|
| 466 |
+
{
|
| 467 |
+
MOVEINDEX max_index = gendeep(board, movelist, 1);
|
| 468 |
+
#ifdef _PVSEARCH
|
| 469 |
+
if (pvsready)
|
| 470 |
+
if (depth)
|
| 471 |
+
if (!newpv)
|
| 472 |
+
if (glevel < treea->bl_len) {
|
| 473 |
+
for (LEVEL level = 0; level < glevel; level++)
|
| 474 |
+
if (treea[level].curr_index) {
|
| 475 |
+
printf("Skip level %d\n", level);
|
| 476 |
+
fflush(stdout);
|
| 477 |
+
goto skippvs;
|
| 478 |
+
}
|
| 479 |
+
MOVE move;
|
| 480 |
+
MOVEINDEX curr_index;
|
| 481 |
+
copy_move(treea->best_line[glevel], move);
|
| 482 |
+
for (curr_index = 0; curr_index < max_index; curr_index++)
|
| 483 |
+
if (!move_cmp(move, movelist[curr_index]))
|
| 484 |
+
break;
|
| 485 |
+
copy_move(movelist[0], movelist[curr_index]);
|
| 486 |
+
copy_move(move, movelist[0]);
|
| 487 |
+
return max_index;
|
| 488 |
+
}
|
| 489 |
+
skippvs:
|
| 490 |
+
#endif
|
| 491 |
+
if (!depth)
|
| 492 |
+
return max_index;
|
| 493 |
+
#ifdef _SORT
|
| 494 |
+
if (glevel < gdepth - _S_DEPTH - 1) {
|
| 495 |
+
MOVEINDEX curr_index;
|
| 496 |
+
MOVEINDEX ncurr_index;
|
| 497 |
+
VALUE valuelist[_MAXINDEX];
|
| 498 |
+
for (curr_index = 0; curr_index < max_index; curr_index++) {
|
| 499 |
+
BOARD aux;
|
| 500 |
+
MOVE move;
|
| 501 |
+
copy_move(movelist[curr_index], move);
|
| 502 |
+
makemove(board, move, aux);
|
| 503 |
+
copy_board(aux, treeb[0].curr_board);
|
| 504 |
+
treeb[0].level = 0;
|
| 505 |
+
LEVEL _s_depth = _S_DEPTH;
|
| 506 |
+
treeb[0].depth = _s_depth;
|
| 507 |
+
treeb[0].alpha = _ALPHA_DFL;
|
| 508 |
+
treeb[0].beta = _BETA_DFL;
|
| 509 |
+
valuelist[curr_index] = -search(treeb, 0, 0);
|
| 510 |
+
}
|
| 511 |
+
for (curr_index = 0; curr_index < max_index; curr_index++)
|
| 512 |
+
for (ncurr_index = curr_index + 1; ncurr_index < max_index; ncurr_index++) {
|
| 513 |
+
if (valuelist[ncurr_index] > valuelist[curr_index]) {
|
| 514 |
+
MOVE move;
|
| 515 |
+
VALUE value;
|
| 516 |
+
copy_move(movelist[ncurr_index], move);
|
| 517 |
+
copy_move(movelist[curr_index], movelist[ncurr_index]);
|
| 518 |
+
copy_move(move, movelist[curr_index]);
|
| 519 |
+
value = valuelist[ncurr_index];
|
| 520 |
+
valuelist[ncurr_index] = valuelist[curr_index];
|
| 521 |
+
valuelist[curr_index] = value;
|
| 522 |
+
}
|
| 523 |
+
}
|
| 524 |
+
#ifdef _CAND7
|
| 525 |
+
LEVEL newmax_index = max_index;
|
| 526 |
+
if (glevel)
|
| 527 |
+
newmax_index = 6;
|
| 528 |
+
if (max_index > newmax_index)
|
| 529 |
+
max_index = newmax_index;
|
| 530 |
+
#endif
|
| 531 |
+
#ifdef _CAND250
|
| 532 |
+
if (glevel)
|
| 533 |
+
for (curr_index = 0; curr_index < max_index; curr_index++)
|
| 534 |
+
if (valuelist[curr_index] < valuelist[0] - _CANDCUT) {
|
| 535 |
+
max_index = curr_index;
|
| 536 |
+
break;
|
| 537 |
+
}
|
| 538 |
+
#endif
|
| 539 |
+
}
|
| 540 |
+
#endif
|
| 541 |
+
return max_index;
|
| 542 |
+
}
|
| 543 |
+
#include <stdlib.h>
|
| 544 |
+
|
| 545 |
+
typedef int VALUE;
|
| 546 |
+
|
| 547 |
+
extern void showCI(VALUE value) {
|
| 548 |
+
// Handle zero and near-zero evaluations as equal
|
| 549 |
+
if (value >= -29 && value <= 29) {
|
| 550 |
+
// U+2261 ≡ IDENTICAL TO (equal position)
|
| 551 |
+
printf("\u2261");
|
| 552 |
+
}
|
| 553 |
+
// White advantage ranges
|
| 554 |
+
else if (value >= 30 && value <= 69) {
|
| 555 |
+
// U+2A72 ⩲ PLUS ABOVE EQUALS SIGN (slight advantage for White)
|
| 556 |
+
printf("\u2A72");
|
| 557 |
+
}
|
| 558 |
+
else if (value >= 70 && value <= 149) {
|
| 559 |
+
// U+00B1 ± PLUS-MINUS SIGN (clear advantage for White)
|
| 560 |
+
printf("\u00B1");
|
| 561 |
+
}
|
| 562 |
+
else if (value >= 150 && value <= 20000) {
|
| 563 |
+
// +- (winning advantage for White) - not a single Unicode character
|
| 564 |
+
printf("+-");
|
| 565 |
+
}
|
| 566 |
+
// Black advantage ranges (mirror of white, but with negative values)
|
| 567 |
+
else if (value <= -30 && value >= -69) {
|
| 568 |
+
// U+2A71 ⩱ MINUS ABOVE EQUALS SIGN (slight advantage for Black)
|
| 569 |
+
printf("\u2A71");
|
| 570 |
+
}
|
| 571 |
+
else if (value <= -70 && value >= -149) {
|
| 572 |
+
// U+2213 ∓ MINUS-OR-PLUS SIGN (clear advantage for Black)
|
| 573 |
+
printf("\u2213");
|
| 574 |
+
}
|
| 575 |
+
else if (value <= -150 && value >= -20000) {
|
| 576 |
+
// -+ (winning advantage for Black) - not a single Unicode character
|
| 577 |
+
printf("-+");
|
| 578 |
+
}
|
| 579 |
+
// Handle values outside the expected range
|
| 580 |
+
else if (value > 20000) {
|
| 581 |
+
// Mate in favor of white or extremely large advantage
|
| 582 |
+
printf("+-");
|
| 583 |
+
}
|
| 584 |
+
else if (value < -20000) {
|
| 585 |
+
// Mate in favor of black or extremely large advantage
|
| 586 |
+
printf("-+");
|
| 587 |
+
}
|
| 588 |
+
}
|
| 589 |
+
|
| 590 |
+
void addm(s5 y, s5 x, s5 y1, s5 x1, MOVEINDEX *curr_index, MOVELIST movelist)
|
| 591 |
+
{
|
| 592 |
+
movelist[*curr_index][0] = y;
|
| 593 |
+
movelist[*curr_index][1] = x;
|
| 594 |
+
movelist[*curr_index][2] = y1;
|
| 595 |
+
movelist[*curr_index][3] = x1;
|
| 596 |
+
(*curr_index)++;
|
| 597 |
+
if (*curr_index >= _MAXINDEX)
|
| 598 |
+
warn("Index too big");
|
| 599 |
+
}
|
| 600 |
+
|
| 601 |
+
void addprom(s5 y, s5 x, s5 y1, s5 x1, s5 to, MOVEINDEX *curr_index, MOVELIST movelist)
|
| 602 |
+
{
|
| 603 |
+
movelist[*curr_index][0] = y;
|
| 604 |
+
movelist[*curr_index][1] = x;
|
| 605 |
+
movelist[*curr_index][2] = y1;
|
| 606 |
+
movelist[*curr_index][3] = x1;
|
| 607 |
+
movelist[*curr_index][4] = to;
|
| 608 |
+
(*curr_index)++;
|
| 609 |
+
if (*curr_index >= _MAXINDEX)
|
| 610 |
+
warn("Index too big");
|
| 611 |
+
}
|
| 612 |
+
|
| 613 |
+
/*void castle(BOARD board, s5 y, s5 x, MOVEINDEX *curr_index, MOVELIST movelist)
|
| 614 |
+
{
|
| 615 |
+
BOARD aux;
|
| 616 |
+
if (y != 0) return;
|
| 617 |
+
if (x != 4) return;
|
| 618 |
+
if (board[y][x] != _WK)
|
| 619 |
+
return;
|
| 620 |
+
if (board[0][0] == _WR)
|
| 621 |
+
if (board[0][1] == 0)
|
| 622 |
+
if (board[0][2] == 0)
|
| 623 |
+
if (board[0][3] == 0)
|
| 624 |
+
if (board[8][0] == 1) {
|
| 625 |
+
copy_board(board, aux);
|
| 626 |
+
aux[0][2] = _WK;
|
| 627 |
+
aux[0][3] = _WK;
|
| 628 |
+
if (! in_check(aux))
|
| 629 |
+
addm(0, 4, 0, 2, curr_index, movelist);
|
| 630 |
+
}
|
| 631 |
+
if (board[0][7] == _WR)
|
| 632 |
+
if (board[0][6] == 0)
|
| 633 |
+
if (board[0][5] == 0)
|
| 634 |
+
if (board[8][1] == 1) {
|
| 635 |
+
copy_board(board, aux);
|
| 636 |
+
aux[0][5] = _WK;
|
| 637 |
+
aux[0][6] = _WK;
|
| 638 |
+
if (! in_check(aux))
|
| 639 |
+
addm(0, 4, 0, 6, curr_index, movelist);
|
| 640 |
+
}
|
| 641 |
+
}*/
|
| 642 |
+
|
| 643 |
+
void castle(BOARD board, s5 y, s5 x, MOVEINDEX *curr_index, MOVELIST movelist)
|
| 644 |
+
{
|
| 645 |
+
BOARD aux;
|
| 646 |
+
if (y != 0) return;
|
| 647 |
+
if (x != 4) return;
|
| 648 |
+
if (board[y][x] != _WK)
|
| 649 |
+
return;
|
| 650 |
+
|
| 651 |
+
#if _CHESS960==0
|
| 652 |
+
// Standard chess castling
|
| 653 |
+
if (board[0][0] == _WR)
|
| 654 |
+
if (board[0][1] == 0)
|
| 655 |
+
if (board[0][2] == 0)
|
| 656 |
+
if (board[0][3] == 0)
|
| 657 |
+
if (board[8][0] == 1) {
|
| 658 |
+
copy_board(board, aux);
|
| 659 |
+
aux[0][2] = _WK;
|
| 660 |
+
aux[0][3] = _WK;
|
| 661 |
+
if (! in_check(aux))
|
| 662 |
+
addm(0, 4, 0, 2, curr_index, movelist);
|
| 663 |
+
}
|
| 664 |
+
if (board[0][7] == _WR)
|
| 665 |
+
if (board[0][6] == 0)
|
| 666 |
+
if (board[0][5] == 0)
|
| 667 |
+
if (board[8][1] == 1) {
|
| 668 |
+
copy_board(board, aux);
|
| 669 |
+
aux[0][5] = _WK;
|
| 670 |
+
aux[0][6] = _WK;
|
| 671 |
+
if (! in_check(aux))
|
| 672 |
+
addm(0, 4, 0, 6, curr_index, movelist);
|
| 673 |
+
}
|
| 674 |
+
#else
|
| 675 |
+
// Chess960 castling logic
|
| 676 |
+
s5 i, rook_x;
|
| 677 |
+
|
| 678 |
+
// Queenside castling (left)
|
| 679 |
+
for (rook_x = 0; rook_x < 4; rook_x++) {
|
| 680 |
+
if (board[0][rook_x] == _WR && board[8][0] == 1) {
|
| 681 |
+
// Check if all squares between king and rook are empty
|
| 682 |
+
int empty = 1;
|
| 683 |
+
for (i = rook_x + 1; i < 4; i++) {
|
| 684 |
+
if (board[0][i] != 0) {
|
| 685 |
+
empty = 0;
|
| 686 |
+
break;
|
| 687 |
+
}
|
| 688 |
+
}
|
| 689 |
+
if (empty) {
|
| 690 |
+
copy_board(board, aux);
|
| 691 |
+
aux[0][2] = _WK; // King moves to c1
|
| 692 |
+
aux[0][3] = _WK; // Rook moves to d1
|
| 693 |
+
if (! in_check(aux))
|
| 694 |
+
addm(0, 4, 0, 2, curr_index, movelist);
|
| 695 |
+
}
|
| 696 |
+
}
|
| 697 |
+
}
|
| 698 |
+
|
| 699 |
+
// Kingside castling (right)
|
| 700 |
+
for (rook_x = 5; rook_x < 8; rook_x++) {
|
| 701 |
+
if (board[0][rook_x] == _WR && board[8][1] == 1) {
|
| 702 |
+
// Check if all squares between king and rook are empty
|
| 703 |
+
int empty = 1;
|
| 704 |
+
for (i = 5; i < rook_x; i++) {
|
| 705 |
+
if (board[0][i] != 0) {
|
| 706 |
+
empty = 0;
|
| 707 |
+
break;
|
| 708 |
+
}
|
| 709 |
+
}
|
| 710 |
+
if (empty) {
|
| 711 |
+
copy_board(board, aux);
|
| 712 |
+
aux[0][5] = _WK; // King moves to f1
|
| 713 |
+
aux[0][6] = _WK; // Rook moves to g1
|
| 714 |
+
if (! in_check(aux))
|
| 715 |
+
addm(0, 4, 0, 6, curr_index, movelist);
|
| 716 |
+
}
|
| 717 |
+
}
|
| 718 |
+
}
|
| 719 |
+
#endif
|
| 720 |
+
}
|
| 721 |
+
|
| 722 |
+
void genP(BOARD board, s5 y, s5 x, MOVEINDEX *curr_index, MOVELIST movelist)
|
| 723 |
+
{
|
| 724 |
+
if (board[y + 1][x] == 0) {
|
| 725 |
+
if (y != 6)
|
| 726 |
+
addm(y, x, y + 1, x, curr_index, movelist);
|
| 727 |
+
if (y == 6) {
|
| 728 |
+
s5 to;
|
| 729 |
+
for (to = 5; to >= 2; to--)
|
| 730 |
+
addprom(y, x, y + 1, x, to, curr_index, movelist);
|
| 731 |
+
}
|
| 732 |
+
if (y == 1)
|
| 733 |
+
if (board[y + 2][x] == 0)
|
| 734 |
+
addm(y, x, y + 2, x, curr_index, movelist);
|
| 735 |
+
}
|
| 736 |
+
if (x > 0)
|
| 737 |
+
if (board[y + 1][x - 1] < 0) {
|
| 738 |
+
if (y != 6)
|
| 739 |
+
addm(y, x, y + 1, x - 1, curr_index, movelist);
|
| 740 |
+
else {
|
| 741 |
+
s5 to;
|
| 742 |
+
for (to = 5; to >= 2; to--)
|
| 743 |
+
addprom(y, x, y + 1, x - 1, to, curr_index, movelist);
|
| 744 |
+
}
|
| 745 |
+
}
|
| 746 |
+
if (x < 7)
|
| 747 |
+
if (board[y + 1][x + 1] < 0) {
|
| 748 |
+
if (y != 6)
|
| 749 |
+
addm(y, x, y + 1, x + 1, curr_index, movelist);
|
| 750 |
+
else {
|
| 751 |
+
s5 to;
|
| 752 |
+
for (to = 5; to >= 2; to--)
|
| 753 |
+
addprom(y, x, y + 1, x + 1, to, curr_index, movelist);
|
| 754 |
+
}
|
| 755 |
+
}
|
| 756 |
+
}
|
| 757 |
+
|
| 758 |
+
void genN(BOARD board, s5 y, s5 x, MOVEINDEX *curr_index, MOVELIST movelist)
|
| 759 |
+
{
|
| 760 |
+
nonslider(board, y, x, +2, -1, curr_index, movelist);
|
| 761 |
+
nonslider(board, y, x, +2, +1, curr_index, movelist);
|
| 762 |
+
nonslider(board, y, x, +1, +2, curr_index, movelist);
|
| 763 |
+
nonslider(board, y, x, +1, -2, curr_index, movelist);
|
| 764 |
+
nonslider(board, y, x, -1, +2, curr_index, movelist);
|
| 765 |
+
nonslider(board, y, x, -1, -2, curr_index, movelist);
|
| 766 |
+
nonslider(board, y, x, -2, -1, curr_index, movelist);
|
| 767 |
+
nonslider(board, y, x, -2, +1, curr_index, movelist);
|
| 768 |
+
}
|
| 769 |
+
|
| 770 |
+
void genB(BOARD board, s5 y, s5 x, MOVEINDEX *curr_index, MOVELIST movelist)
|
| 771 |
+
{
|
| 772 |
+
slider(board, y, x, +1, -1, curr_index, movelist);
|
| 773 |
+
slider(board, y, x, +1, +1, curr_index, movelist);
|
| 774 |
+
slider(board, y, x, -1, +1, curr_index, movelist);
|
| 775 |
+
slider(board, y, x, -1, -1, curr_index, movelist);
|
| 776 |
+
}
|
| 777 |
+
|
| 778 |
+
void genR(BOARD board, s5 y, s5 x, MOVEINDEX *curr_index, MOVELIST movelist)
|
| 779 |
+
{
|
| 780 |
+
slider(board, y, x, +1, 0, curr_index, movelist);
|
| 781 |
+
slider(board, y, x, 0, -1, curr_index, movelist);
|
| 782 |
+
slider(board, y, x, 0, +1, curr_index, movelist);
|
| 783 |
+
slider(board, y, x, -1, 0, curr_index, movelist);
|
| 784 |
+
}
|
| 785 |
+
|
| 786 |
+
void genQ(BOARD board, s5 y, s5 x, MOVEINDEX *curr_index, MOVELIST movelist)
|
| 787 |
+
{
|
| 788 |
+
genR(board, y, x, curr_index, movelist);
|
| 789 |
+
genB(board, y, x, curr_index, movelist);
|
| 790 |
+
}
|
| 791 |
+
|
| 792 |
+
void genK(BOARD board, s5 y, s5 x, MOVEINDEX *curr_index, MOVELIST movelist, LEVEL depth)
|
| 793 |
+
{
|
| 794 |
+
nonslider(board, y, x, -1, -1, curr_index, movelist);
|
| 795 |
+
nonslider(board, y, x, -1, 0, curr_index, movelist);
|
| 796 |
+
nonslider(board, y, x, -1, +1, curr_index, movelist);
|
| 797 |
+
nonslider(board, y, x, 0, -1, curr_index, movelist);
|
| 798 |
+
nonslider(board, y, x, 0, +1, curr_index, movelist);
|
| 799 |
+
nonslider(board, y, x, +1, -1, curr_index, movelist);
|
| 800 |
+
nonslider(board, y, x, +1, 0, curr_index, movelist);
|
| 801 |
+
nonslider(board, y, x, +1, +1, curr_index, movelist);
|
| 802 |
+
#ifdef _ALLOW_CASTLE
|
| 803 |
+
if (depth == 1)
|
| 804 |
+
castle(board, y, x, curr_index, movelist);
|
| 805 |
+
#endif
|
| 806 |
+
}
|
| 807 |
+
|
| 808 |
+
MOVEINDEX gendeep(BOARD board, MOVELIST movelist, LEVEL depth)
|
| 809 |
+
{
|
| 810 |
+
MOVEINDEX curr_index = 0;
|
| 811 |
+
s5 x;
|
| 812 |
+
s5 y;
|
| 813 |
+
for (y = 7; y >= 0; y--)
|
| 814 |
+
for (x = 0; x < 8; x++) {
|
| 815 |
+
switch(board[y][x]) {
|
| 816 |
+
case _WP: genP(board, y, x, &curr_index, movelist); break;
|
| 817 |
+
case _WN: genN(board, y, x, &curr_index, movelist); break;
|
| 818 |
+
case _WB: genB(board, y, x, &curr_index, movelist); break;
|
| 819 |
+
case _WR: genR(board, y, x, &curr_index, movelist); break;
|
| 820 |
+
case _WQ:
|
| 821 |
+
#ifdef _Q0BLK
|
| 822 |
+
if (glevel)
|
| 823 |
+
#endif
|
| 824 |
+
genQ(board, y, x, &curr_index, movelist); break;
|
| 825 |
+
case _WK: genK(board, y, x, &curr_index, movelist, depth); break;
|
| 826 |
+
default:;
|
| 827 |
+
}
|
| 828 |
+
}
|
| 829 |
+
return (curr_index);
|
| 830 |
+
}
|
| 831 |
+
|
| 832 |
+
void nonslider(BOARD board, s5 y, s5 x, s3 dy, s3 dx, MOVEINDEX *curr_index, MOVELIST movelist)
|
| 833 |
+
{
|
| 834 |
+
if (y + dy < 0) return;
|
| 835 |
+
if (y + dy > 7) return;
|
| 836 |
+
if (x + dx < 0) return;
|
| 837 |
+
if (x + dx > 7) return;
|
| 838 |
+
if (board[y + dy][x + dx] <= 0)
|
| 839 |
+
addm(y, x, y + dy, x + dx, curr_index, movelist);
|
| 840 |
+
}
|
| 841 |
+
|
| 842 |
+
void slider(BOARD board, s5 y, s5 x, s3 dy, s3 dx, MOVEINDEX *curr_index, MOVELIST movelist)
|
| 843 |
+
{
|
| 844 |
+
s5 y1 = y;
|
| 845 |
+
s5 x1 = x;
|
| 846 |
+
while (1) {
|
| 847 |
+
y1 += dy;
|
| 848 |
+
x1 += dx;
|
| 849 |
+
if (y1 < 0) return;
|
| 850 |
+
if (y1 > 7) return;
|
| 851 |
+
if (x1 < 0) return;
|
| 852 |
+
if (x1 > 7) return;
|
| 853 |
+
if (board[y1][x1] < 0) {
|
| 854 |
+
addm(y, x, y1, x1, curr_index, movelist);
|
| 855 |
+
return;
|
| 856 |
+
}
|
| 857 |
+
if (board[y1][x1] > 0)
|
| 858 |
+
return;
|
| 859 |
+
addm(y, x, y1, x1, curr_index, movelist);
|
| 860 |
+
}
|
| 861 |
+
}
|
| 862 |
+
|
| 863 |
+
void init(ELAPSED *elapsed)
|
| 864 |
+
{
|
| 865 |
+
elapsed->seconds = 0;
|
| 866 |
+
elapsed->useconds = 0;
|
| 867 |
+
elapsed->clock0 = clock();
|
| 868 |
+
elapsed->clock1 = clock();
|
| 869 |
+
elapsed->diffclock = 0;
|
| 870 |
+
}
|
| 871 |
+
|
| 872 |
+
void update(ELAPSED *elapsed)
|
| 873 |
+
{
|
| 874 |
+
elapsed->clock1 = clock();
|
| 875 |
+
elapsed->diffclock = (elapsed->clock1 - elapsed->clock0);
|
| 876 |
+
if (elapsed->diffclock < 0)
|
| 877 |
+
elapsed->diffclock =0;
|
| 878 |
+
if (elapsed->diffclock > 655360000)
|
| 879 |
+
elapsed->diffclock = 0;
|
| 880 |
+
elapsed->useconds += elapsed->diffclock;
|
| 881 |
+
elapsed->seconds += (elapsed->useconds / 1000000);
|
| 882 |
+
elapsed->useconds = elapsed->useconds % 1000000;
|
| 883 |
+
elapsed->clock0 = clock();
|
| 884 |
+
}
|
| 885 |
+
|
| 886 |
+
double dclock(ELAPSED *elapsed)
|
| 887 |
+
{
|
| 888 |
+
return (double) (elapsed->seconds) + (double) (elapsed->useconds) / 1000000.0;
|
| 889 |
+
}
|
| 890 |
+
|
| 891 |
+
int board_cmp(BOARD src, BOARD dest)
|
| 892 |
+
{
|
| 893 |
+
u5 x;
|
| 894 |
+
u5 y;
|
| 895 |
+
for (y = 0; y < 9; y++)
|
| 896 |
+
for (x = 0; x < 8; x++)
|
| 897 |
+
if (dest[y][x] != src[y][x])
|
| 898 |
+
return (1);
|
| 899 |
+
return (0);
|
| 900 |
+
}
|
| 901 |
+
|
| 902 |
+
void copy_board(BOARD src, BOARD dest)
|
| 903 |
+
{
|
| 904 |
+
u5 x;
|
| 905 |
+
u5 y;
|
| 906 |
+
for (y = 0; y < 9; y++)
|
| 907 |
+
for (x = 0; x < 8; x++)
|
| 908 |
+
dest[y][x] = src[y][x];
|
| 909 |
+
}
|
| 910 |
+
|
| 911 |
+
void copy_move(MOVE src, MOVE dest)
|
| 912 |
+
{
|
| 913 |
+
u5 u;
|
| 914 |
+
for (u = 0; u < 6; u++)
|
| 915 |
+
dest[u] = src[u];
|
| 916 |
+
}
|
| 917 |
+
|
| 918 |
+
BOARD *get_init(void)
|
| 919 |
+
{
|
| 920 |
+
static BOARD init = {
|
| 921 |
+
{_WR, _WN, _WB, _WQ, _WK, _WB, _WN, _WR, },
|
| 922 |
+
{_WP, _WP, _WP, _WP, _WP, _WP, _WP, _WP, },
|
| 923 |
+
{_UO, _UO, _UO, _UO, _UO, _UO, _UO, _UO, },
|
| 924 |
+
{_UO, _UO, _UO, _UO, _UO, _UO, _UO, _UO, },
|
| 925 |
+
{_UO, _UO, _UO, _UO, _UO, _UO, _UO, _UO, },
|
| 926 |
+
{_UO, _UO, _UO, _UO, _UO, _UO, _UO, _UO, },
|
| 927 |
+
{_BP, _BP, _BP, _BP, _BP, _BP, _BP, _BP, },
|
| 928 |
+
{_BR, _BN, _BB, _BQ, _BK, _BB, _BN, _BR, },
|
| 929 |
+
{ 1, 1, 1, 1, 0, 0, 0, 0, },
|
| 930 |
+
};
|
| 931 |
+
return (&init);
|
| 932 |
+
}
|
| 933 |
+
|
| 934 |
+
void load(BOARD board)
|
| 935 |
+
{
|
| 936 |
+
FILE *f;
|
| 937 |
+
s5 pp;
|
| 938 |
+
u5 x;
|
| 939 |
+
u5 y;
|
| 940 |
+
f = fopen(_BRDFILE, "r");
|
| 941 |
+
if (!f)
|
| 942 |
+
warn("Cannot open .brd file for read");
|
| 943 |
+
for (y = 8; y > 0; y--)
|
| 944 |
+
for (x = 0; x < 8; x++) {
|
| 945 |
+
fscanf(f, "%d", &pp);
|
| 946 |
+
board[y - 1][x] = (s3) pp;
|
| 947 |
+
}
|
| 948 |
+
for (x = 0; x < 8; x++)
|
| 949 |
+
board[8][x] = (x < 4);
|
| 950 |
+
fscanf(f, "%d", &stm);
|
| 951 |
+
fclose(f);
|
| 952 |
+
show_board(board, stdout);
|
| 953 |
+
if (stm)
|
| 954 |
+
transpose(board);
|
| 955 |
+
}
|
| 956 |
+
|
| 957 |
+
void save(BOARD board)
|
| 958 |
+
{
|
| 959 |
+
FILE *f;
|
| 960 |
+
s5 pp;
|
| 961 |
+
s5 x;
|
| 962 |
+
s5 y;
|
| 963 |
+
f = fopen(_BRDFILE, "w");
|
| 964 |
+
if (!f)
|
| 965 |
+
warn("Cannot open .brd file for write");
|
| 966 |
+
for (y = 8; y > 0; y--) {
|
| 967 |
+
for (x = 0; x < 8; x++) {
|
| 968 |
+
pp = (s5) board[y - 1][x];
|
| 969 |
+
fprintf(f, "%2d ", pp);
|
| 970 |
+
}
|
| 971 |
+
fprintf(f, "\n");
|
| 972 |
+
}
|
| 973 |
+
fprintf(f, "%d\n", stm);
|
| 974 |
+
fflush(stdout);
|
| 975 |
+
fclose(f);
|
| 976 |
+
if (stm)
|
| 977 |
+
transpose(board);
|
| 978 |
+
}
|
| 979 |
+
|
| 980 |
+
s4 in_check(BOARD board)
|
| 981 |
+
{
|
| 982 |
+
BOARD aux;
|
| 983 |
+
MOVE curr_move;
|
| 984 |
+
MOVEINDEX curr_index;
|
| 985 |
+
MOVEINDEX max_index;
|
| 986 |
+
MOVELIST movelist;
|
| 987 |
+
copy_board(board, aux);
|
| 988 |
+
transpose(aux);
|
| 989 |
+
max_index = gendeep(aux, movelist, 0);
|
| 990 |
+
for (curr_index = 0; curr_index < max_index; curr_index++) {
|
| 991 |
+
copy_move(movelist[curr_index], curr_move);
|
| 992 |
+
if (aux[(u5) curr_move[2]][(u5) curr_move[3]] == _BK)
|
| 993 |
+
return (1);
|
| 994 |
+
}
|
| 995 |
+
return (0);
|
| 996 |
+
}
|
| 997 |
+
|
| 998 |
+
void makemove(BOARD src, MOVE move, BOARD dest)
|
| 999 |
+
{
|
| 1000 |
+
copy_board(src, dest);
|
| 1001 |
+
if (dest[(u5) move[0]][(u5) move[1]] == _WK) {
|
| 1002 |
+
if (move[0] == 0)
|
| 1003 |
+
if (move[2] == 0)
|
| 1004 |
+
if (move[1] == 4) {
|
| 1005 |
+
if (move[3] == 2) {
|
| 1006 |
+
dest[0][0] = 0;
|
| 1007 |
+
dest[0][3] = _WR;
|
| 1008 |
+
}
|
| 1009 |
+
if (move[3] == 6) {
|
| 1010 |
+
dest[0][7] = 0;
|
| 1011 |
+
dest[0][5] = _WR;
|
| 1012 |
+
}
|
| 1013 |
+
}
|
| 1014 |
+
dest[8][0] = 0;
|
| 1015 |
+
dest[8][1] = 0;
|
| 1016 |
+
}
|
| 1017 |
+
if (dest[(u5) move[0]][(u5) move[1]] == _WR)
|
| 1018 |
+
if (move[0] == 0) {
|
| 1019 |
+
if (move[1] == 0)
|
| 1020 |
+
dest[8][0] = 0;
|
| 1021 |
+
if (move[1] == 7)
|
| 1022 |
+
dest[8][1] = 0;
|
| 1023 |
+
}
|
| 1024 |
+
if (dest[(u5) move[0]][(u5) move[1]] == _WP)
|
| 1025 |
+
if (move[0] == 6) {
|
| 1026 |
+
dest[(u5) move[2]][(u5) move[3]] = move[4] ? (u5) move[4] : _WQ;
|
| 1027 |
+
dest[(u5) move[0]][(u5) move[1]] = 0;
|
| 1028 |
+
transpose(dest);
|
| 1029 |
+
goto end;
|
| 1030 |
+
}
|
| 1031 |
+
if (dest[(u5) move[0]][(u5) move[1]] == _WP)
|
| 1032 |
+
if (move[0] == 4)
|
| 1033 |
+
if (move[1] != move[3])
|
| 1034 |
+
if (dest[(u5) move[0]][(u5) move[3]] == _BP)
|
| 1035 |
+
dest[(u5) move[0]][(u5) move[3]] = 0;
|
| 1036 |
+
dest[(u5) move[2]][(u5) move[3]] = dest[(u5) move[0]][(u5) move[1]];
|
| 1037 |
+
dest[(u5) move[0]][(u5) move[1]] = 0;
|
| 1038 |
+
transpose(dest);
|
| 1039 |
+
end: ;
|
| 1040 |
+
}
|
| 1041 |
+
|
| 1042 |
+
s4 move_cmp(MOVE src, MOVE dest)
|
| 1043 |
+
{
|
| 1044 |
+
u5 u;
|
| 1045 |
+
for (u = 0; u < 4; u++)
|
| 1046 |
+
if (src[u] != dest[u])
|
| 1047 |
+
return (u + 1);
|
| 1048 |
+
return (0);
|
| 1049 |
+
}
|
| 1050 |
+
|
| 1051 |
+
void show_move(MOVE move, BOARD board, u5 stm, char *buf)
|
| 1052 |
+
/*
|
| 1053 |
+
* FIXME
|
| 1054 |
+
* What about promotions?!
|
| 1055 |
+
*/
|
| 1056 |
+
{
|
| 1057 |
+
BOARD aux;
|
| 1058 |
+
char *p;
|
| 1059 |
+
p = buf;
|
| 1060 |
+
switch (board[(u5) move[0]][(u5) move[1]]) {
|
| 1061 |
+
case 2: p += sprintf(p, "♘"); break;
|
| 1062 |
+
case 3: p += sprintf(p, "♗"); break;
|
| 1063 |
+
case 4: p += sprintf(p, "♖"); break;
|
| 1064 |
+
case 5: p += sprintf(p, "♕"); break;
|
| 1065 |
+
case 6: if ((move[1] == 4) && ((move[3] == 6) || (move[3] == 2))) {
|
| 1066 |
+
switch (move[3]) {
|
| 1067 |
+
case 6: p += sprintf(p, "O-O"); break;
|
| 1068 |
+
case 2: p += sprintf(p, "O-O-O"); break;
|
| 1069 |
+
default:;
|
| 1070 |
+
}
|
| 1071 |
+
return;
|
| 1072 |
+
} else p += sprintf(p, "♔"); break;
|
| 1073 |
+
default:;
|
| 1074 |
+
}
|
| 1075 |
+
p += sprintf(p, "%c", 97 + move[1]);
|
| 1076 |
+
if (stm)
|
| 1077 |
+
p += sprintf(p, "%d", 9 - (move[0] + 1));
|
| 1078 |
+
else
|
| 1079 |
+
p += sprintf(p, "%d", move[0] + 1);
|
| 1080 |
+
if (((board[(u5) move[0]][(u5) move[1]] == 1) && \
|
| 1081 |
+
(move[1] != move[3])) || \
|
| 1082 |
+
((board[(u5) move[0]][(u5) move[1]] > 1) && \
|
| 1083 |
+
(board[(u5) move[2]][(u5) move[3]] < 0)))
|
| 1084 |
+
p += sprintf(p, "x");
|
| 1085 |
+
p += sprintf(p, "%c", 97 + move[3]);
|
| 1086 |
+
if (stm)
|
| 1087 |
+
p += sprintf(p, "%d", 9 - (move[2] + 1));
|
| 1088 |
+
else
|
| 1089 |
+
p += sprintf(p, "%d", move[2] + 1);
|
| 1090 |
+
makemove(board, move, aux);
|
| 1091 |
+
if (in_check(aux))
|
| 1092 |
+
p += sprintf(p, "+");
|
| 1093 |
+
(*p) = 0;
|
| 1094 |
+
}
|
| 1095 |
+
|
| 1096 |
+
void show_board(BOARD board, FILE *f)
|
| 1097 |
+
{
|
| 1098 |
+
s3 piece_symbol[80];
|
| 1099 |
+
wchar_t c;
|
| 1100 |
+
s3 s;
|
| 1101 |
+
s3 t;
|
| 1102 |
+
u5 x;
|
| 1103 |
+
u5 y;
|
| 1104 |
+
for (y = 0; y < 8; y++) {
|
| 1105 |
+
fprintf(f, "%d", (int) (8 - y));
|
| 1106 |
+
for (x = 0; x < 8; x++) {
|
| 1107 |
+
t = board[7 - y][x];
|
| 1108 |
+
s5 bg = 4 - 3 * ((x ^ y) & 1);
|
| 1109 |
+
s5 fg = 7 * (t > 0);
|
| 1110 |
+
switch (t) {
|
| 1111 |
+
case 0: fprintf(f, " . "); break;
|
| 1112 |
+
case 1: fprintf(f, " P "); break;
|
| 1113 |
+
case 2: fprintf(f, " N "); break;
|
| 1114 |
+
case 3: fprintf(f, " B "); break;
|
| 1115 |
+
case 4: fprintf(f, " R "); break;
|
| 1116 |
+
case 5: fprintf(f, " Q "); break;
|
| 1117 |
+
case 6: fprintf(f, " K "); break;
|
| 1118 |
+
case -1: fprintf(f, " p "); break;
|
| 1119 |
+
case -2: fprintf(f, " n "); break;
|
| 1120 |
+
case -3: fprintf(f, " b "); break;
|
| 1121 |
+
case -4: fprintf(f, " r "); break;
|
| 1122 |
+
case -5: fprintf(f, " q "); break;
|
| 1123 |
+
case -6: fprintf(f, " k "); break;
|
| 1124 |
+
default: warn("Wrong piece code");
|
| 1125 |
+
}
|
| 1126 |
+
}
|
| 1127 |
+
fprintf(f, "\n");
|
| 1128 |
+
}
|
| 1129 |
+
fprintf(f, " a b c d e f g h\n");
|
| 1130 |
+
fprintf(f, "\n");
|
| 1131 |
+
fflush(f);
|
| 1132 |
+
}
|
| 1133 |
+
|
| 1134 |
+
void transpose(BOARD board)
|
| 1135 |
+
{
|
| 1136 |
+
s3 t;
|
| 1137 |
+
u5 x;
|
| 1138 |
+
u5 y;
|
| 1139 |
+
for (y = 0; y < 4; y++)
|
| 1140 |
+
for (x = 0; x < 8; x++) {
|
| 1141 |
+
t = board[y][x];
|
| 1142 |
+
board[y][x] = board[7 - y][x];
|
| 1143 |
+
board[7 - y][x] = t;
|
| 1144 |
+
}
|
| 1145 |
+
for (y = 0; y < 8; y++)
|
| 1146 |
+
for (x = 0; x < 8; x++)
|
| 1147 |
+
board[y][x] = -board[y][x];
|
| 1148 |
+
t = board[8][0];
|
| 1149 |
+
board[8][0] = board[8][2];
|
| 1150 |
+
board[8][2] = t;
|
| 1151 |
+
t = board[8][1];
|
| 1152 |
+
board[8][1] = board[8][3];
|
| 1153 |
+
board[8][3] = t;
|
| 1154 |
+
}
|
| 1155 |
+
|
| 1156 |
+
void setup_board(BOARD board)
|
| 1157 |
+
{
|
| 1158 |
+
s5 x = 3;
|
| 1159 |
+
s5 y = 3;
|
| 1160 |
+
unsigned int symbol = 0;
|
| 1161 |
+
parse_fen(board);
|
| 1162 |
+
while (1) {
|
| 1163 |
+
printf("\033[2J");
|
| 1164 |
+
printf("\033[1;1H");
|
| 1165 |
+
show_board(board, stdout);
|
| 1166 |
+
system("cat start.fen");
|
| 1167 |
+
printf("enter square %d %c%d\n",
|
| 1168 |
+
stm, x + 97, y + 1);
|
| 1169 |
+
fflush(stdout);
|
| 1170 |
+
scanf("%u", &symbol);
|
| 1171 |
+
fflush(stdin);
|
| 1172 |
+
switch(symbol) {
|
| 1173 |
+
case 4:
|
| 1174 |
+
return;
|
| 1175 |
+
break;
|
| 1176 |
+
case 20: board[y][x] = 0; break;
|
| 1177 |
+
case 31:
|
| 1178 |
+
case 32:
|
| 1179 |
+
case 33:
|
| 1180 |
+
case 34:
|
| 1181 |
+
case 35:
|
| 1182 |
+
case 36:
|
| 1183 |
+
board[y][x] = symbol - 30;
|
| 1184 |
+
break;
|
| 1185 |
+
case 41:
|
| 1186 |
+
case 42:
|
| 1187 |
+
case 43:
|
| 1188 |
+
case 44:
|
| 1189 |
+
case 45:
|
| 1190 |
+
case 46:
|
| 1191 |
+
board[y][x] = 40 - symbol;
|
| 1192 |
+
break;
|
| 1193 |
+
case 51:
|
| 1194 |
+
case 52:
|
| 1195 |
+
case 53:
|
| 1196 |
+
case 54:
|
| 1197 |
+
case 55:
|
| 1198 |
+
case 56:
|
| 1199 |
+
case 57:
|
| 1200 |
+
case 58:
|
| 1201 |
+
x = symbol - 51;
|
| 1202 |
+
break;
|
| 1203 |
+
case 61:
|
| 1204 |
+
case 62:
|
| 1205 |
+
case 63:
|
| 1206 |
+
case 64:
|
| 1207 |
+
case 65:
|
| 1208 |
+
case 66:
|
| 1209 |
+
case 67:
|
| 1210 |
+
case 68:
|
| 1211 |
+
y = symbol - 61;
|
| 1212 |
+
break;
|
| 1213 |
+
case 70:
|
| 1214 |
+
copy_board(*get_init(), board);
|
| 1215 |
+
break;
|
| 1216 |
+
case 71:
|
| 1217 |
+
load(board);
|
| 1218 |
+
break;
|
| 1219 |
+
case 72:
|
| 1220 |
+
save(board);
|
| 1221 |
+
break;
|
| 1222 |
+
case 73:
|
| 1223 |
+
stm = 1 - stm;
|
| 1224 |
+
board[8][4] = stm;
|
| 1225 |
+
break;
|
| 1226 |
+
case 74:
|
| 1227 |
+
exit(0);
|
| 1228 |
+
break;
|
| 1229 |
+
case 75:
|
| 1230 |
+
transpose(board);
|
| 1231 |
+
break;
|
| 1232 |
+
case 76:
|
| 1233 |
+
parse_fen(board);
|
| 1234 |
+
break;
|
| 1235 |
+
default:;
|
| 1236 |
+
}
|
| 1237 |
+
}
|
| 1238 |
+
}
|
| 1239 |
+
|
| 1240 |
+
void load_values(void)
|
| 1241 |
+
{
|
| 1242 |
+
FILE *vf = fopen("start.bpf", "r");
|
| 1243 |
+
if (!vf)
|
| 1244 |
+
exit(1);
|
| 1245 |
+
for (int i = 1; i < 6; i++) {
|
| 1246 |
+
int j;
|
| 1247 |
+
fscanf(vf, "%d", &j);
|
| 1248 |
+
if (j != i)
|
| 1249 |
+
exit(0);
|
| 1250 |
+
fscanf(vf, "%d", &_VALUES[i]);
|
| 1251 |
+
}
|
| 1252 |
+
_VALUES[0] = 0;
|
| 1253 |
+
fclose(vf);
|
| 1254 |
+
}
|
| 1255 |
+
|
| 1256 |
+
int main(int argc, char *argv[])
|
| 1257 |
+
{
|
| 1258 |
+
gmode = 4;
|
| 1259 |
+
srand(time(NULL));
|
| 1260 |
+
load_values();
|
| 1261 |
+
analysis();
|
| 1262 |
+
return (0);
|
| 1263 |
+
}
|
| 1264 |
+
|
| 1265 |
+
void warn(const char *msg)
|
| 1266 |
+
{
|
| 1267 |
+
fprintf(stderr, "\nwarn %s\n", msg);
|
| 1268 |
+
}
|
| 1269 |
+
|
| 1270 |
+
void parse_fen(BOARD board) {
|
| 1271 |
+
FILE *f;
|
| 1272 |
+
int x = 1;
|
| 1273 |
+
int y = 8;
|
| 1274 |
+
int ch;
|
| 1275 |
+
int pp;
|
| 1276 |
+
f = fopen("start.fen", "r");
|
| 1277 |
+
if (!f)
|
| 1278 |
+
warn("warn in parse_fen"); // Make sure 'warn' is defined
|
| 1279 |
+
while ((ch = fgetc(f)) != EOF && ch != ' ') {
|
| 1280 |
+
pp = 0;
|
| 1281 |
+
if ((ch >= '1') && (ch <= '8')) {
|
| 1282 |
+
while (ch > '0') {
|
| 1283 |
+
board[y - 1][x - 1] = 0;
|
| 1284 |
+
x++;
|
| 1285 |
+
ch--;
|
| 1286 |
+
}
|
| 1287 |
+
if (x == 9) {
|
| 1288 |
+
x = 1;
|
| 1289 |
+
y--;
|
| 1290 |
+
if (!y) {
|
| 1291 |
+
ch = fgetc(f);
|
| 1292 |
+
goto end;
|
| 1293 |
+
}
|
| 1294 |
+
continue;
|
| 1295 |
+
}
|
| 1296 |
+
} else if (ch != '/') {
|
| 1297 |
+
switch (ch) {
|
| 1298 |
+
case 'p': pp = -1; break;
|
| 1299 |
+
case 'n': pp = -2; break;
|
| 1300 |
+
case 'b': pp = -3; break;
|
| 1301 |
+
case 'r': pp = -4; break;
|
| 1302 |
+
case 'q': pp = -5; break;
|
| 1303 |
+
case 'k': pp = -6; break;
|
| 1304 |
+
case 'P': pp = 1; break;
|
| 1305 |
+
case 'N': pp = 2; break;
|
| 1306 |
+
case 'B': pp = 3; break;
|
| 1307 |
+
case 'R': pp = 4; break;
|
| 1308 |
+
case 'Q': pp = 5; break;
|
| 1309 |
+
case 'K': pp = 6; break;
|
| 1310 |
+
default: pp = -8;
|
| 1311 |
+
}
|
| 1312 |
+
if (pp > -8)
|
| 1313 |
+
board[y - 1][x - 1] = pp;
|
| 1314 |
+
x++;
|
| 1315 |
+
if (x == 9) {
|
| 1316 |
+
x = 1;
|
| 1317 |
+
y--;
|
| 1318 |
+
if (!y) {
|
| 1319 |
+
ch = fgetc(f);
|
| 1320 |
+
goto end;
|
| 1321 |
+
}
|
| 1322 |
+
}
|
| 1323 |
+
}
|
| 1324 |
+
}
|
| 1325 |
+
|
| 1326 |
+
end:
|
| 1327 |
+
ch = fgetc(f);
|
| 1328 |
+
fclose(f);
|
| 1329 |
+
// Update stm based on the character read from FEN
|
| 1330 |
+
if (ch == 'w')
|
| 1331 |
+
stm = 0;
|
| 1332 |
+
else if (ch == 'b')
|
| 1333 |
+
stm = 1;
|
| 1334 |
+
else {
|
| 1335 |
+
warn("Cannot set `stm' variable");
|
| 1336 |
+
}
|
| 1337 |
+
}
|
| 1338 |
+
|
| 1339 |
+
void parse_pgn(void)
|
| 1340 |
+
{
|
| 1341 |
+
if (system("pgn-extract -F start.pgn -w200 > pf") != 0) {
|
| 1342 |
+
// Handle error when pgn-extract fails
|
| 1343 |
+
fprintf(stderr, "Error running pgn-extract\n");
|
| 1344 |
+
return;
|
| 1345 |
+
}
|
| 1346 |
+
FILE *f = fopen("pf", "r");
|
| 1347 |
+
FILE *g = fopen("start.fen", "w");
|
| 1348 |
+
if (!f || !g) {
|
| 1349 |
+
fprintf(stderr, "Error opening files\n");
|
| 1350 |
+
if (f) fclose(f);
|
| 1351 |
+
if (g) fclose(g);
|
| 1352 |
+
return;
|
| 1353 |
+
}
|
| 1354 |
+
int ch;
|
| 1355 |
+
BOARD board;
|
| 1356 |
+
copy_board(*get_init(), board);
|
| 1357 |
+
while ((ch = fgetc(f)) != '{') {}
|
| 1358 |
+
while ((ch = fgetc(f)) != '"') {}
|
| 1359 |
+
while ((ch = fgetc(f)) != '"') fputc(ch, g);
|
| 1360 |
+
fclose(f);
|
| 1361 |
+
fclose(g);
|
| 1362 |
+
parse_fen(board);
|
| 1363 |
+
save(board);
|
| 1364 |
+
}
|