#include "uci.hpp" #include "board.hpp" #include "bot.hpp" #include "fen.hpp" #include "moves.hpp" #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; 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 b.canEnpassant = false; b.state = TURN; b.turn = true; b.castle = ""; b.halfMoveClock = 0; b.MoveClock = 0; setBoardFen(startingFEN, &b); Piece EmptyPiece = { false, NONEPIECE, false, }; for (int i = 0; i < 64; i++) { b.pieces[i] = EmptyPiece; }; return b; }; void Uci() { Game game = initBoard("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); string line; while (getline(cin, line)) { string cmd = line; LogUci("GUI -> ENGINE: " + line); transform(cmd.begin(), cmd.end(), cmd.begin(), ::tolower); if (cmd == "quit") { break; } if (cmd == "uci") { cout << "uciok\n"; cout.flush(); } else if (cmd == "ucinewgame") { game = initBoard("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); } else if (cmd == "isready") { cout << "readyok\n"; cout.flush(); } else if (cmd.starts_with("go")) { Move best = EngineGetBestMove(&game); cout << "bestmove "; cout << (char)(best.From.file + 'a') << 8 - best.From.rank << (char)(best.To.file + 'a') << 8 - best.To.rank; if (best.promotion != NONEPIECE) { switch (best.promotion) { case QUEEN: cout << "q"; break; case ROOK: cout << "r"; break; case KNIGHT: cout << "n"; break; case BISHOP: cout << "b"; break; default: assert(false && "Unexepted promotion type"); } } 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); MakeMove(move, &game); } } } }