stellar

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

uci.cpp (4753B)


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