stellar

UCI Chess engine written in C++20
git clone git://git.dimitrijedobrota.com/stellar.git
Log | Files | Refs | README | LICENSE |

uci.cpp (4753B)


1 #include <iostream> 2 #include <limits> 3 #include <sstream> 4 #include <string> 5 6 #include "engine.hpp" 7 #include "stellar_version.hpp" 8 #include "timer.hpp" 9 #include "uci.hpp" 10 11 namespace uci { 12 13 void communicate(const uci::Settings *settings) { 14 if (!settings->infinite && timer::get_ms() > settings->stoptime) { 15 settings->stopped = true; 16 return; 17 } 18 } 19 20 inline bool parse_move(const Board &board, Move &move, const std::string &move_string) { 21 const square::Square source = square::from_coordinates(move_string.substr(0, 2)); 22 const square::Square target = square::from_coordinates(move_string.substr(2, 2)); 23 24 const MoveList list(board); 25 for (int i = 0; i < list.size(); i++) { 26 const Move crnt = list[i]; 27 if (crnt.source() == source && crnt.target() == target) { 28 if (move_string[4]) { 29 if (tolower(piece::get_code(crnt.promoted())) != move_string[4]) continue; 30 } 31 move = crnt; 32 return true; 33 } 34 } 35 return false; 36 } 37 38 void loop(void) { 39 static Settings settings; 40 static std::string line, command; 41 static Move move; 42 43 Board board; 44 45 while (true) { 46 std::getline(std::cin, line); 47 std::istringstream iss(line); 48 iss >> command; 49 if (command == "quit") { 50 break; 51 } else if (command == "uci") { 52 std::cout << "id name Stellar " << getStellarVersion() << "\n"; 53 std::cout << "id author Dimitrije Dobrota\n"; 54 std::cout << "uciok\n"; 55 } else if (command == "debug") { 56 iss >> command; 57 settings.debug = (command == "on"); 58 } else if (command == "isready") { 59 std::cout << "readyok\n"; 60 } else if (command == "ucinewgame") { 61 settings = Settings(); 62 settings.board = Board(start_position); 63 } else if (command == "position") { 64 settings.madeMoves.clear(); 65 iss >> command; 66 if (command == "startpos") { 67 settings.board = Board(start_position); 68 } else if (command == "fen") { 69 iss >> command; 70 settings.board = Board(line.substr(13)); 71 } 72 73 while (iss >> command) 74 if (command == "moves") break; 75 76 board = settings.board; 77 while (iss >> command) { 78 if (!parse_move(board, move, command)) break; 79 settings.madeMoves.push(move); 80 move.make(board); 81 } 82 } else if (command == "go") { 83 settings.searchMoves.clear(); 84 uint64_t wtime = 0, btime = 0, movetime = 0; 85 uint16_t winc = 0, binc = 0, movestogo = 60; 86 87 while (iss >> command) { 88 if (command == "wtime") 89 iss >> wtime; 90 else if (command == "btime") 91 iss >> btime; 92 else if (command == "winc") 93 iss >> winc; 94 else if (command == "binc") 95 iss >> binc; 96 else if (command == "depth") 97 iss >> settings.depth; 98 else if (command == "nodes") 99 iss >> settings.nodes; 100 else if (command == "movetime") 101 iss >> movetime; 102 else if (command == "movestogo") 103 iss >> movestogo; 104 else if (command == "ponder") 105 settings.ponder = true; 106 else if (command == "mate") 107 settings.mate = true; 108 else if (command == "infinite") 109 settings.infinite = true; 110 else if (command == "searchmoves") { 111 while (iss >> command) { 112 if (!parse_move(board, move, command)) break; 113 settings.searchMoves.push(move); 114 } 115 } 116 } 117 118 settings.starttime = timer::get_ms(); 119 uint64_t time = (board.get_side() == color::WHITE) ? wtime : btime; 120 121 if (movetime != 0) { 122 time = movetime; 123 movestogo = 1; 124 } else if (time != 0) { 125 uint16_t inc = (board.get_side() == color::WHITE) ? winc : binc; 126 time /= movestogo; 127 time -= 50; 128 settings.stoptime = settings.starttime + time + inc; 129 settings.infinite = false; 130 } else 131 settings.infinite = true; 132 133 const Move best = engine::search_position(settings); 134 std::cout << "bestmove " << best << '\n'; 135 136 settings.newgame = false; 137 } 138 } 139 } 140 } // namespace uci