summaryrefslogtreecommitdiff
path: root/Userland/Games/Chess/Engine.h
blob: 22c0c072d61146935fe6b0ad9bdb36e648f9bbb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
 * 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(const StringView& command);

    Engine(const Engine&) = delete;
    Engine& operator=(const Engine&) = delete;

    virtual void handle_bestmove(const Chess::UCI::BestMoveCommand&);

    template<typename Callback>
    void get_best_move(const Chess::Board& board, int time_limit, Callback&& callback)
    {
        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:
    Function<void(Chess::Move)> m_bestmove_callback;
    pid_t m_pid { -1 };
};