From ab198bd13a44344989c34ee1fda1e56fa00cbe1f Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 27 Jul 2026 20:44:07 +0200 Subject: making it uci compatible --- src/uci.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 77 insertions(+), 9 deletions(-) (limited to 'src/uci.cpp') diff --git a/src/uci.cpp b/src/uci.cpp index debf886..d436cb1 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -3,14 +3,34 @@ #include "bot.hpp" #include "fen.hpp" #include "moves.hpp" + #include #include #include #include #include #include +#include #include +#include +#include +#include + +std::ofstream uciLog("uci_log.txt", std::ios::app); + +void LogUci(const std::string &message) { + if (!uciLog.is_open()) + return; + + auto now = std::chrono::system_clock::now(); + auto time = std::chrono::system_clock::to_time_t(now); + + uciLog << "[" << std::put_time(std::localtime(&time), "%H:%M:%S") << "] " + << message << "\n"; + + uciLog.flush(); +} using namespace std; bool isValidChessRank(char rank) { if (rank >= '1' && rank <= '8') { @@ -26,6 +46,18 @@ bool isValidChessFile(char rank) { return false; } } +Move UciToMove(string uci) { + Position from; + Position to; + + from.file = uci[0] - 'a'; + from.rank = '8' - uci[1]; + + to.file = uci[2] - 'a'; + to.rank = '8' - uci[3]; + + return {from, to}; +} static Game initBoard(string startingFEN) { Game b; b.enPassant = IndexToPosition(0); // default value @@ -38,7 +70,7 @@ static Game initBoard(string startingFEN) { setBoardFen(startingFEN, &b); return b; }; -void UciInit() { +void Uci() { Game game = initBoard("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); @@ -46,28 +78,64 @@ void UciInit() { while (getline(cin, line)) { string cmd = line; + LogUci("GUI -> ENGINE: " + line); + transform(cmd.begin(), cmd.end(), cmd.begin(), ::tolower); - transform(cmd.begin(), cmd.end(), cmd.begin(), ::toupper); - - if (cmd == "QUIT") { + if (cmd == "quit") { break; } - if (cmd == "UCI") { + if (cmd == "uci") { cout << "uciok\n"; cout.flush(); - } else if (cmd == "ISREADY") { + } else if (cmd == "isready") { cout << "readyok\n"; cout.flush(); - } else if (cmd.starts_with("GO")) { + } else if (cmd.starts_with("go")) { Move best = EngineGetBestMove(&game); cout << "bestmove "; - cout << (char)(best.From.file + 'a') - << 8 - best.From.rank + cout << (char)(best.From.file + 'a') << 8 - best.From.rank << (char)(best.To.file + 'a') << 8 - best.To.rank; println(); cout.flush(); + } else if (cmd.starts_with("position")) { + stringstream ss(line); + string token; + + ss >> token; // position + + ss >> token; + + if (token == "startpos") { + game = initBoard( + "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); + } else if (token == "fen") { + string fen; + string part; + + // FEN has spaces, need 6 parts + for (int i = 0; i < 6; i++) { + ss >> part; + if (i) + fen += " "; + fen += part; + } + + game = initBoard(fen); + } + + // check for moves + while (ss >> token) { + if (token == "moves") + break; + } + + while (ss >> token) { + Move move = UciToMove(token); + + PlayMove(move, &game); + } } } } -- cgit v1.2.3