-
Notifications
You must be signed in to change notification settings - Fork 0
/
UCI.cpp
248 lines (201 loc) · 6.47 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//
// UCI.cpp
// Chess Engine
//
// Created by Blake Johnson on 2/22/19.
// Copyright © 2019 Blake Johnson. All rights reserved.
//
#include "UCI.hpp"
UCI::UCI()
{
wTime = 0;
bTime = 0;
wInc = 0;
bInc = 0;
depth = 0;
moveTime = 0;
}
void UCI::loop(){
std::string line ="";
std::string token = "";
returnState newState = returnState();
while (std::getline(std::cin, line))
{
std::istringstream stream (line);
token.clear();
stream >> std::skipws >> token;
if(token == "uci")
{
std::cout << "id name: Overhead\n";
std::cout << "id author: Blake Johnson\n";
std::cout << "uciok\n";
}
else if( token == "isready") std::cout << "readyok\n";
else if( token == "setoption"); //do options later
else if( token == "register"); //later
else if( token == "ucinewgame"); //do prep for new game
else if( token == "position") parsePosition(stream, newState);
else if (token == "go") parseGo(stream);
//Try and figure out a solution to poll the stream buffer. getline blocks which is not a solution
//uci says white space before message is possible so peeking is not a solution
else if (token == "stop");
else if(token == "quit") break;
else std::cout <<"not a command\n";
}
}
void UCI::parsePosition(std::istringstream &stream, returnState& newState)
{
logFile.open("log.txt" , std::ofstream::app);
move m = none;
std::string token, fen;
stream >> token;
if( token == "startpos")
{
pos.reset();
stream >> token;
}
else if ( token == "fen")
{
//credit to stockfish
while (stream >> token && token != "moves")
fen += token + " ";
std::cout<<fen;
logFile << fen << "\n";
pos.updatePositionFen(fen);
pos.printBoard();
}
else return;
while (stream >> token && ((m = moveCheck(token)) != none))
{
// returnState newState = returnState();
// newState = returnState();
logFile << token << " ";
//Ok this is a memory leak. fix this with an array of 256 structs
returnState * nnewState = new returnState();
pos.do_move(m, *nnewState);
pos.printBoard();
}
pos.printBoard();
logFile << "Ply: " << pos.get_ply() << "\n";
logFile.close();
}
void UCI::parseGo(std::istringstream &stream)
{
std::string token = "";
while(stream >> token){
if(token == "wtime") {
stream >> token;
wTime = std::stoi(token);
}
else if (token == "btime"){
stream >> token;
bTime = std::stoi(token);
}
else if (token == "winc"){
stream >> token;
wInc = std::stoi(token);
}
else if (token == "binc"){
stream >> token;
bInc = std::stoi(token);
}
else if (token == "depth"){
stream >> token;
depth = std::stoi(token);
}
else if (token == "movetime"){
stream >> token;
moveTime = std::stoi(token);
}
else if (token == "perft"){
stream >> token;
bitboard nodes = 0;
clock_t t;
std::cout <<"Starting Perft... \n\n";
t = clock();
nodes= perftDivide(5, pos);
t = clock() - t;
logFile.open("log.txt",std::ofstream::app);
std::cout << "nodes = " << nodes << " seconds: " << float(t)/CLOCKS_PER_SEC << std::endl;
std::cout << "totalNodes = " << pos.get_nodes() << "\n";
logFile << "nodes = " << nodes << " seconds: " << float(t)/CLOCKS_PER_SEC << std::endl;
logFile.close();
}
}
//search
pos.reset_nodes();
clock_t t;
t= clock();
rootSearch(pos);
t = clock() - t;
std::cout << "bestmove " << moveToString(pos.bestMove) << "\n";
std::cout << "nodes = " << pos.get_nodes() << " seconds: " << float(t)/CLOCKS_PER_SEC << std::endl;
int nps = pos.get_nodes() / (float(t)/CLOCKS_PER_SEC);
int k = 1;
if( (pos.get_material(white) + pos.get_material(black)) >= gamePhaseCutoff) k = 0;
std::cout << "info score cp "<<valueee<<" nodes " << pos.get_nodes() << " nps " << nps << " string " << k << "\n";
;
int totalstates = totalState(pos.get_returnState());
std::cout << " totalstates: " <<totalstates << "\n";
logFile.open("log.txt" , std::ofstream::app);
logFile << "info score cp "<<valueee<<" nodes " << pos.get_nodes() << " nps " << nps << " string " << k << " totalstates: " <<totalstates <<"\n";
logFile << "/////////////////////// \n";
logFile.close();
}
move UCI::moveCheck(std::string str)
{
candMoveList movs;
movs.end = generateAllLegal(pos, movs.end);
while(movs.start != movs.end)
{
// std::cout<< moveToString(movs.start -> mv) << "\n";
// std::cout<<str << "\n";
if(str == moveToString(movs.start -> mv))
{
return movs.start -> mv;
}
movs.start++;
}
return none;
}
std::string UCI::moveToString (move m)
{
//the only internal moves different to external are castling and promotions
//internally: castling is king to rook sq
//externally: castling is king src to king dest
//for promotions, just add the promo letter i.e. fromToPromo
std::string mv = " ";
std::string pieceletters = " nbrq";
square from ,to;
from = source_sq(m);
to = destination_sq(m);
mv[0] = square_to_file(from) + 'a';
mv[1] = square_to_rank(from) + '1';
mv[2] = square_to_file(to) + 'a';
mv[3] = square_to_rank(to) + '1';
if(move_flag(m) == promoting)
mv += pieceletters[promo_pieceType(m)];
if(move_flag(m) == castling){
if(to == a1){
mv[2] = 'c';
mv[3] = '1';
}
else if(to == h1){
mv[2] = 'g';
mv[3] = '1';
}
else if(to == a8){
mv[2] = 'c';
mv[3] = '8';
}
else if(to == h8){
mv[2] = 'g';
mv[3] = '8';
}
}
return mv;
}
int UCI::totalState(returnState *st){
if( st == NULL) return 0;
return totalState(st->prev) + 1;
}