-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnilatac.cc
229 lines (201 loc) · 6.17 KB
/
nilatac.cc
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
/**
* Copyright 2002-2006 Catalin Francu <[email protected]>
* This file is part of Nilatac.
*
* Nilatac 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.
*
* Nilatac 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 Nilatac; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <getopt.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "egtb.h"
#include "pns.h"
#include "suicide.h"
const char* SPC = " ";
inline char* get_token(char* s) {
return strtok(s, " \t\n");
}
/**
* Calculates how much time we can afford to think given our remaining time
* and increment and the opponent's remaining time. All values are in
* milliseconds.
*/
int allocate_time(int my_time, int my_inc, int opp_time) {
// If I have under 5 seconds left, or I am more than 3 seconds behind the
// opponent with less than 20 seconds left, move quickly.
if (my_time < 500 ||
(my_time <= 2000 && (my_time < opp_time - 300)))
return 10; // 0.1 seconds
return my_time / 15 + my_inc * 2 / 3;
}
void parse_option(char* name, char* value) {
if (strcmp(strtok(NULL, " "), "name")) {
fatal("UCI: \"setoption\" should be followed by \"name\"");
}
name[0] = value[0] = '\0';
int onName = true;
char *t;
while ((t = strtok(NULL, " ")) != NULL) {
if (!strcmp(t, "value")) {
onName = false;
} else if (onName) {
if (name[0]) {
strcat(name, SPC);
}
strcat(name, t);
} else {
if (value[0]) {
strcat(value, SPC);
}
strcat(value, t);
}
}
}
void set_position(tboard* b) {
char *t = strtok(NULL, " ");
if (!strcmp(t, "startpos")) {
fen_to_board(NEW_BOARD, b);
} else if (!strcmp(t, "fen")) {
// the FEN notation has exactly six words
char fen[100] = "";
for (int i = 0; i < 6; i++) {
if (i) {
strcat(fen, SPC);
}
strcat(fen, strtok(NULL, " "));
}
fen_to_board(fen, b);
} else {
fatal((string)"UCI: Unknown position type [" + t + "]");
}
t = strtok(NULL, " ");
if (t && !strcmp(t, "moves")) {
while ((t = strtok(NULL, " ")) != NULL) {
int error = execute_move(t, b);
if (error) {
fatal((string)"Incorrect move [" + t + "]");
}
}
}
// printboard(b);
}
void parse_go(tboard* b) {
char* cmd;
int my_time = 0, my_inc = 0, opp_time = 0; // in millis
while ((cmd = strtok(NULL, " ")) != NULL) {
if (!strcmp(cmd, "wtime") || !strcmp(cmd, "btime")) {
if ((b->side == WHITE) ^ !strcmp(cmd, "btime")) {
my_time = atoi(strtok(NULL, " "));
} else {
opp_time = atoi(strtok(NULL, " "));
}
} else if (!strcmp(cmd, "winc") || !strcmp(cmd, "binc")) {
if ((b->side == WHITE) ^ !strcmp(cmd, "binc")) {
my_inc = atoi(strtok(NULL, " "));
} else {
// ignore opponent's increment
strtok(NULL, " ");
}
} else if (!strcmp(cmd, "movestogo") ||
!strcmp(cmd, "movetime")) {
strtok(NULL, " "); // ignore one argument
} else {
fatal((string)"Unknown go command [" + cmd + "]");
}
}
int millis = allocate_time(my_time, my_inc, opp_time);
info((string)"Thinking for " + to_string(millis) + " millis");
tmove mv = find_best_move(b, millis);
if (mv.from != -1) { // not stalemate
cout << "bestmove " << movetostring(mv) << endl << flush;
}
}
int main(int argc, char* argv[]) {
tboard b;
char* book_file_name = NULL;
char* egtb_dir_name = NULL;
char* log_file_name = NULL;
int opt = 0;
static struct option long_options[] =
{
{"book", required_argument, 0, 'b' },
{"egtb", required_argument, 0, 'e' },
{"log", required_argument, 0, 'l' },
{0, 0, 0, 0 }
};
while ((opt = getopt_long(argc, argv, "b:e:l:", long_options, NULL)) != -1) {
switch (opt) {
case 'b':
book_file_name = strdup(optarg);
break;
case 'e':
egtb_dir_name = strdup(optarg);
break;
case 'l':
log_file_name = strdup(optarg);
break;
default:
fatal((string)"Usage: " + argv[0] +
" [-b|--book book_file_name]" +
" [-e|--egtb egtb_dir_name]" +
" [-l|--log log_file_name]");
}
}
init_common(log_file_name);
init(book_file_name ? book_file_name : BOOK_FILENAME,
egtb_dir_name ? egtb_dir_name : EGTB_DIRNAME);
restart();
setbuf(stdout, NULL);
char s[10000], name[1000], value[1000];
while (fgets(s, 1000, stdin)) {
s[strlen(s) - 1] = '\0';
char* command = get_token(s);
if (!strcmp(command, "uci")) {
printf("id name Nilatac\n");
printf("id author Cătălin Frâncu\n");
printf("option name UCI_Variant type combo default antichess var antichess\n");
printf("uciok\n");
} else if (!strcmp(command, "isready")) {
printf("readyok\n");
} else if (!strcmp(command, "setoption")) {
parse_option(name, value);
if (!strcasecmp(name, "UCI_Variant")) {
if (strcasecmp(value, "suicide") &&
strcasecmp(value, "antichess") &&
strcasecmp(value, "giveaway")) {
fatal("Nilatac only plays suicide/antichess/giveaway");
}
} else {
// ignore other options
}
} else if (!strcmp(command, "ucinewgame")) {
restart();
} else if (!strcmp(command, "position")) {
set_position(&b);
} else if (!strcmp(command, "go")) {
parse_go(&b);
} else if (!strcmp(command, "quit")) {
break;
} else if (!strcmp(command, "stop")) {
info((string)"Ignoring command " + command);
} else {
fatal((string)"Unknown command " + command);
}
}
info("Shutting down.");
return 0;
}