diff --git a/source/main.cpp b/source/main.cpp index 347717c..996e312 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -11,8 +11,7 @@ int main() { Board board; - ParsePosition(board, "position startpos moves a2a4 b7b6"); - board.PrintBoard(); + UCILoop(board); return 0; } \ No newline at end of file diff --git a/source/search.cpp b/source/search.cpp index 46cbe66..e1f4b37 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -54,6 +54,7 @@ Move NegaMaxHandler(Board &board, int depth) { } void SearchPosition(Board &board, int depth) { - std::cout << "best move: "; - NegaMaxHandler(board, depth).PrintMove(); + Move bestMove = NegaMaxHandler(board, depth); + std::cout << "bestmove " << squareCoords[bestMove.MoveFrom()] + << squareCoords[bestMove.MoveTo()] << '\n'; } \ No newline at end of file diff --git a/source/uci.cpp b/source/uci.cpp index 3c4e99a..5fed146 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -4,6 +4,7 @@ #include #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; + } + } } \ No newline at end of file diff --git a/source/uci.h b/source/uci.h index 058c83f..8fe18d5 100644 --- a/source/uci.h +++ b/source/uci.h @@ -4,4 +4,13 @@ // Returns Move object from a string Move ParseMove(Board &board, const char* moveString); -void ParsePosition(Board &board, char* command); \ No newline at end of file + +// Initializes the position with given parameters +void ParsePosition(Board &board, const char* command); + +// Parse UCI "go" command +// GUI sends the engine this command if it should start searching for the best move +void ParseGo(Board &board, const char* command); + +// main UCI loop +void UCILoop(Board &board); \ No newline at end of file