forked from JasonCreighton/snowy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUCI.cpp
195 lines (161 loc) · 6.24 KB
/
UCI.cpp
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
// Copyright (c) 2017 Jason Creighton
// Available under the MIT license, see included LICENSE file for details
#include "UCI.hpp"
#include "Board.hpp"
#include "Search.hpp"
#include "IO.hpp"
#include "Constants.hpp"
#include "Util.hpp"
#include <iostream>
#include <string>
#include <sstream>
#include <list>
namespace {
int ChooseMoveTime(int timeLeft_ms, int increment_ms, int movesUntilNextTimeControl) {
const int TIME_MANAGEMENT_MARGIN_MS = 1000;
// No move time specified, so no time limit
if(timeLeft_ms == -1) {
return -1;
}
int totalTimeAvailableUntilNextTimeControl_ms = timeLeft_ms + (increment_ms * movesUntilNextTimeControl);
int idealMoveTime_ms = totalTimeAvailableUntilNextTimeControl_ms / movesUntilNextTimeControl;
// idealMoveTime takes into account the increment, which is time that we
// don't actually have yet, so we also need to consider the right we
// have left right now.
int avoidFlagTime_ms = timeLeft_ms - TIME_MANAGEMENT_MARGIN_MS;
// The move time is the more conservative of the two
int moveTime_ms = std::min(idealMoveTime_ms, avoidFlagTime_ms);
IO::PutInfo("Thinking for " + std::to_string(moveTime_ms) + " ms");
return moveTime_ms;
}
}
UCI::UCI() : m_Search(m_Board) {
}
void UCI::Run() {
std::string line;
while(IO::GetLine(line)) {
if(!DoCommand(line)) {
return;
}
}
}
bool UCI::DoCommand(const std::string& commandLine) {
std::stringstream lineStream(commandLine);
std::string command;
lineStream >> command;
if(command == "uci") {
IO::PutLine("id name Snowy");
IO::PutLine("id author Jason Creighton");
IO::PutLine("uciok");
} else if(command == "position") {
m_Search.WaitForSearch();
std::string fenString;
std::string token;
std::list<std::string> tokens;
while(lineStream >> token) {
tokens.push_back(token);
}
if(tokens.front() == "startpos") {
fenString = Board::FEN_START_POSITION;
tokens.pop_front();
} else if (tokens.front() == "fen") {
tokens.pop_front();
// FEN string should consist of up to 6 tokens (last two seemingly optional)
for(int i = 0; i < 6; ++i) {
if(!tokens.empty() && tokens.front() != "moves") {
fenString += tokens.front() + " ";
tokens.pop_front();
}
}
}
// Set up board
m_Board.ParseFen(fenString);
if(!tokens.empty() && tokens.front() == "moves") {
tokens.pop_front();
while(!tokens.empty()) {
Board::Move m = Board::ParseMove(tokens.front());
tokens.pop_front();
if(!m_Board.Make(m)) {
IO::PutInfo("WARNING: Unable to make move " + m.ToString());
}
}
}
IO::PutInfo("Position hash: " + IntegerToHexString(m_Board.Hash()));
} else if (command == "isready") {
IO::PutLine("readyok");
} else if (command == "go") {
std::string optionName;
int depth = -1;
bool bruteForce = false;
int moveTime_ms = -1;
int wtime_ms = -1;
int winc_ms = 0;
int btime_ms = -1;
int binc_ms = 0;
int movesUntilNextTimeControl = 40;
bool showHistograms = false;
while(lineStream >> optionName) {
if(optionName == "depth") { lineStream >> depth; }
else if(optionName == "bruteforce") { lineStream >> bruteForce; }
else if(optionName == "movetime") { lineStream >> moveTime_ms; }
else if(optionName == "wtime") { lineStream >> wtime_ms; }
else if(optionName == "winc") { lineStream >> winc_ms; }
else if(optionName == "btime") { lineStream >> btime_ms; }
else if(optionName == "binc") { lineStream >> binc_ms; }
else if(optionName == "movestogo") { lineStream >> movesUntilNextTimeControl; }
else if(optionName == "showhistograms") { lineStream >> showHistograms; }
}
if(moveTime_ms == -1) {
// No move time, try to calculate one
if(m_Board.WhiteToMove()) {
moveTime_ms = ChooseMoveTime(wtime_ms, winc_ms, movesUntilNextTimeControl);
} else {
moveTime_ms = ChooseMoveTime(btime_ms, binc_ms, movesUntilNextTimeControl);
}
// NB: At this point moveTime_ms might still be -1, depending on the options given
}
if(depth == -1) {
// No depth was specified, unlimited search depth
depth = MAX_PLY;
// However, if no move time was given, perhaps a reasonable default
// so we don't search forever
if(moveTime_ms == -1) {
moveTime_ms = 5000;
}
}
m_Search.StartSearch(depth, bruteForce, showHistograms, moveTime_ms);
} else if (command == "stop") {
m_Search.StopSearch();
} else if(command == "perft") {
m_Search.WaitForSearch();
int depth;
lineStream >> depth;
std::vector<Board::Move> moveList;
m_Board.FindPseudoLegalMoves(moveList);
for(auto &m : moveList) {
if(m_Board.Make(m)) {
long perftCount = m_Search.Perft(depth - 1);
if(perftCount != 0) {
IO::PutLine(m.ToString() + ": " + std::to_string(perftCount));
}
m_Board.Unmake();
}
}
} else if(command == "eval") {
m_Search.WaitForSearch();
int score = m_Search.Quiesce();
int staticEval = m_Board.StaticEvaluation();
IO::PutInfo("Static evaluation: " + std::to_string(staticEval));
IO::PutInfo("Quiesce score: " + std::to_string(score));
} else if(command == "d") {
m_Search.WaitForSearch();
m_Board.Print();
} else if (command == "quit") {
return false;
}
return true;
}
void UCI::WaitForSearch() {
// Man, I hate dumb little wrappers like this. There must be a better way.
m_Search.WaitForSearch();
}