-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuci.c
187 lines (163 loc) · 3.84 KB
/
uci.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
#include "stdio.h"
#include "string.h"
#include "def.h"
#define NOMOVE 0
char START_FEN[] = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
#define INPUTBUFFER 400 * 6
// Go depth 6 wtime 180000 btime 100000 binc 1000 winc 1000 movetime 1000 movestogo 40.
void ParseGo(char* line, SEARCHINFO* info, 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->starttime = GetTime();
info->depth = depth;
if (time != -1)
{
info->timeset = TRUE;
time /= movestogo;
time -= 50;
info->stoptime = info->starttime + time + inc;
}
if (depth == -1)
{
info->depth = MAXDEPTH;
}
printf("time:%d start:%d stop:%d depth:%d timeset:%d\n",
time,
info->starttime,
info->stoptime,
info->depth,
info->timeset);
SearchPos(pos, info);
}
void ParsePosition(char* lineIn, BOARD* pos)
{
lineIn += 9;
char* ptrChar = lineIn;
if (strncmp(lineIn, "startpos", 8) == 0)
{
CalcFen(START_FEN, pos);
}
else
{
ptrChar = strstr(lineIn, "fen");
if (ptrChar == NULL)
{
CalcFen(START_FEN, pos);
}
else
{
ptrChar += 4;
CalcFen(ptrChar, pos);
}
}
ptrChar = strstr(lineIn, "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);
}
void Uci_Loop()
{
setbuf(stdin, NULL);
setbuf(stdout, NULL);
char line[INPUTBUFFER];
printf("id name %s\n", NAME);
printf("id author Prakhar\n");
printf("uciok\n");
BOARD pos[1];
SEARCHINFO info[1];
info->quit = 0;
InitPvTable(pos->PvTable);
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))
{
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))
{
printf("id name %s\n", NAME);
printf("id author Prakhar\n");
printf("uciok\n");
}
if (info->quit)
break;
}
}