Belofte version 2.2.0
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&& n, std::string&& 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& mv, std::string const& reason)
54 { sendError("Illegal move", mv); }
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 static void initCommand(std::vector<engineUserCommand *> cmds);
95 void attachCommand(belofte::stringList const& sCommands,
96 bool const published = true);
97 virtual std::string scoreString(bScore const score) const
98 { return engineInterface::scoreAsString(score); }
99
101 static engineCommands_t* instance = new engineCommands_t();
102 return *instance;
103 }
104
105private:
106 std::string m_name;
107 std::string m_hint;
108 commandList_t m_allowedCommands;
109 bool m_running = false;
110};
111
112//-----------------------------------------------------------------------
113
114/**
115 * implementation of specific implementation
116 */
118public:
119 BelofteMode();
120 ~BelofteMode() override
121 {}
122
123 void sendPrompt() override;
124};
125
126//-----------------------------------------------------------------------
127
128class UCIMode : public engineInterface {
129public:
130 explicit UCIMode(std::string&& s = "uci");
131 ~UCIMode() override
132 {}
133
134 // UCI mode does not have a persistent running state
135 bool isRunning() const override
136 { return false; }
137
138 void sendInfo(std::string const& info) override;
139 void sendDebug(int const l, std::string const& info) override;
140 void sendMove(bBoard& b, bMove const& m) override;
141 void sendResult(bBoard const& b, gameResult_t const gr) const override;
142 void sendInfoDepth(int depth, int seldepth, int64_t nodes,
143 int nps) override;
144 void sendInfoSearching(bBoard const& b, int const nLogDepth,
145 depth_t const nMaxDepth,
146 bScore const sc, int64_t const t,
147 int64_t const nodes) const override;
148 void sendInfoSearching(bBoard const& b, int const nLogDepth,
149 depth_t const nMaxDepth, std::string const& comment,
150 bScore const sc, int64_t const t,
151 int64_t const nodes) const override;
152 void sendInfoCurrMove(bBoard const& b,
153 depth_t const nCurDepth,
154 bMove const& m, movenum_t const moveid,
155 int64_t const nodes) const override;
156
157protected:
158 std::string scoreString(bScore const score) const override;
159
160private:
161 inline static void printPV(bBoard const& b,
162 int const nLogDepth,
163 int const nVariationDepth);
164};
165
166#if defined(__GNUC__)
167#pragma GCC diagnostic pop
168#endif
169
170//-----------------------------------------------------------------------
171
173public:
174 explicit XboardMode(std::string&& s = "xboard");
175 ~XboardMode() override
176 {}
177
178 void sendInfo(std::string const& info) override;
179 void sendDebug(int const l, std::string const& info) override;
180 void sendResult(bBoard const& b, gameResult_t const gr) const override;
181 void sendInvalidMove(std::string const& mv,
182 std::string const& reason) override;
183 void sendInfoSearching(bBoard const& b, int const nLogDepth,
184 depth_t const nMaxDepth,
185 bScore const sc, int64_t const t,
186 int64_t const nodes) const override;
187
188 std::string engineGameResultString(gameResult_t const gr) const override;
189
190protected:
191 std::string scoreString(bScore const score) const override;
192};
193
194//-----------------------------------------------------------------------
195
196typedef std::map<std::string, engineInterface*> engineInterfaces_t;
197
198//-----------------------------------------------------------------------
199
200#endif // defined ENGINEINTERFACE_H
201
202// eof
std::map< std::string, bool > commandList_t
Definition belofte.h:145
uint_fast8_t movenum_t
Definition belofte.h:103
std::map< std::string, engineUserCommand * > engineCommands_t
Definition belofte.h:146
int_fast8_t depth_t
Definition belofte.h:106
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
UCIMode(std::string &&s="uci")
std::string scoreString(bScore const score) const override
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
XboardMode(std::string &&s="xboard")
void sendInvalidMove(std::string const &mv, 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
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 & operator=(engineInterface &&)=delete
virtual void sendDebug(int const l, std::string const &info)
engineInterface(engineInterface const &)=delete
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
static void initCommand(std::vector< engineUserCommand * > cmds)
engineInterface(engineInterface &&)=delete
void execute(std::string const &command, std::string const &params)
virtual void sendInvalidMove(std::string const &mv, std::string const &reason)
virtual void sendInfo(std::string const &info)
virtual bool isRunning() const
std::string const & getHint() const
virtual void sendInfoSearchStart(std::string const &info)
engineInterface(std::string &&n, std::string &&h="")
Common commands to all modes this ctor is called for each iface.
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:40
int16_t bScore
Definition eval.h:11
std::vector< std::string > stringList
Definition util.h:48