/
uci.h
210 lines (176 loc) · 6.21 KB
/
uci.h
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
#pragma once
#include "defs.h"
#include "movegen.h"
#include "nnue.h"
#include "position.h"
#include "search.h"
#include "utils.h"
#include <iostream>
#include <memory>
#include <stdio.h>
#include <thread>
std::thread s;
void run_thread(Position &position, ThreadInfo &thread_info) {
// This wrapper function makes you able to use the "stop" command to stop the
// search immediately.
s = std::thread(iterative_deepen, std::ref(position), std::ref(thread_info));
}
void uci(ThreadInfo &thread_info, Position &position) {
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
printf("Patricia Chess Engine, written by Adam Kulju\n\n\n");
thread_info.nnue_state.m_accumulator_stack.reserve(100);
std::string input;
while (getline(std::cin, input)) {
std::istringstream input_stream(input);
std::string command;
input_stream >> std::skipws >> command; // write into command
if (command == "d") {
input_stream.clear();
input_stream.str("position fen "
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/"
"R3K2R w KQkq - 0 1");
input_stream >> std::skipws >> command;
}
if (command == "quit") {
if (s.joinable()) {
s.join();
}
exit(0);
}
else if (command == "uci") {
printf("id name Patricia 2.0.1\nid author Adam Kulju\n"
"option name Hash type spin default 32 min 1 max 131072\n"
"option name Threads type spin default 1 min 1 max 1\n"
"option name UCI_Elo type spin default 3200 min 1000 max 3200\n"
"uciok\n");
}
else if (command == "isready") {
printf("readyok\n");
}
else if (command == "setoption") {
std::string name;
int value;
input_stream >> command;
input_stream >> name;
input_stream >> command;
input_stream >> value;
if (name == "Hash") {
resize_TT(value);
}
else if (name == "Threads") {
}
else if (name == "UCI_Elo"){
thread_info.max_nodes_searched = UINT64_MAX / 2;
if (value >= 3100){
thread_info.max_iter_depth = MaxSearchDepth;
}
else if (value == 1200){
thread_info.max_iter_depth = 2; //60 + 0.6 elo: 1100-1200
}
else if (value == 1400){
thread_info.max_nodes_searched = 1000; //60 + 0.6 elo: 1300-1400
}
else if (value == 1600){
thread_info.max_nodes_searched = 1600; //60 + 0.6 elo: 1550-1600
}
else if (value == 1800){
thread_info.max_iter_depth = 4; //60 + 0.6 elo: ~1700
}
else if (value == 2000){
thread_info.max_nodes_searched = 4000; //60 + 0.6 elo: ~1900
}
else if (value == 2200){
thread_info.max_nodes_searched = 8000; //60 + 0.6 elo: 2150-2250
}
else if (value == 2600){
thread_info.max_nodes_searched = 64000; //60 + 0.6 elo: 2500-2600
}
else{
printf("Unknown UCI Elo option, defaulting to maximum strength\n");
thread_info.max_iter_depth = MaxSearchDepth;
}
}
}
else if (command == "stop") {
thread_info.stop = true;
s.join();
}
else if (command == "ucinewgame") {
new_game(thread_info);
set_board(position, thread_info,
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
}
else if (command == "position") {
thread_info.game_ply = 0;
std::string setup;
input_stream >> setup;
if (setup == "fen") {
std::string fen;
for (int i = 0; i < 6; i++) {
// 1 2 3 4 5 6 subtokens
// rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
std::string substr;
input_stream >> substr;
fen += substr + " ";
}
set_board(position, thread_info, fen);
} else {
set_board(position, thread_info,
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
}
thread_info.nnue_state.reset_nnue(position);
thread_info.zobrist_key = calculate(position);
std::string has_moves;
if (input_stream >>
has_moves) { // we're at the "moves" part of the command now
std::string moves;
while (input_stream >> moves) {
Move move = uci_to_internal(moves);
ss_push(position, thread_info, move, thread_info.zobrist_key,
0); // fill the game hist stack as we go
update_nnue_state(thread_info.nnue_state, move, position);
make_move(position, move, thread_info, false);
}
}
}
else if (command == "go") {
if (s.joinable()) {
s.join();
}
thread_info.max_nodes_searched = UINT64_MAX / 2;
thread_info.max_iter_depth = MaxSearchDepth;
int color = position.color, time = 1000000000, increment = 0;
std::string token;
while (input_stream >> token) {
if (token == "infinite") {
;
} else if (token == "wtime" && color == Colors::White) {
input_stream >> time;
} else if (token == "btime" && color == Colors::Black) {
input_stream >> time;
} else if (token == "winc" && color == Colors::White) {
input_stream >> increment;
} else if (token == "binc" && color == Colors::Black) {
input_stream >> increment;
}
else if (token == "nodes"){
uint64_t nodes;
input_stream >> nodes;
thread_info.max_nodes_searched = nodes;
}
else if (token == "depth"){
int depth;
input_stream >> depth;
thread_info.max_iter_depth = depth;
}
}
// Calculate time allotted to search
time = std::max(1, time - 50);
thread_info.max_time = time / 5;
thread_info.opt_time = (time / 20 + increment) * 6 / 10;
thread_info.start_time = std::chrono::steady_clock::now();
run_thread(position, thread_info);
}
}
}