diff options
| author | Adam <adammegarules1@gmail.com> | 2026-08-11 13:53:26 +0200 |
|---|---|---|
| committer | Adam <adammegarules1@gmail.com> | 2026-08-11 13:53:26 +0200 |
| commit | 4dc1623d5e313a9e03fc8e3fc96bb495325a3283 (patch) | |
| tree | 8653eb1040ef405ad7d73fca4bab496d801c4337 /src/uci.cpp | |
| parent | 8afc1355db1e78d1f4c4d13071d23f43efe1f20d (diff) | |
improving code
Diffstat (limited to 'src/uci.cpp')
| -rw-r--r-- | src/uci.cpp | 240 |
1 files changed, 118 insertions, 122 deletions
diff --git a/src/uci.cpp b/src/uci.cpp index 23fe123..cc5ae18 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -1,6 +1,8 @@ +#include <array> #include <cassert> #include <cctype> #include <chrono> +#include <cstddef> #include <cstdint> #include <cstdlib> #include <fstream> @@ -23,24 +25,7 @@ static const std::string starting_fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; -std::ofstream uciLog("cache/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(const string &uci) { +Move UciToMove(const std::string &uci) { Position from; Position to; PieceType promotion = NONEPIECE; @@ -71,7 +56,7 @@ Move UciToMove(const string &uci) { break; default: assert(false && "Unexpected promotion type"); - cout << "Unexpected promotion type"; + std::cout << "Unexpected promotion type"; exit(1); } } @@ -115,7 +100,7 @@ static void PrintBitboard(uint64_t bb, const std::string &name) { } static void DebugBitboards(const Game *g) { - static constexpr const char *pieceNames[] = { + static constexpr const std::array<std::string, 7> pieceNames = { "None", "Pawn", "Knight", "Bishop", "Rook", "Queen", "King", }; @@ -135,7 +120,8 @@ static void DebugBitboards(const Game *g) { std::cout << (color ? "White\n" : "Black\n"); for (int type = PAWN; type <= KING; ++type) { - PrintBitboard(g->PieceBitboards[color][type], pieceNames[type]); + PrintBitboard(g->PieceBitboards[color][type], + pieceNames[static_cast<size_t>(type)]); } } } @@ -145,16 +131,16 @@ static void printPromotionLetter(PieceType p) { } switch (p) { case QUEEN: - cout << "q"; + std::cout << "q"; break; case ROOK: - cout << "r"; + std::cout << "r"; break; case KNIGHT: - cout << "n"; + std::cout << "n"; break; case BISHOP: - cout << "b"; + std::cout << "b"; break; default: assert(false && "Unexpected promotion type"); @@ -168,7 +154,7 @@ static void printBoard(Game *g) { Piece piece = g->pieces[(rank * 8) + file]; if (piece.type == NONEPIECE) { - print(" 00"); + std::print(" 00"); continue; }; std::print(" {0}{1}", static_cast<int>(piece.type), @@ -178,14 +164,108 @@ static void printBoard(Game *g) { std::print(" | {0}\n", rank + 1); } } +static void GoCommand(Game *game, std::string &cmd) { + std::stringstream ss(cmd); + + std::string token; + int depth = -1; + + int wtime = -1; + int btime = -1; + + int wncr = 0; + int bncr = 0; + + ss >> token; // go + + while (ss >> token) { + if (token == "depth") { + ss >> depth; + } + if (token == "wtime") { + ss >> wtime; + } + if (token == "btime") { + ss >> btime; + } + if (token == "incr") { + ss >> wncr; + bncr = wncr; + } + if (token == "winc") { + ss >> wncr; + } + if (token == "binc") { + ss >> bncr; + } + } + + if (depth != -1 && depth <= 0) { + std::cout << "Fatal: Depth must be positive integer"; + exit(1); + } + move_options options = { + .wtime = wtime, + .btime = btime, + .wncr = wncr, + .bncr = bncr, + }; + Move best = GetBestMove(game, depth, options); + + std::cout << "bestmove "; + std::cout << static_cast<char>(best.From.file + 'a') << 1 + best.From.rank + << static_cast<char>(best.To.file + 'a') << 1 + best.To.rank; + + printPromotionLetter(best.promotion); + + std::cout << "\n"; + std::cout.flush(); +} +static void positionCommnad(Game *game, std::string &cmd) { + std::stringstream ss(cmd); + std::string token; + + ss >> token; // position + + ss >> token; + + if (token == "startpos") { + *game = initBoard(starting_fen, game->Transpositions); + } else if (token == "fen") { + std::string fen; + std::string part; + + // FEN has spaces, need 6 parts + for (int i = 0; i < 6; i++) { + ss >> part; + if (i != 0) { + fen += ' '; + } + fen += part; + } + + *game = initBoard(fen, game->Transpositions); + } + + // check for moves + while (ss >> token) { + if (token == "moves") { + break; + } + } + + while (ss >> token) { + Move move = UciToMove(token); + + MakeMove(move, game); + } +} void Uci() { - unordered_map<uint64_t, TranspositionsEntry> transpositions = {}; + std::unordered_map<uint64_t, TranspositionsEntry> transpositions = {}; Game game = initBoard(starting_fen, &transpositions); - string cmd; - - while (getline(cin, cmd)) { - LogUci("GUI -> ENGINE: " + cmd); + std::string cmd; + while (getline(std::cin, cmd)) { if (cmd == "quit") { return; } @@ -196,9 +276,9 @@ void Uci() { DebugBitboards(&game); } if (cmd == "uci") { - cout << "id name " << engine_info(); - cout << "uciok\n"; - cout.flush(); + std::cout << "id name " << engine_info(); + std::cout << "uciok\n"; + std::cout.flush(); } if (cmd == "ucinewgame") { game = @@ -206,98 +286,14 @@ void Uci() { &transpositions); } if (cmd == "isready") { - cout << "readyok\n"; - cout.flush(); + std::cout << "readyok\n"; + std::cout.flush(); } if (cmd.starts_with("go")) { - stringstream ss(cmd); - - string token; - int depth = -1; - - int wtime = -1; - int btime = -1; - - int wncr = 0; - int bncr = 0; - - ss >> token; // go - - while (ss >> token) { - if (token == "depth") { - ss >> depth; - } - if (token == "wtime") { - ss >> wtime; - } - if (token == "btime") { - ss >> btime; - } - if (token == "incr") { - ss >> wncr; - bncr = wncr; - } - } - - if (depth != -1 && depth <= 0) { - cout << "Fatal: Depth must be positive integer"; - exit(1); - } - move_options options = { - .wtime = wtime, - .btime = btime, - .wncr = wncr, - .bncr = bncr, - }; - Move best = GetBestMove(&game, depth, options); - - cout << "bestmove "; - cout << static_cast<char>(best.From.file + 'a') << 1 + best.From.rank - << static_cast<char>(best.To.file + 'a') << 1 + best.To.rank; - - printPromotionLetter(best.promotion); - - cout << "\n"; - cout.flush(); + GoCommand(&game, cmd); } if (cmd.starts_with("position")) { - stringstream ss(cmd); - string token; - - ss >> token; // position - - ss >> token; - - if (token == "startpos") { - game = initBoard(starting_fen, &transpositions); - } 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 != 0) { - fen += " "; - } - fen += part; - } - - game = initBoard(fen, &transpositions); - } - - // check for moves - while (ss >> token) { - if (token == "moves") { - break; - } - } - - while (ss >> token) { - Move move = UciToMove(token); - - MakeMove(move, &game); - } + positionCommnad(&game, cmd); } } } |
