Skip to content

Commit 4ed3794

Browse files
committed
We have fixed some **critical** changes which leads to issue Invalid PV and user input #1
1 parent eaceefa commit 4ed3794

File tree

2 files changed

+66
-28
lines changed

2 files changed

+66
-28
lines changed

search.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ int16_t alphaBeta(chess::Position &board, int16_t alpha, int16_t beta, int depth
158158
if (pv.cmove > 0) {
159159
std::cout << " pv";
160160
for (int i = 0; i < pv.cmove; ++i) {
161-
std::cout << " " << chess::Move(pv.argmove[i]);
161+
std::cout << " " << chess::uci::moveToUci(chess::Move(pv.argmove[i]), board.chess960());
162162
}
163163
}
164164

uci.cpp

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -76,29 +76,52 @@ inline void handle_stop() { search::stop_requested = true; }
7676
inline void handle_display() { std::cout << board << std::endl; }
7777

7878
inline void handle_eval() { trace(board); }
79-
8079
void handle_position(std::istringstream& iss) {
81-
std::string sub;
82-
iss >> sub;
80+
std::string token;
81+
iss >> token;
8382

84-
if (sub == "startpos") {
83+
if (token == "startpos") {
8584
board.setFen(chess::constants::STARTPOS);
86-
if (iss >> sub && sub == "moves") {
87-
while (iss >> sub) board.makeMove(chess::uci::uciToMove(board, sub));
85+
iss >> token;
86+
} else if (token == "fen") {
87+
std::vector<std::string> fen_parts;
88+
while (iss >> token && token != "moves") {
89+
fen_parts.push_back(token);
8890
}
89-
} else if (sub == "fen") {
90-
std::string fen, temp;
91-
int fenParts = 6;
92-
while (fenParts-- && iss >> temp) fen += temp + " ";
91+
92+
// Assign default values if necessary
93+
while (fen_parts.size() < 6) {
94+
switch (fen_parts.size()) {
95+
case 1: fen_parts.push_back("w"); break; // Active color
96+
case 2: fen_parts.push_back("-"); break; // Castling availability
97+
case 3: fen_parts.push_back("-"); break; // En passant target square
98+
case 4: fen_parts.push_back("0"); break; // Halfmove clock
99+
case 5: fen_parts.push_back("1"); break; // Fullmove number
100+
}
101+
}
102+
103+
// Reconstruct the FEN string
104+
std::string fen = fen_parts[0];
105+
for (size_t i = 1; i < 6; ++i) {
106+
fen += " " + fen_parts[i];
107+
}
108+
93109
board.setFen(fen);
94-
if (iss >> sub && sub == "moves") {
95-
while (iss >> sub) board.makeMove(chess::uci::uciToMove(board, sub));
110+
}
111+
112+
if (token == "moves") {
113+
while (iss >> token) {
114+
chess::Move mv = chess::uci::uciToMove(board, token);
115+
fprintf(stderr, "[DEBUG] User interface received move: %s, Internal interface received move: ", token.c_str());
116+
std::cout << mv << std::endl;
117+
board.makeMove(mv);
96118
}
97119
}
98120

99121
search::tt.clear();
100122
}
101123

124+
102125
void handle_bench() {
103126
chess::Position board;
104127
uint64_t nodes = 0;
@@ -138,10 +161,14 @@ void handle_go(std::istringstream& iss) {
138161
std::string sub;
139162

140163
while (iss >> sub) {
141-
if (sub == "depth") iss >> depth;
142-
else if (sub == "movetime") iss >> movetime;
143-
else if (sub == "wtime" && white) iss >> movetime;
144-
else if (sub == "btime" && !white) iss >> movetime;
164+
if (sub == "depth")
165+
iss >> depth;
166+
else if (sub == "movetime")
167+
iss >> movetime;
168+
else if (sub == "wtime" && white)
169+
iss >> movetime;
170+
else if (sub == "btime" && !white)
171+
iss >> movetime;
145172
else if (sub == "infinite") {
146173
infinite = true;
147174
depth = MAX_PLY;
@@ -171,16 +198,27 @@ void uci_loop() {
171198
std::istringstream iss(line);
172199
std::string token;
173200
iss >> token;
174-
if (token == "ucinewgame") handle_ucinewgame();
175-
else if (token == "uci") handle_uci();
176-
else if (token == "isready") handle_isready();
177-
else if (token == "quit") handle_quit();
178-
else if (token == "stop") handle_stop();
179-
else if (token == "setoption") handleSetOption(line);
180-
else if (token == "position") handle_position(iss);
181-
else if (token == "go") handle_go(iss);
182-
else if (token == "d") handle_display();
183-
else if (token == "bench") handle_bench();
184-
else if (token == "eval") handle_eval();
201+
if (token == "ucinewgame")
202+
handle_ucinewgame();
203+
else if (token == "uci")
204+
handle_uci();
205+
else if (token == "isready")
206+
handle_isready();
207+
else if (token == "quit")
208+
handle_quit();
209+
else if (token == "stop")
210+
handle_stop();
211+
else if (token == "setoption")
212+
handleSetOption(line);
213+
else if (token == "position")
214+
handle_position(iss);
215+
else if (token == "go")
216+
handle_go(iss);
217+
else if (token == "d")
218+
handle_display();
219+
else if (token == "bench")
220+
handle_bench();
221+
else if (token == "eval")
222+
handle_eval();
185223
}
186224
}

0 commit comments

Comments
 (0)