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
+1
-2
@@ -11,8 +11,7 @@ int main() {
|
|||||||
|
|
||||||
Board board;
|
Board board;
|
||||||
|
|
||||||
ParsePosition(board, "position startpos moves a2a4 b7b6");
|
UCILoop(board);
|
||||||
board.PrintBoard();
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
+3
-2
@@ -54,6 +54,7 @@ Move NegaMaxHandler(Board &board, int depth) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SearchPosition(Board &board, int depth) {
|
void SearchPosition(Board &board, int depth) {
|
||||||
std::cout << "best move: ";
|
Move bestMove = NegaMaxHandler(board, depth);
|
||||||
NegaMaxHandler(board, depth).PrintMove();
|
std::cout << "bestmove " << squareCoords[bestMove.MoveFrom()]
|
||||||
|
<< squareCoords[bestMove.MoveTo()] << '\n';
|
||||||
}
|
}
|
||||||
+86
-1
@@ -4,6 +4,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
|
#include "search.h"
|
||||||
|
|
||||||
Move ParseMove(Board &board, const char* moveString) {
|
Move ParseMove(Board &board, const char* moveString) {
|
||||||
GenerateMoves(board, board.sideToMove);
|
GenerateMoves(board, board.sideToMove);
|
||||||
@@ -80,7 +81,7 @@ Move ParseMove(Board &board, const char* moveString) {
|
|||||||
return Move();
|
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)
|
// shifting pointer to where the token begins (skipping 'position' word)
|
||||||
command += 9;
|
command += 9;
|
||||||
|
|
||||||
@@ -100,6 +101,7 @@ void ParsePosition(Board &board, char* command) {
|
|||||||
board.SetByFen(fen.c_str());
|
board.SetByFen(fen.c_str());
|
||||||
} else if (*command == 's') { // startpos
|
} else if (*command == 's') { // startpos
|
||||||
command += 9;
|
command += 9;
|
||||||
|
board.SetByFen(StartingFen);
|
||||||
}
|
}
|
||||||
|
|
||||||
// moves
|
// 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+10
-1
@@ -4,4 +4,13 @@
|
|||||||
|
|
||||||
// Returns Move object from a string
|
// Returns Move object from a string
|
||||||
Move ParseMove(Board &board, const char* moveString);
|
Move ParseMove(Board &board, const char* moveString);
|
||||||
void ParsePosition(Board &board, char* command);
|
|
||||||
|
// 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);
|
||||||
Reference in new issue
Block a user