-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUCI_client.cpp
More file actions
200 lines (189 loc) · 5 KB
/
UCI_client.cpp
File metadata and controls
200 lines (189 loc) · 5 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
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
#include "UCI_client.h"
UCIclient::UCIclient() : ai(AI("*", black))
{
// Standard computer opponent is black
// Starting from standard position
getHostCommandCode = {
{ "uci", hostCommandCode::uci },
{ "debug", hostCommandCode::debug },
{ "isready", hostCommandCode::isready },
{ "setoption", hostCommandCode::setoption },
{ "register", hostCommandCode::registerEngine },
{ "ucinewgame", hostCommandCode::ucinewgame },
{ "position", hostCommandCode::position },
{ "go", hostCommandCode::go },
{ "stop", hostCommandCode::stop },
{ "ponderhit", hostCommandCode::ponderhit },
{ "quit", hostCommandCode::quit }
};
getGoArgCode = {
{ "depth", goArgument::depth },
{ "infinite", goArgument::infinite },
{ "mate", goArgument::mate },
{ "movetime", goArgument::movetime }
};
}
void UCIclient::UCI_IO_loop()
{
// Communicates with external GUI (Host)
string input;
vector<string> inputList;
while (true) {
waitForInput(inputList);
if(!interpretInput(inputList)) return;
}
}
void UCIclient::waitForInput(vector<string>& input)
{
input.clear();
string line;
getline(cin, line); // User or GUI input
// Tokenize input
boost::trim(line);
boost::escaped_list_separator<char> sep("", " ", "");
boost::tokenizer<boost::escaped_list_separator<char>> tokenize(line, sep);
outFile.open("turnierLog.log", ios::app);
for (auto token : tokenize) {
input.push_back(token);
//cout << token << ' ';
outFile << token << ' ';
}
outFile << '\n';
outFile.close();
}
bool UCIclient::interpretInput(vector<string>& inputList)
{
if (inputList.empty()) return true;
switch (getHostCommandCode[inputList[0]]) {
case hostCommandCode::uci:
printEngineID();
break;
case hostCommandCode::debug:
break;
case hostCommandCode::isready:
cout << "readyok\n";
break;
case hostCommandCode::setoption:
break;
case hostCommandCode::registerEngine:
/* Engine does not require registering */
break;
case hostCommandCode::ucinewgame:
uciNewGame();
cout << "readyok\n";
break;
case hostCommandCode::position:
parsePosition(inputList);
break;
case hostCommandCode::go:
go(inputList);
break;
case hostCommandCode::stop:
break;
case hostCommandCode::ponderhit:
/* Engine doesnt care */
break;
case hostCommandCode::quit:
return false;
default:
cout << "Unknown command: " << inputList[0] << '\n';
}
return true;
}
void UCIclient::printEngineID() const
{
printf("id name %s %s\n", ENGINE_NAME, ENGINE_VERSION);
cout << "id author Vukan Jevtic (github.com/VukanJ)\n"
"option name Hash type spin\n"
"uciok\n";
}
void UCIclient::uciNewGame()
{
// Resets hash tables and sets position to standard starting position
ai.reset();
ai.resetHash();
ai.currentAge = 0;
}
void UCIclient::parsePosition(vector<string>& inputList)
{
inputList.erase(inputList.begin()); // "position"
if (inputList.empty()) {
return;
}
if (*inputList.begin() == "startpos") {
inputList.erase(inputList.begin()); // "startpos"
ai.sideToMove = white;
// Standard starting position
ai.setFen("*");
}
else if (*inputList.begin() == "fen") {
// Custom initial position
inputList.erase(inputList.begin()); // "fen"
if (inputList.size() < 6) {
cerr << "Invalid fen!\n";
return;
}
string FEN;
ai.sideToMove = (inputList[1] == "w" ? white : black);
for (int i = 0; i < 6; ++i)
FEN += inputList[i] + ' ';
inputList.erase(inputList.begin(), inputList.begin() + 6);
ai.setFen(FEN);
//ai.printAscii();
}
if (inputList.empty()) {
// ready
return;
}
else if (inputList[0] == "moves") {
// Play given moves
inputList.erase(inputList.begin()); // "moves"
ai.playStringMoves(inputList, ai.sideToMove);
}
//ai.printAscii();
}
void UCIclient::go(vector<string>& inputList)
{
ai.resetHash();
pair<Move, Move> bestMoves;
inputList.erase(inputList.begin());
if (inputList.empty()) {
bestMoves = ai.getBestMove(ai.sideToMove, 5, true);
}
else {
switch (getGoArgCode[*inputList.begin()]) {
case goArgument::depth:
inputList.erase(inputList.begin()); // "depth"
if (inputList.empty()) {
cerr << "No depth specified!\n";
return;
}
bestMoves = ai.getBestMove(ai.sideToMove, stoi(inputList[0]), true);
break;
case goArgument::infinite:
// Create thread, run until "stop" is passed
break;
case goArgument::movetime:
// Run timed NegaMax for x seconds
break;
case goArgument::mate:
// Search mate in x, same as search to specified depth
inputList.erase(inputList.begin()); // "mate"
if (inputList.empty()) {
cerr << "# Moves to mate not specified!\n";
return;
}
bestMoves = ai.getBestMove(ai.sideToMove, stoi(inputList[0]), true);
break;
default:
bestMoves = ai.getBestMove(ai.sideToMove, 5, true);
break;
}
}
cout << "bestmove " << shortNotation(bestMoves.first)
<< " ponder " << shortNotation(bestMoves.second) << '\n';
outFile.open("turnierLog.log", ios::app);
outFile << "best = " << shortNotation(bestMoves.first) << '\n';
outFile.close();
//ai.currentAge++;
}