diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/fen.cpp | 3 | ||||
| -rw-r--r-- | src/main.cpp | 2 | ||||
| -rw-r--r-- | src/uci.cpp | 86 | ||||
| -rw-r--r-- | src/uci.hpp | 6 |
4 files changed, 82 insertions, 15 deletions
diff --git a/src/fen.cpp b/src/fen.cpp index 21ea778..7597143 100644 --- a/src/fen.cpp +++ b/src/fen.cpp @@ -1,7 +1,6 @@ #include "fen.hpp" #include "board.hpp" #include <cctype> -#include <cstdio> #include <cstdlib> #include <print> #include <string> @@ -27,8 +26,6 @@ void setBoardFen(std::string fen, Game *b) { parserState state = POSITION; for (uint i = 0; i < fen.length(); i++) { - std::printf("state: %d\n", state); - std::printf("doing: %c\n", fen[i]); if (fen[i] == ' ') { if (state == POSITION) { state = TURN; diff --git a/src/main.cpp b/src/main.cpp index 7b7e6fe..dea578b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -46,6 +46,6 @@ int main(int argc, char **argv) { if (argc > 1 && std::string(argv[1]) == "--repl") return startREPL(); - UciInit(); + Uci(); return 0; } 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 <algorithm> #include <cctype> #include <cstdint> #include <cstdlib> #include <iostream> #include <print> +#include <sstream> #include <string> +#include <chrono> +#include <fstream> +#include <iomanip> + +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); + } } } } diff --git a/src/uci.hpp b/src/uci.hpp index 9ef939d..18e216e 100644 --- a/src/uci.hpp +++ b/src/uci.hpp @@ -2,12 +2,14 @@ #define SRC_UCI_H_ #include "board.hpp" +#include <string> #include <vector> int startREPL(); void PrintMoves(const std::vector<Move> &moves); bool isValidChessRank(char rank); bool isValidChessFile(char rank); - -void UciInit(); +Move UciToMove(std::string uci); +void LogUci(const std::string &message); +void Uci(); #endif /* SRC_UCI_H_ */ |
