ref: 2d4418c24ad95172831580fe71bb00259eb716d0
dir: /main.c/
/* moonfish is licensed under the AGPL (v3 or later) */
/* copyright 2023, 2024 zamfofex */
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include "moonfish.h"
static void moonfish_go(struct moonfish_node *node)
{
static struct moonfish_result result;
static struct moonfish_options options;
static struct moonfish_chess chess;
long int our_time, their_time, *xtime, time;
char *arg, *end;
char name[6];
our_time = -1;
their_time = -1;
time = -1;
moonfish_root(node, &chess);
for (;;) {
arg = strtok(NULL, "\r\n\t ");
if (arg == NULL) break;
if (!strcmp(arg, "wtime") || !strcmp(arg, "btime")) {
if (chess.white) {
if (!strcmp(arg, "wtime")) xtime = &our_time;
else xtime = &their_time;
}
else {
if (!strcmp(arg, "wtime")) xtime = &their_time;
else xtime = &our_time;
}
arg = strtok(NULL, "\r\n\t ");
if (arg == NULL) {
fprintf(stderr, "missing time in 'go' command\n");
exit(1);
}
errno = 0;
*xtime = strtol(arg, &end, 10);
if (errno || *end != 0 || *xtime < 0) {
fprintf(stderr, "malformed time in 'go' command\n");
exit(1);
}
continue;
}
if (!strcmp(arg, "movetime")) {
arg = strtok(NULL, "\r\n\t ");
if (arg == NULL) {
fprintf(stderr, "malformed 'go movetime' command\n");
exit(1);
}
errno = 0;
time = strtol(arg, &end, 10);
if (errno || *end != 0 || time < 0) {
fprintf(stderr, "malformed 'movetime' in 'go' command\n");
exit(1);
}
continue;
}
}
options.max_time = time;
options.our_time = our_time;
moonfish_best_move(node, &result, &options);
moonfish_to_uci(&chess, &result.move, name);
printf("info nodes %ld\n", result.node_count);
printf("bestmove %s\n", name);
}
static void moonfish_position(struct moonfish_node *node)
{
static struct moonfish_chess chess, chess0;
static struct moonfish_move move;
static char line[2048];
char *arg;
arg = strtok(NULL, "\r\n\t ");
if (arg == NULL) {
fprintf(stderr, "incomplete 'position' command\n");
exit(1);
}
moonfish_chess(&chess);
if (!strcmp(arg, "fen")) {
arg = strtok(NULL, "\r\n");
if (arg == NULL) {
fprintf(stderr, "incomplete 'position fen' command\n");
exit(1);
}
moonfish_from_fen(&chess, arg);
arg = strstr(arg, "moves");
if (arg != NULL) {
do arg--;
while (*arg == '\t' || *arg == ' ');
strcpy(line, arg);
strtok(line, "\r\n\t ");
}
}
else {
if (strcmp(arg, "startpos")) {
fprintf(stderr, "malformed 'position' command\n");
exit(1);
}
}
arg = strtok(NULL, "\r\n\t ");
if (arg != NULL && !strcmp(arg, "moves")) {
for (;;) {
arg = strtok(NULL, "\r\n\t ");
if (arg == NULL) break;
if (moonfish_from_uci(&chess, &move, arg)) {
fprintf(stderr, "malformed move '%s'\n", arg);
exit(1);
}
moonfish_root(node, &chess0);
if (moonfish_equal(&chess0, &chess)) moonfish_reroot(node, &move.chess);
chess = move.chess;
}
}
moonfish_root(node, &chess0);
if (!moonfish_equal(&chess0, &chess)) moonfish_reroot(node, &chess);
}
int main(int argc, char **argv)
{
static char line[2048];
char *arg;
struct moonfish_node *node;
if (argc > 1) {
fprintf(stderr, "usage: %s (no arguments)\n", argv[0]);
return 1;
}
node = moonfish_new();
for (;;) {
fflush(stdout);
if (fgets(line, sizeof line, stdin) == NULL) {
if (feof(stdin)) break;
perror("fgets");
return 1;
}
arg = strtok(line, "\r\n\t ");
if (arg == NULL) continue;
if (!strcmp(arg, "go")) {
moonfish_go(node);
continue;
}
if (!strcmp(arg, "quit")) break;
if (!strcmp(arg, "position")) {
moonfish_position(node);
continue;
}
if (!strcmp(arg, "uci")) {
printf("id name moonfish\n");
printf("id author zamfofex\n");
printf("uciok\n");
continue;
}
if (!strcmp(arg, "isready")) {
printf("readyok\n");
continue;
}
if (!strcmp(arg, "debug") || !strcmp(arg, "setoption") || !strcmp(arg, "ucinewgame") || !strcmp(arg, "stop")) continue;
fprintf(stderr, "warning: unknown command '%s'\n", arg);
}
moonfish_finish(node);
return 0;
}