Files
ladybird/Userland/Games/Chess/Engine.h
Tim Ledbetter 8b6c538f2a Chess: Gracefully handle ChessEngine disconnections
The GUI now tracks when it becomes disconnected from ChessEngine.
If not currently waiting for a move from ChessEngine, it will
automatically reconnect on the next engine move. If a disconnection
occurs while waiting for a move, the player is asked whether they
want to try again or not.
2023-04-20 09:59:18 +01:00

49 lines
1.2 KiB
C++

/*
* Copyright (c) 2020, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Function.h>
#include <LibChess/UCIEndpoint.h>
#include <sys/types.h>
class Engine : public Chess::UCI::Endpoint {
C_OBJECT(Engine)
public:
virtual ~Engine() override;
Engine(StringView command);
Engine(Engine const&) = delete;
Engine& operator=(Engine const&) = delete;
Function<void()> on_connection_lost;
virtual void handle_bestmove(Chess::UCI::BestMoveCommand const&) override;
virtual void handle_unexpected_eof() override;
template<typename Callback>
void get_best_move(Chess::Board const& board, int time_limit, Callback&& callback)
{
if (!m_connected)
connect_to_engine_service();
send_command(Chess::UCI::PositionCommand({}, board.moves()));
Chess::UCI::GoCommand go_command;
go_command.movetime = time_limit;
send_command(go_command);
m_bestmove_callback = move(callback);
}
private:
void quit();
void connect_to_engine_service();
DeprecatedString m_command;
Function<void(ErrorOr<Chess::Move>)> m_bestmove_callback;
bool m_connected { false };
};