-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuci.c
More file actions
417 lines (343 loc) · 10.5 KB
/
uci.c
File metadata and controls
417 lines (343 loc) · 10.5 KB
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
#include "uci.h"
#include "board.h"
#include "validate.h"
#include "util.h"
#include "search.h"
#include "movegen.h"
#include "makemove.h"
#include "attack.h"
#include "evaluate.h"
#include "io.h"
#include "tt.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define INPUTBUFFER 400 * 6
//go depth 6 wtime 18000 btime 10000 binc 1000 winc 1000 movetime 1000 movestogo 40
void parseGo(char *line, S_SEARCHINFO *info, S_BOARD *pos) {
int depth = -1, movestogo = 30, movetime = -1;
int time = -1, inc = 0;
char *ptr = NULL;
info->timeset = FALSE;
if ((ptr = strstr(line,"infinite"))) {
;
}
if ((ptr = strstr(line,"binc")) && pos->side == BLACK) {
inc = atoi(ptr + 5);
}
if ((ptr = strstr(line,"winc")) && pos->side == WHITE) {
inc = atoi(ptr + 5);
}
if ((ptr = strstr(line,"wtime")) && pos->side == WHITE) {
time = atoi(ptr + 6);
}
if ((ptr = strstr(line,"btime")) && pos->side == BLACK) {
time = atoi(ptr + 6);
}
if ((ptr = strstr(line,"movestogo"))) {
movestogo = atoi(ptr + 10);
}
if ((ptr = strstr(line,"movetime"))) {
movetime = atoi(ptr + 9);
}
if ((ptr = strstr(line,"depth"))) {
depth = atoi(ptr + 6);
}
if(movetime != -1) {
time = movetime;
movestogo = 1;
}
info->start = getTimeMs();
info->depth = depth;
if(time != -1) {
info->timeset = TRUE;
time /= movestogo;
time -= 50;
info->stop = info->start + time + inc;
}
if(depth == -1) {
info->depth = MAXDEPTH;
}
printf("time:%d start:%ld stop:%ld depth:%d timeset:%d\n",
time,info->start,info->stop,info->depth,info->timeset);
searchPosition(pos, info);
}
//position fen
//position startpos
// ... moves e2e4 e7e5 b7b8q
void parsePosition(char *line, S_BOARD *pos) {
line += 9;
char *ptrchar = line;
if(!strncmp(line, "startpos", 8)) {
parseFEN(START_FEN, pos);
} else {
ptrchar = strstr(line, "fen");
if(ptrchar == NULL) {
parseFEN(START_FEN, pos);
} else {
ptrchar += 4;
parseFEN(ptrchar, pos);
}
}
ptrchar = strstr(line, "moves");
int move;
if(ptrchar != NULL) {
ptrchar += 6;
while(*ptrchar) {
move = parsemove(ptrchar, pos);
if(move == NOMOVE) break;
makeMove(pos, move);
pos->ply = 0;
while(*ptrchar && *ptrchar != ' ') {
ptrchar++;
}
ptrchar++;
}
}
printBoard(pos);
}
static void uciInfo() {
printf("id name %s\n", NAME);
printf("id author notoh\n");
printf("option name Hash type spin default %d min %d max %d\n", 64, 2, 1024); //TODO make these constants
printf("uciok\n");
}
void uciLoop(S_BOARD *pos, S_SEARCHINFO *info) {
setbuf(stdin, NULL);
setbuf(stdout, NULL);
char line[INPUTBUFFER];
uciInfo();
int MB = 64;
while(TRUE) {
memset(&line[0], 0, sizeof(line));
fflush(stdout);
if(!fgets(line, INPUTBUFFER, stdin)) {
continue;
}
if(line[0]== '\n') {
continue;
}
if(!strncmp(line, "isready", 7)) {
printf("readyok\n");
continue;
} else if(!strncmp(line, "position", 8)) {
parsePosition(line, pos);
} else if(!strncmp(line, "ucinewgame", 10)) {
clearHashTable(pos->hashtable);
parsePosition("position startpos\n", pos);
} else if(!strncmp(line, "go", 2)) {
parseGo(line, info, pos);
} else if(!strncmp(line, "quit", 4)) {
info->quit = TRUE;
break;
} else if(!strncmp(line, "uci", 3)) {
uciInfo();
} else if(!strncmp(line, "setoption name Hash value ", 26)) {
sscanf(line,"%*s %*s %*s %*s %d",&MB);
if(MB < 4) MB = 4;
if(MB > 1024) MB = 1024;
printf("Set Hash to %d MB\n", MB);
initHashTable(pos->hashtable, MB);
}
if(info->quit) {
break;
}
}
}
static int threefold(const S_BOARD *pos) {
int r = 0;
for(int i = 0; i < pos->histPly; i++) {
if(pos->history[i].posKey == pos->posKey) {
r++;
}
}
return r;
}
static int DrawMaterial(const S_BOARD *pos) {
if (pos->pceNum[wP] || pos->pceNum[bP]) return FALSE;
if (pos->pceNum[wQ] || pos->pceNum[bQ] || pos->pceNum[wR] || pos->pceNum[bR]) return FALSE;
if (pos->pceNum[wB] > 1 || pos->pceNum[bB] > 1) {return FALSE;}
if (pos->pceNum[wN] > 1 || pos->pceNum[bN] > 1) {return FALSE;}
if (pos->pceNum[wN] && pos->pceNum[wB]) {return FALSE;}
if (pos->pceNum[bN] && pos->pceNum[bB]) {return FALSE;}
return TRUE;
}
static int checkresult(S_BOARD *pos) {
if(pos->fiftyMove > 100) {
printf("1/2-1/2 {fifty move rule (claimed by BCE)}\n");
return TRUE;
}
if(threefold(pos) >= 2) {
printf("1/2-1/2 {3-fold repetition (claimed by BCE)}\n");
return TRUE;
}
if(DrawMaterial(pos) == TRUE) {
printf("1/2-1/2 {insufficient material (claimed by BCE)}\n");
}
S_MOVELIST list[1];
generateAllMoves(pos, list);
int moveNum = 0;
int found = 0;
for(moveNum = 0; moveNum < list->count; moveNum++) {
if(!makeMove(pos, list->moves[moveNum].move)) {
continue;
}
found++;
takeMove(pos);
break;
}
if(found != 0) {
return FALSE;
}
int inCheck = sqAttacked(pos->kingSq[pos->side], pos->side^1, pos);
if(inCheck == TRUE) {
if(pos->side == WHITE) {
printf("0-1 {black mates (claimed by BCE)}\n");
} else {
printf("1-0 {white mates (claimed by BCE)}\n");
}
} else {
printf("1/2-1/2 {stalemate (claimed by BCE)}\n");
}
return TRUE;
}
//only used if CONSOLE is defined
void consoleLoop(S_BOARD *pos, S_SEARCHINFO *info) {
printf("Welcome to BCE In Console Mode!\n");
printf("Type help for commands\n\n");
info->POST_THINKING = TRUE;
setbuf(stdin, NULL);
setbuf(stdout, NULL);
int depth = MAXDEPTH, movetime = 3000;
int engineSide = BOTH;
int move = NOMOVE;
char inBuf[80], command[80];
engineSide = BLACK;
parseFEN(START_FEN, pos);
while(TRUE) {
fflush(stdout);
if(pos->side == engineSide && checkresult(pos) == FALSE) {
info->start = getTimeMs();
info->depth = depth;
if(movetime != 0) {
info->timeset = TRUE;
info->stop = info->start + movetime;
}
searchPosition(pos, info);
}
printf("\nBCE > ");
fflush(stdout);
memset(&inBuf[0], 0, sizeof(inBuf));
fflush(stdout);
if (!fgets(inBuf, 80, stdin))
continue;
sscanf(inBuf, "%s", command);
if(!strcmp(command, "help")) {
printf("Commands:\n");
printf("quit - quit game\n");
printf("force - computer will not think\n");
printf("print - show board\n");
printf("post - show thinking\n");
printf("nopost - do not show thinking\n");
printf("new - start new game\n");
printf("go - set computer thinking\n");
printf("depth x - set depth to x\n");
printf("time x - set thinking time to x seconds (depth still applies if set)\n");
printf("view - show current depth and movetime settings\n");
printf("** note ** - to reset time and depth, set to 0\n");
printf("enter moves using b7b8q notation\n\n\n");
continue;
}
if(!strcmp(command, "mirror")) {
engineSide = BOTH;
mirrorEvalTest(pos);
continue;
}
if(!strcmp(command, "eval")) {
printBoard(pos);
printf("Eval:%d",eval(pos));
mirrorBoard(pos);
printBoard(pos);
printf("Eval:%d",eval(pos));
continue;
}
if(!strcmp(command, "quit")) {
info->quit = TRUE;
break;
}
if(!strcmp(command, "post")) {
info->POST_THINKING = TRUE;
continue;
}
if(!strcmp(command, "print")) {
printBoard(pos);
continue;
}
if(!strcmp(command, "nopost")) {
info->POST_THINKING = FALSE;
continue;
}
if(!strcmp(command, "force")) {
engineSide = BOTH;
continue;
}
if(!strcmp(command, "view")) {
if(depth == MAXDEPTH) printf("depth not set ");
else printf("depth %d",depth);
if(movetime != 0) printf(" movetime %ds\n",movetime/1000);
else printf(" movetime not set\n");
continue;
}
if(!strcmp(command, "depth")) {
sscanf(inBuf, "depth %d", &depth);
if(depth==0) depth = MAXDEPTH;
continue;
}
if(!strcmp(command, "time")) {
sscanf(inBuf, "time %d", &movetime);
movetime *= 1000;
continue;
}
if(!strcmp(command, "new")) {
engineSide = BLACK;
parseFEN(START_FEN, pos);
continue;
}
if(!strcmp(command, "go")) {
engineSide = pos->side;
continue;
}
move = parsemove(inBuf, pos);
if(move == NOMOVE) {
printf("Command unknown:%s\n",inBuf);
continue;
}
makeMove(pos, move);
pos->ply=0;
}
}
//from Weiss
static inline int mateScore(const int score) {
return score > 0 ? ((INFINITE - score) / 2) + 1
: -((INFINITE + score) / 2);
}
//largely based on weiss
void printThinking(const S_BOARD *pos, int depth, int score, long nodes, long elapsed, int pvMoves) {
//mate or centipawn score
char *type = abs(score) >= ISMATE ? "mate" : "cp";
score = abs(score) >= ISMATE ? mateScore(score)
: score;
int hash = hashFull(pos);
long nps = (1000 * (nodes / (elapsed + 1)));
printf("info depth %d score %s %d time %ld nodes %ld nps %ld hashfull %d pv", depth, type, score, elapsed, nodes, nps, hash);
for(int i = 0; i < pvMoves; i++) {
printf(" %s", prmove(pos->pvarray[i]));
}
printf("\n");
fflush(stdout);
}
void printConclusion(const S_BOARD *pos, int move) {
printf("bestmove %s", prmove(move));
printf("\n");
fflush(stdout);
}