Belofte version 2.1.9
A promising chess program using the UCI or Winboard interface
engineinterface.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------+
2 * File: engineinterface.h
3 * Project: part of belofte - A Promising Chess Program
4 * Author: yves
5 * SPDX-License-Identifier: GPL-2.0-only
6+----------------------------------------------------------------------*/
7
8#if !defined(ENGINEINTERFACE_H)
9#define ENGINEINTERFACE_H
10
11//-----------------------------------------------------------------------
12
13class quitCommandException : public std::exception
14{
15 const char * what () const noexcept override
16 { return "Quit command"; }
17};
18
19//-----------------------------------------------------------------------
20
21typedef std::map<std::string, bool> commandList_t;
22typedef std::map<std::string, engineUserCommand*> engineCommands_t;
23
24#if defined(__GNUC__)
25#pragma GCC diagnostic push
26#pragma GCC diagnostic ignored "-Wunused-parameter"
27#endif
28
29/**
30 * implementation of user interface
31 */
33public:
34 explicit engineInterface(std::string const& s, std::string const& h = "");
35 virtual ~engineInterface();
36
37 // deny creation of copy/move constructor because of pointer members
42
43 void execute(std::string const& command, std::string const& params);
44
45 virtual void setRunning(bool const r)
46 { m_running = r; }
47 virtual bool isRunning() const
48 { return m_running; }
49
50 virtual void sendPrompt()
51 {}
52 virtual void sendResponse(std::string const& response);
53 virtual void sendInvalidMove(std::string const& info, std::string const& reason)
54 { sendError("Illegal move", info); }
55 virtual void sendError(std::string const& error, std::string const& description);
56 virtual void sendHelp(std::string const& args);
57 virtual void sendInfo(std::string const& info)
58 {}
59 virtual void sendDebug(int const l, std::string const& info)
60 {}
61
62 virtual void sendMove(bBoard& b, bMove const& m);
63 virtual void sendResult(bBoard const& b, gameResult_t const gr) const;
64 virtual void sendInfoSearchStart(std::string const& info);
65
66 virtual void sendInfoDepth(int depth, int seldepth,
67 int64_t nodes, int nps)
68 {}
69 virtual void sendInfoSearching(bBoard const& b, int const nLogDepth,
70 depth_t const nMaxDepth,
71 bScore const sc, int64_t const t, int64_t const nodes) const
72 {}
73 virtual void sendInfoSearching(bBoard const& b, int const nLogDepth,
74 depth_t const nMaxDepth, std::string const& comment,
75 bScore const sc, int64_t const t, int64_t const nodes) const
76 {}
77 virtual void sendInfoCurrMove(bBoard const& b,
78 depth_t const nCurDepth,
79 bMove const& m, movenum_t const moveid,
80 int64_t const nodes) const
81 {}
82
83 std::string const& getHint() const
84 { return m_hint; }
85
86 operator std::string() const&
87 { return const_cast<std::string const&>(m_name); }
88
89public:
90 static std::string scoreAsString(bScore const score);
91 virtual std::string engineGameResultString(gameResult_t const gr) const;
92
93protected:
94 void attachCommand(belofte::stringList const& sCommands,
95 bool const published = true);
96 virtual std::string scoreString(bScore const score) const
97 { return engineInterface::scoreAsString(score); }
98
100 static engineCommands_t* instance = new engineCommands_t();
101 return *instance;
102 }
103
104private:
105 void initCommand(std::vector<engineUserCommand *> cmds);
106
107 std::string m_name;
108 std::string m_hint;
109 commandList_t m_allowedCommands;
110 bool m_running = false;
111};
112
113//-----------------------------------------------------------------------
114
115/**
116 * implementation of specific implementation
117 */
119public:
120 BelofteMode();
121 ~BelofteMode() override
122 {}
123
124 void sendPrompt() override;
125};
126
127//-----------------------------------------------------------------------
128
129class UCIMode : public engineInterface {
130public:
131 explicit UCIMode(std::string const& s = "uci");
132 ~UCIMode() override
133 {}
134
135 // UCI mode does not have a persistent running state
136 bool isRunning() const override
137 { return false; }
138
139 void sendInfo(std::string const& info) override;
140 void sendDebug(int const l, std::string const& info) override;
141 void sendMove(bBoard& b, bMove const& m) override;
142 void sendResult(bBoard const& b, gameResult_t const gr) const override;
143 void sendInfoDepth(int depth, int seldepth, int64_t nodes,
144 int nps) override;
145 void sendInfoSearching(bBoard const& b, int const nLogDepth,
146 depth_t const nMaxDepth,
147 bScore const sc, int64_t const t,
148 int64_t const nodes) const override;
149 void sendInfoSearching(bBoard const& b, int const nLogDepth,
150 depth_t const nMaxDepth, std::string const& comment,
151 bScore const sc, int64_t const t,
152 int64_t const nodes) const override;
153 void sendInfoCurrMove(bBoard const& b,
154 depth_t const nCurDepth,
155 bMove const& m, movenum_t const moveid,
156 int64_t const nodes) const override;
157
158protected:
159 std::string scoreString(bScore const score) const override;
160};
161
162#if defined(__GNUC__)
163#pragma GCC diagnostic pop
164#endif
165
166//-----------------------------------------------------------------------
167
169public:
170 explicit XboardMode(std::string const& s = "xboard");
171 ~XboardMode() override
172 {}
173
174 void sendInfo(std::string const& info) override;
175 void sendDebug(int const l, std::string const& info) override;
176 void sendResult(bBoard const& b, gameResult_t const gr) const override;
177 void sendInvalidMove(std::string const& info,
178 std::string const& reason) override;
179 void sendInfoSearching(bBoard const& b, int const nLogDepth,
180 depth_t const nMaxDepth,
181 bScore const sc, int64_t const t,
182 int64_t const nodes) const override;
183
184 std::string engineGameResultString(gameResult_t const gr) const override;
185
186protected:
187 std::string scoreString(bScore const score) const override;
188};
189
190//-----------------------------------------------------------------------
191
192typedef std::map<std::string, engineInterface*> engineInterfaces_t;
193
194//-----------------------------------------------------------------------
195
196#endif // defined ENGINEINTERFACE_H
197
198// eof
std::map< std::string, bool > commandList_t
Definition belofte.h:140
uint_fast8_t movenum_t
Definition belofte.h:100
std::map< std::string, engineUserCommand * > engineCommands_t
Definition belofte.h:141
int_fast8_t depth_t
Definition belofte.h:103
void sendPrompt() override
~BelofteMode() override
void sendDebug(int const l, std::string const &info) override
~UCIMode() override
void sendResult(bBoard const &b, gameResult_t const gr) const override
void sendInfoCurrMove(bBoard const &b, depth_t const nCurDepth, bMove const &m, movenum_t const moveid, int64_t const nodes) const override
bool isRunning() const override
void sendInfoDepth(int depth, int seldepth, int64_t nodes, int nps) override
void sendMove(bBoard &b, bMove const &m) override
void sendInfoSearching(bBoard const &b, int const nLogDepth, depth_t const nMaxDepth, bScore const sc, int64_t const t, int64_t const nodes) const override
std::string scoreString(bScore const score) const override
UCIMode(std::string const &s="uci")
void sendInfo(std::string const &info) override
std::string engineGameResultString(gameResult_t const gr) const override
void sendDebug(int const l, std::string const &info) override
void sendInvalidMove(std::string const &info, std::string const &reason) override
~XboardMode() override
void sendResult(bBoard const &b, gameResult_t const gr) const override
void sendInfoSearching(bBoard const &b, int const nLogDepth, depth_t const nMaxDepth, bScore const sc, int64_t const t, int64_t const nodes) const override
std::string scoreString(bScore const score) const override
void sendInfo(std::string const &info) override
XboardMode(std::string const &s="xboard")
board
Definition board.h:45
Definition move.h:13
virtual void sendResult(bBoard const &b, gameResult_t const gr) const
virtual ~engineInterface()
virtual void sendInfoSearching(bBoard const &b, int const nLogDepth, depth_t const nMaxDepth, std::string const &comment, bScore const sc, int64_t const t, int64_t const nodes) const
engineInterface & operator=(engineInterface const &)=delete
void attachCommand(belofte::stringList const &sCommands, bool const published=true)
virtual void sendResponse(std::string const &response)
engineInterface(std::string const &s, std::string const &h="")
Common commands to all modes this ctor is called for each iface.
engineInterface & operator=(engineInterface &&)=delete
virtual void sendDebug(int const l, std::string const &info)
engineInterface(engineInterface const &)=delete
virtual void sendInvalidMove(std::string const &info, std::string const &reason)
virtual void sendInfoSearching(bBoard const &b, int const nLogDepth, depth_t const nMaxDepth, bScore const sc, int64_t const t, int64_t const nodes) const
static engineCommands_t & getEngineCommands()
virtual void setRunning(bool const r)
virtual void sendMove(bBoard &b, bMove const &m)
virtual void sendHelp(std::string const &args)
Send help on command or all commands.
virtual std::string engineGameResultString(gameResult_t const gr) const
virtual void sendInfoCurrMove(bBoard const &b, depth_t const nCurDepth, bMove const &m, movenum_t const moveid, int64_t const nodes) const
engineInterface(engineInterface &&)=delete
void execute(std::string const &command, std::string const &params)
virtual void sendInfo(std::string const &info)
virtual bool isRunning() const
std::string const & getHint() const
virtual void sendInfoSearchStart(std::string const &info)
virtual std::string scoreString(bScore const score) const
virtual void sendError(std::string const &error, std::string const &description)
static std::string scoreAsString(bScore const score)
virtual void sendInfoDepth(int depth, int seldepth, int64_t nodes, int nps)
virtual void sendPrompt()
std::map< std::string, engineUserCommand * > engineCommands_t
std::map< std::string, engineInterface * > engineInterfaces_t
enum gameResult gameResult_t
Definition eval.h:43
int16_t bScore
Definition eval.h:11
std::vector< std::string > stringList
Definition util.h:46