-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcom.cpp
More file actions
390 lines (307 loc) · 9.12 KB
/
com.cpp
File metadata and controls
390 lines (307 loc) · 9.12 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
#include "stdafx.h"
#include "transposition.h"
enum eproto {
PROTO_NOTHING,
PROTO_XBOARD,
PROTO_UCI
} mode = PROTO_NOTHING;
int debug = 0;
#ifndef __linux__
#include <windows.h>
int pipe;
HANDLE hstdin;
int com_init() {
unsigned long dw;
hstdin = GetStdHandle(STD_INPUT_HANDLE);
pipe = !GetConsoleMode(hstdin, &dw);
if (!pipe) {
SetConsoleMode(hstdin,dw&~(ENABLE_MOUSE_INPUT|ENABLE_WINDOW_INPUT));
FlushConsoleInputBuffer(hstdin);
} else {
setvbuf(stdin,NULL,_IONBF,0);
setvbuf(stdout,NULL,_IONBF,0);
}
/* default search settings */
chronos.movetime = 5000;
chronos.flags = FMOVETIME;
printWelcome();
return 0;
}
int input() {
unsigned long dw=0;
if (task == TASK_NOTHING) return 1;
if (stdin->_cnt > 0) return 1;
if (pipe) {
if (!PeekNamedPipe(hstdin, 0, 0, 0, &dw, 0)) return 1;
return dw;
} else {
GetNumberOfConsoleInputEvents(hstdin, &dw);
if (dw > 1) task = TASK_NOTHING;
}
return 0;
}
#else
#include "sys/time.h"
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/timeb.h>
#include "sys/select.h"
int com_init()
{
}
int input()
{
fd_set readfds;
struct timeval tv;
FD_ZERO (&readfds);
FD_SET (fileno(stdin), &readfds);
tv.tv_sec=0; tv.tv_usec=0;
int ret=select(16, &readfds, 0, 0, &tv);
if (ret==-1) {
#ifndef NDEBUG
switch(errno) {
case EBADF:
printf("Bad file number???\n");
break;
case EINTR:
printf("******************************* Interrupt signal \n");
return 0; //HACK
break;
case EINVAL:
printf("The timeout argument is invalid; one of the components is negative or too large.\n");
break;
}
#else
// in release build just return 0 when select errors out.
return 0;
#endif
}
return (FD_ISSET(fileno(stdin), &readfds));
}
#endif
char string[32767];
char *getsafe(char *buffer, int count)
{
char *result = buffer, *np;
if ((buffer == NULL) || (count < 1))
result = NULL;
else if (count == 1)
*result = '\0';
else if ((result = fgets(buffer, count, stdin)) != NULL)
if (np = strchr(buffer, '\n'))
*np = '\0';
return result;
}
static char command[65536];
int com() {
if (!input()) return 0;
/* unwind the search-stack first */
if (task == TASK_SEARCH) {
task = TASK_NOTHING;
return 0;
}
getsafe(command, sizeof command);
switch (mode) {
case PROTO_XBOARD :
com_xboard(command);
break;
case PROTO_UCI :
com_uci(command);
break;
case PROTO_NOTHING:
com_nothing(command);
break;
}
return 0;
}
int com_nothing(char * command) {
int converted;
if (!strcmp(command, "xboard")) com_xboard(command);
else if (!strcmp(command, "uci")) com_uci(command);
else if (!strncmp(command,"perft", 5)) perft_start(command);
else if (!strncmp(command,"bench", 5)) util_bench(command);
else if (!strcmp(command, "eval")) printEval();
else if (!strcmp(command, "stat")) printStats();
else if (!strcmp(command, "d")) board_display();
else if (!strcmp(command, "new")) board_loadFromFen(STARTFEN);
else if (!strncmp(command, "pos", 3)) board_loadFromFen(command+4);
else if (!strcmp(command, "go")) time_nothing_go();
else if (!strcmp(command, "quit")) exit(0);
else if (!strcmp(command, "help")) printHelp();
else if (com_ismove(command) ) {
if ( algebraic_moves(command) )
time_nothing_go();
else
printf("Sorry, this is not a legal move\n");
}
else if (!strncmp(command, "st", 2)) {
converted = sscanf(command, "st %d", &chronos.movetime);
chronos.movetime *= 1000;
chronos.flags = FMOVETIME;
}
else if (!strncmp(command, "sd", 2)) {
converted = sscanf(command, "sd %d", &chronos.depth);
chronos.flags = FDEPTH;
}
else if (command[0] == '\n') {}
else {
strcat(command, " - UNKNOWN COMMAND (type 'help' for a list of commands)");
com_send(command);
}
return 0;
}
int com_xboard(char * command) {
int converted;
if (!strcmp(command, "xboard"))
mode = PROTO_XBOARD;
if (!strcmp(command, "new"))
board_loadFromFen(STARTFEN);
else if (!strcmp(command, "force"))
task = TASK_NOTHING;
else if (!strcmp(command, "white"))
sd.myside = WHITE;
else if (!strcmp(command, "black"))
sd.myside = BLACK;
else if (!strncmp(command, "st", 2)) {
converted = sscanf(command, "st %d", &chronos.movetime);
chronos.movetime *= 1000;
chronos.flags = FMOVETIME;
}
else if (!strncmp(command, "sd", 2)) {
converted = sscanf(command, "sd %d", &chronos.depth);
chronos.flags = FDEPTH;
}
else if (!strncmp(command, "time", 4)) {
converted = sscanf(command, "time %d", &chronos.time[sd.myside]);
chronos.flags = FTIME;
}
else if (!strncmp(command, "otim", 4)) {
converted = sscanf(command, "otim %d", &chronos.time[!sd.myside]);
chronos.flags = FTIME;
}
else if (!strcmp(command, "go"))
time_xboard_go();
else if (!strcmp(command, "hint")) {
// hint
}
else if (!strcmp(command, "undo")) {
// undo
}
else if (!strcmp(command, "remove")) {
// remove
}
else if (!strcmp(command, "post")) {
// post
}
else if (!strcmp(command, "nopost")) {
// nopost
}
else if (!strcmp(command, "quit")) {
exit(0);
}
else if (com_ismove(command)) {
algebraic_moves(command);
time_xboard_go();
}
return 0;
}
int com_uci(char * command) {
int converted;
if (!strcmp(command, "uci")) {
mode = PROTO_UCI;
com_send("id name CPW-Engine 1.1.18");
com_send("id author Computer Chess Wiki");
printf("option name Hash type spin default 64 min 1 max 1024\n");
printf("option name Ponder type check default true\n");
// send options
com_send("uciok");
}
if (!strcmp(command, "isready"))
com_send("readyok");
if (!strncmp(command, "setoption", 9)) {
char name[256];
char value[256];
if (strstr(command, "setoption name Ponder value")) options.ponder = (strstr(command, "value true") != 0);
converted = sscanf(command, "setoption name %s value %s", name, value);
if (!strcmp(name, "Hash")) {
int val;
converted = sscanf(value, "%d", &val);
tt_setsize(val<<20);
ttpawn_setsize(val<<18);
}
}
if (!strcmp(command, "ucinewgame")) {}
if (!strncmp(command, "position", 8)) {
//position [fen | startpos] [moves ...]
if (!strncmp(command,"position fen",12)) {
board_loadFromFen(command + 13);
} else {
board_loadFromFen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
}
char * moves = strstr(command, "moves");
if (moves) algebraic_moves(moves+6);
}
if (!strncmp(command, "go", 2))
time_uci_go(command);
if (!strncmp(command, "debug", 5))
debug = strcmp(command,"debug off");
if (!strcmp(command, "ponderhit"))
time_uci_ponderhit();
if (!strcmp(command, "stop"))
task = TASK_NOTHING;
if (!strcmp(command, "quit"))
exit(0);
return 0;
}
int com_send(char * command) {
printf("%s\n",command);
return 0;
}
int com_sendmove(smove m) {
int promotion = 0;
char parray[5] = {0,'q','r','b','n'};
char command[20];
char move[6];
switch (mode) {
case PROTO_XBOARD:
strcpy(command,"move ");
break;
case PROTO_UCI:
strcpy(command,"bestmove ");
break;
default:
strcpy(command,"CPW: ");
}
convert_0x88_a(m.from, move);
convert_0x88_a(m.to, move+2);
//Promotion piece
if (m.piece_to != m.piece_from) {
promotion = m.piece_to;
}
move[4] = parray[promotion];
move[5] = 0;
strcat(command, move);
com_send(command);
/* in xboard and nothing actually do the move on the board */
if (mode == PROTO_XBOARD || mode == PROTO_NOTHING)
move_make(m);
return 0;
}
int com_ismove(char * command) {
return (command[0] >= 'a' && command[0] <= 'h' &&
command[1] >= '1' && command[1] <= '8' &&
command[2] >= 'a' && command[2] <= 'h' &&
command[3] >= '1' && command[3] <= '8' &&
( command[4] == ' ' || command[4] == '\n' || command[4] == 0 ||
command[4] == '-' ||
command[4] == 'q' || command[4] == 'r' || command[4] == 'b' || command[4] == 'n'));
/***************************************************************
* command[4] might be: *
* *
* (a) any kind of a blank space *
* (b) '-' or any other mark used in opening book processing *
* (c) first letter of a name of a promoted piece *
***************************************************************/
}