added UCI: main loop and go command parser
This commit is contained in:
1 parent
1d6a9a8554
commit
8df4630bb4
4 files changed
+100
-6
No files matched your search
+86
-1
@@ -4,6 +4,7 @@
|
||||
#include <string>
|
||||
#include "types.h"
|
||||
#include "movegen.h"
|
||||
#include "search.h"
|
||||
|
||||
Move ParseMove(Board &board, const char* moveString) {
|
||||
GenerateMoves(board, board.sideToMove);
|
||||
@@ -80,7 +81,7 @@ Move ParseMove(Board &board, const char* moveString) {
|
||||
return Move();
|
||||
}
|
||||
|
||||
void ParsePosition(Board &board, char* command) {
|
||||
void ParsePosition(Board &board, const char* command) {
|
||||
// shifting pointer to where the token begins (skipping 'position' word)
|
||||
command += 9;
|
||||
|
||||
@@ -100,6 +101,7 @@ void ParsePosition(Board &board, char* command) {
|
||||
board.SetByFen(fen.c_str());
|
||||
} else if (*command == 's') { // startpos
|
||||
command += 9;
|
||||
board.SetByFen(StartingFen);
|
||||
}
|
||||
|
||||
// moves
|
||||
@@ -133,4 +135,87 @@ void ParsePosition(Board &board, char* command) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ParseGo(Board &board, const char* command) {
|
||||
// init depth
|
||||
int depth = -1;
|
||||
|
||||
// handle fixed depth search
|
||||
if (std::string(command).find("depth") != std::string::npos) {
|
||||
// skipping to the depth number
|
||||
command += 9;
|
||||
|
||||
depth = atoi(command);
|
||||
}
|
||||
|
||||
// search position
|
||||
SearchPosition(board, depth);
|
||||
}
|
||||
|
||||
void UCILoop(Board &board) {
|
||||
// reset STDIN & STDOUT buffers
|
||||
setbuf(stdin, NULL);
|
||||
setbuf(stdout, NULL);
|
||||
|
||||
// define user / GUI input buffer
|
||||
std::string input = "";
|
||||
|
||||
// print engine info
|
||||
std::cout << "id name Eleanor\n";
|
||||
std::cout << "id name rektdie\n";
|
||||
std::cout << "uciok\n";
|
||||
|
||||
// main loop
|
||||
while (true) {
|
||||
// reset user / GUI input
|
||||
getline(std::cin, input);
|
||||
|
||||
// make sure output reaches the GUI
|
||||
fflush(stdout);
|
||||
|
||||
// make sure input is available
|
||||
if (input[0] == '\n') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// parse UCI "isready" command
|
||||
if (input.find("isready") != std::string::npos) {
|
||||
std::cout << "readyok\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
// parse UCI "position" command
|
||||
if (input.find("position") != std::string::npos) {
|
||||
ParsePosition(board, input.c_str());
|
||||
continue;
|
||||
}
|
||||
|
||||
// parse UCI "ucinewgame" command
|
||||
if (input.find("ucinewgame") != std::string::npos) {
|
||||
ParsePosition(board, "position startpos");
|
||||
continue;
|
||||
}
|
||||
|
||||
// parse UCI "go" command
|
||||
if (input.find("go") != std::string::npos) {
|
||||
ParseGo(board, input.c_str());
|
||||
continue;
|
||||
}
|
||||
|
||||
// parse UCI "quit" command
|
||||
if (input.find("quit") != std::string::npos) {
|
||||
// stop the loop
|
||||
break;
|
||||
}
|
||||
|
||||
// parse UCI "uci" command
|
||||
if (input.find("uci") != std::string::npos) {
|
||||
// print engine info
|
||||
std::cout << "id name Eleanor\n";
|
||||
std::cout << "id name rektdie\n";
|
||||
std::cout << "uciok\n";
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user