stellarUCI Chess engine written in C++20 |
git clone git://git.dimitrijedobrota.com/stellar.git |
Log | Files | Refs | README | LICENSE |
match.cpp (4749B)
0 #include "match.hpp"
1 #include "logger.hpp"
2 #include "repetition.hpp"
3 #include "timer.hpp"
5 uint16_t Match::id_t = 0;
6 Match::~Match() { logger::log(std::format("Match {}: destroyed", id), logger::Debug); }
7 Match::Match(Engine &white, Engine &black) : engines({&white, &black}) {
8 logger::log(std::format("Match {}: created", id), logger::Debug);
9 }
11 Game Match::play(Settings swhite, Settings sblack, const std::string fen = Game::startPosition) {
12 const std::string position = "position " + (fen == Game::startPosition ? "startpos" : "fen " + fen);
14 repetition::Table rtable;
15 Board board(fen);
16 Move move;
18 logger::log(std::format("Match {}: Play a game between {}(white) and {}(black)", id,
19 engines[0]->get_name(), engines[1]->get_name()));
20 Game game(id, engines[0]->get_name(), engines[1]->get_name(), fen);
22 engines[0]->send("ucinewgame");
23 engines[1]->send("ucinewgame");
25 color::Color turn = board.get_side();
26 while (true) {
27 const MoveList list = MoveList(board, false, true);
28 if (!list.size()) {
29 game.set_winner(color::other(turn));
30 break;
31 }
33 Engine *engine = engines[to_underlying(turn)];
34 engine->send(std::format("{} moves {}", position, game.get_moves()));
35 engine->send(get_go(swhite, sblack, turn));
37 uint64_t time_start = timer::get_ms();
39 std::string response;
40 while (true) {
41 response = engine->receive();
42 if (response.starts_with("bestmove")) break;
43 }
45 std::string move_str = response.substr(9);
46 if ((move = parse_move(list, move_str)) == Move() || !move.make(board)) {
47 logger::log(
48 std::format("Match {}: {} illegal {}", id, color::to_string(turn), (std::string)move));
49 game.set_terminate(Game::Illegal);
50 game.set_winner(color::other(turn));
51 break;
52 }
54 if (rtable.is_repetition(board.get_hash())) {
55 logger::log(std::format("Match {}: {} repetition", id, color::to_string(turn)));
56 game.set_terminate(Game::Repetition);
57 game.set_draw(true);
58 break;
59 }
61 rtable.push_hash(board.get_hash());
62 if (!move.is_repeatable()) rtable.push_null();
63 game.play(move);
65 uint64_t time_passed = timer::get_ms() - time_start;
66 if (turn == color::WHITE ? swhite.time <= time_passed : sblack.time <= time_passed) {
67 logger::log(std::format("Match {}: {} timeout", id, color::to_string(turn)));
68 game.set_terminate(Game::Timeout);
69 game.set_winner(color::other(turn));
70 break;
71 }
73 if (turn == color::WHITE && !swhite.depth) swhite.time -= time_passed;
74 if (turn == color::BLACK && !sblack.depth) sblack.time -= time_passed;
76 turn = color::other(turn);
77 }
79 if (!game.is_draw()) {
80 logger::log(std::format("Match {}: winner is {}", id, color::to_string(turn)));
81 } else {
82 logger::log(std::format("Match {}: ended in a draw", id));
83 }
85 std::swap(engines[0], engines[1]);
86 return game;
87 }
89 std::string Match::get_go(Settings &swhite, Settings &sblack, color::Color side) {
90 std::string go = "go";
91 if (side == color::WHITE && swhite.depth) go += " depth " + std::to_string(swhite.depth);
92 else {
93 if (side == color::WHITE && swhite.togo) go += " movestogo " + std::to_string(swhite.togo);
94 if (!sblack.depth && swhite.time) go += " wtime " + std::to_string(swhite.time);
95 if (swhite.inc) go += " winc " + std::to_string(swhite.inc);
96 if (swhite.movetime) go += " movetime " + std::to_string(swhite.movetime);
97 }
99 if (side == color::BLACK && sblack.depth) go += " depth " + std::to_string(sblack.depth);
100 else {
101 if (side == color::BLACK && sblack.togo) go += " movestogo " + std::to_string(sblack.togo);
102 if (!swhite.depth && sblack.time) go += " btime " + std::to_string(sblack.time);
103 if (sblack.inc) go += " binc " + std::to_string(sblack.inc);
104 if (swhite.movetime) go += " movetime " + std::to_string(sblack.movetime);
105 }
106 return go;
107 }
109 Move Match::parse_move(const MoveList list, const std::string &move_string) {
110 const square::Square source = square::from_coordinates(move_string.substr(0, 2));
111 const square::Square target = square::from_coordinates(move_string.substr(2, 2));
113 for (int i = 0; i < list.size(); i++) {
114 const Move crnt = list[i];
115 if (crnt.source() != source || crnt.target() != target) continue;
116 if (move_string[4] && tolower(piece::get_code(crnt.promoted())) != move_string[4]) continue;
117 return crnt;
118 }
119 return {};
120 }