-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
84 lines (66 loc) · 2.37 KB
/
main.c
File metadata and controls
84 lines (66 loc) · 2.37 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
#include <stdio.h>
#include "chess.h"
#include "engine.h"
extern Board board;
extern unsigned long long int repetition_history[1000];
extern int move_history_count;
int debug = 0;
extern bool inBook;
void trim_newline(char* str) {
size_t len = strlen(str);
if (len > 0 && str[len - 1] == '\n') {
str[len - 1] = '\0';
}
}
int main(int argc, char* argv[]) {
initChess();
initEngine();
char line[2048];
bool running = true;
inBook = true; // start with book enabled
if (argc == 2 && strcmp(argv[1], "engine") == 0) {
while (running && fgets(line, sizeof(line), stdin)) {
trim_newline(line);
if (strcmp(line, "setfen") == 0) {
if (!fgets(line, sizeof(line), stdin)) break;
trim_newline(line);
parseFEN(line);
repetition_history[0] = getZobristHash();
move_history_count = 0;
printf("ok\n");
} else if (strcmp(line, "setmovehistory") == 0) {
if (!fgets(line, sizeof(line), stdin)) break;
trim_newline(line);
char* token = strtok(line, " ");
while (token != NULL) {
Move move = stringToMove(token);
makeMove(move);
move_history_count++;
repetition_history[move_history_count] = getZobristHash();
token = strtok(NULL, " ");
}
printf("ok\n");
} else if (strcmp(line, "getmove") == 0) {
if (!fgets(line, sizeof(line), stdin)) break;
trim_newline(line);
int milliseconds = atoi(line);
int depth_searched = 0;
int eval = 0;
Move best_move = getbestMove(milliseconds, &depth_searched, &eval);
printMove(best_move); // this prints to stdout (e.g., "e2e4")
printf("%d\n", depth_searched);
printf("%d\n", eval);
printf("ok\n");
} else if (strcmp(line, "end") == 0 || strcmp(line, "quit") == 0) {
printf("ok\n");
running = false;
} else {
printf("unknown command: %s\n", line);
}
fflush(stdout);
}
cleanupEngine();
cleanupChess();
}
return 0;
}