Belofte version 2.1.8
A promising chess program using the UCI or Winboard interface
belofte.h
Go to the documentation of this file.
1/**
2 * @file belofte.h
3 * @brief This is the main include file, needs to be included before any other include.
4 * @copyright This program is distributed under the GNU GPL Version 2,
5 * June 1991 General Public License. See COPYING.md for more
6 * information. SPDX-License-Identifier: GPL-2.0-only
7 * Part of belofte - A Promising Chess Program
8 * @author Yves De Billoëz
9 */
10
11#if !defined(BELOFTE_H)
12#define BELOFTE_H
13
14// --------------------------------------------------------------------
15
16#define BELOFTE_NOUNICODE 1 // the system does not support unicode, selected by default on _WIN32
17//#define INCOMPLETE_C11 // incomplete implementation of C++11, some alternative code
18
19//-----------------------------------------------------------------------
20
21#include "myplatform.h"
22
23//-----------------------------------------------------------------------
24
25//#define BELOFTE_NORANDOM 1
26//#define BELOFTE_UCIMODE 1 // start with UCI interface
27//#define BELOFTE_NOSIGNALS 1 // suppress default signal handling
28
29// --------------------------------------------------------------------
30
31#define MYNAME "Belofte"
32#define MYLCNAME "belofte"
33#define MYVERSION "2.1.8"
34#if defined(BELOFTE_NOUNICODE)
35#define MYAUTHOR "Yves De Billoez"
36#else
37#define MYAUTHOR "Yves De Billoëz"
38#endif
39#define DEVDATES "2004-2024"
40#define MYCOPYRIGHT DEVDATES " (c)"
41#define MYLICENSE "Released under the GNU GPL v2 license"
42
43#if !defined(_DEBUG)
44#define MYRELEASEDATE "on 10/10/2024"
45#else
46#define MYRELEASEDATE __DATE__ " - " __TIME__
47#endif
48
49#define MYFULLNAME MYNAME " " MYVERSION
50
51#if defined(_DEBUG)
52#define DEBUGFILE "debug-" MYLCNAME "-" MYVERSION ".log"
53#endif
54
55//-----------------------------------------------------------------------
56
57#include <map>
58#include <string>
59#include <iostream>
60#include <fstream>
61#include <sstream>
62#include <array>
63#include <vector>
64#include <numeric>
65#include <memory>
66#include <stdexcept>
67#include <iomanip>
68#include <algorithm>
69#include <thread>
70#include <atomic>
71
72#if defined(BELOFTE_NOSIGNALS)
73#include <csignal>
74#endif
75
76#if defined(CHRONO_MISSING)
77#include <sys/time.h>
78#else
79#include <chrono>
80#endif
81
82#if !defined(_MSC_VER)
83#include <dirent.h>
84#endif
85
86//-----------------------------------------------------------------------
87
88class noCommandException : public std::exception
89{
90 const char * what () const noexcept override {
91 return "No command";
92 }
93};
94
95class quitCommandException : public std::exception
96{
97 const char * what () const noexcept override {
98 return "Quit command";
99 }
100};
101
102//-----------------------------------------------------------------------
103
104typedef int8_t rank_t;
105typedef int8_t column_t;
106typedef uint8_t case_t; // 0-63, 0xFF
107typedef uint16_t basicmove_t; // casefrom & caseto, 0
108typedef uint16_t plynum_t; // used to return the ply number in a position
109typedef uint_fast8_t movenum_t; // used to return id of move in movelist
110typedef uint8_t movenum50_t; // moves till last capture or pawn move
111typedef uint64_t boardbitmap_t; // used for king and knightmoves
112typedef int_fast8_t depth_t; // search depth
113
114//-----------------------------------------------------------------------
115
116#include "piece.h"
117#include "usercmd.h"
118#include "bel_debug.h"
119#include "bel_hash.h"
120#include "util.h"
121#include "level.h"
122#include "bestmoveinfo.h"
123#include "eval.h"
124#include "case.h"
125#include "move.h"
126#include "movelist.h"
127#include "fen.h"
128#include "board.h"
129#include "coordmove.h"
130#include "pgnmove.h"
131#include "search.h"
132#include "search_random.h"
133#include "search_qsonly.h"
134#include "search_bf.h"
135#include "search_ab.h"
136#include "search_perft.h"
137#include "epd_testsuite.h"
138#include "configurablegame.h"
139#include "game.h"
140
141//-----------------------------------------------------------------------
142
143typedef std::map<std::string, bool> commandList_t;
144typedef std::map<std::string, engineUserCommand*> engineCommands_t;
145
146class commandReader final {
147public:
150
151 // no copy or move ctor nor assignment defined
152 commandReader(commandReader const&) = delete;
156
157 bool attach(std::string const& ifile);
158 void detach();
159 bool isFileAttached();
160 bool isBatchMode();
161 void setBatchMode();
162 void clearBatchMode();
163
165
166 void runner(void);
167
168private:
169 std::string readLine();
170
171 std::string m_filename;
172 std::ifstream m_inputfile;
173 bool m_isBatch = true;
174};
175
176#if defined(__GNUC__)
177#pragma GCC diagnostic push
178#pragma GCC diagnostic ignored "-Wunused-parameter"
179#endif
180
181/** implementation of user interface
182 */
184public:
185 explicit engineInterface(std::string const& n, std::string const& h = "");
186 virtual ~engineInterface();
187
188 // deny creation of copy/move constructor because of pointer members
193
194 operator std::string() const& { return const_cast<std::string const&>(m_name); }
195
196 void execute(std::string const& command, std::string const& params);
197
198 virtual void setRunning(bool const r) { m_running = r; }
199 virtual bool isRunning() const { return m_running; }
200
201 virtual void sendPrompt() {}
202 virtual void sendResponse(std::string const& response);
203 virtual void sendInvalidMove(std::string const& info,
204 std::string const& reason) {
205 sendError("Illegal move", info); }
206 virtual void sendError(std::string const& error, std::string const& description);
207 virtual void sendHelp(std::string const& args);
208 virtual void sendInfo(std::string const& info) {}
209 virtual void sendDebug(int const l, std::string const& info) {}
210
211 virtual void sendMove(bBoard& b, bMove const& m);
212 virtual void sendResult(bBoard const& b, gameResult_t rs) const;
213 virtual void sendInfoDepth(int depth, int seldepth,
214 int64_t nodes, int nps) {}
215 virtual void sendInfoScore(long long timems,
216 bBoard const& b, bScore const cp) {}
217 virtual void sendInfoSearching(bBoard const& b, int const nLogDepth,
218 depth_t const nMaxDepth, std::string const& comment,
219 bScore const sc, int64_t const t, int64_t const nodes) const {}
220 virtual void sendInfoCurrMove(bBoard const& b, depth_t const nLogDepth,
221 depth_t const nMaxDepth, std::string const& comment,
222 bMove const& m, movenum_t const moveid,
223 int64_t const nodes) const {}
224
225 std::string const& getHint() const { return m_hint; }
226
227protected:
228 void attachCommand(belofte::stringList const& sCommands,
229 bool const published = true);
230
231private:
232 void initCommand(std::vector<engineUserCommand *> cmds);
233
234 std::string m_name;
235 std::string m_hint;
236 commandList_t m_allowedCommands;
237 bool m_running = false;
238
239public:
241};
242
243#if defined(__GNUC__)
244#pragma GCC diagnostic pop
245#endif
246
247/** implementation of specific implementation
248 */
249class UCIMode : public engineInterface {
250public:
251 explicit UCIMode(std::string const& n = "uci");
252 ~UCIMode() override;
253
254 // UCI mode does not have a persistent running state
255 bool isRunning() const override { return false; }
256
257 void sendInfo(std::string const& info) override;
258 void sendDebug(int const l, std::string const& info) override;
259 void sendMove(bBoard& b, bMove const& m) override;
260 void sendResult(bBoard const& b UNUSED,
261 gameResult_t rs UNUSED) const override {}
262 void sendInfoDepth(int depth, int seldepth, int64_t nodes,
263 int nps) override;
264 void sendInfoScore(long long timems, bBoard const& b,
265 bScore const cp) override;
266 void sendInfoSearching(bBoard const& b, int const nLogDepth,
267 depth_t const nMaxDepth, std::string const& comment,
268 bScore const sc, int64_t const t,
269 int64_t const nodes) const override;
270 void sendInfoCurrMove(bBoard const& b, depth_t const nLogDepth,
271 depth_t const nMaxDepth, std::string const& comment,
272 bMove const& m, movenum_t const moveid,
273 int64_t const nodes) const override;
274};
275
276class BelofteMode final : public engineInterface {
277public:
278 BelofteMode();
279 ~BelofteMode() override;
280
281 void sendPrompt() override;
282};
283
285public:
286 explicit XboardMode(std::string const& n = "xboard");
287 ~XboardMode() override;
288
289 void sendInfo(std::string const& info) override;
290 void sendDebug(int const l, std::string const& info) override;
291 void sendInvalidMove(std::string const& info,
292 std::string const& reason) override;
293 void sendInfoSearching(bBoard const& b, int const nLogDepth,
294 depth_t const nMaxDepth, std::string const& comment,
295 bScore const sc, int64_t const t,
296 int64_t const nodes) const override;
297};
298
299//-----------------------------------------------------------------------
300
301typedef std::map<std::string, engineInterface*> engineInterfaces_t;
302
303/** Singleton implementation of application
304 */
305class appInstance final {
306public:
307 appInstance();
308 ~appInstance();
309
310 // make sure we cannot accidentally copy or assign the class by deleting
311 // the implementation
312 appInstance(appInstance const&) = delete;
316
317 std::string const setMode(std::string const& iName);
318 std::string const& getMode() const { return m_interfaceName; }
319
320 /// @remark C++17 would allow std::any to combine both
321 /// we could use a template as well
322 void setConfig(std::string const& s, int64_t v);
323 int64_t getConfig(std::string const& s, int64_t v);
324 void setConfig(std::string const& s, std::string const& v);
325 std::string getConfig(std::string const& s, std::string const& v);
326
327 void setName(char *sName);
328 std::string getName() const;
329
330 outputWriter bout; /// normal output
331 outputWriter sout; /// searching output
332 commandReader m_reader; /// read input
337
338private:
339 /// @remark C++17 would allow std::any to combine both
340 std::map<std::string, int64_t> m_settings;
341 std::map<std::string, std::string> m_stringsettings;
342 engineInterfaces_t m_engineInterfaces;
343 std::string m_interfaceName;
344};
345
348bGame *Game();
349
350#endif // defined BELOFTE_H
351
352// eof
appInstance & App()
Definition belofte.cpp:480
int8_t rank_t
Definition belofte.h:104
std::map< std::string, bool > commandList_t
Definition belofte.h:143
uint8_t movenum50_t
Definition belofte.h:110
uint16_t basicmove_t
Definition belofte.h:107
engineInterface * AppEI()
Definition belofte.cpp:486
bGame * Game()
Definition belofte.cpp:491
int8_t column_t
Definition belofte.h:105
uint_fast8_t movenum_t
Definition belofte.h:109
uint8_t case_t
Definition belofte.h:106
std::map< std::string, engineUserCommand * > engineCommands_t
Definition belofte.h:144
uint64_t boardbitmap_t
Definition belofte.h:111
int_fast8_t depth_t
Definition belofte.h:112
uint16_t plynum_t
Definition belofte.h:108
std::map< std::string, engineInterface * > engineInterfaces_t
Definition belofte.h:301
int16_t bScore
void sendPrompt() override
Definition usercmd.cpp:340
~BelofteMode() override
Definition belofte.cpp:363
implementation of specific implementation
Definition belofte.h:249
void sendDebug(int const l, std::string const &info) override
Definition usercmd.cpp:196
~UCIMode() override
Definition belofte.cpp:375
void sendResult(bBoard const &b UNUSED, gameResult_t rs UNUSED) const override
Definition belofte.h:260
bool isRunning() const override
Definition belofte.h:255
void sendInfoScore(long long timems, bBoard const &b, bScore const cp) override
Definition usercmd.cpp:218
void sendInfoDepth(int depth, int seldepth, int64_t nodes, int nps) override
Definition usercmd.cpp:203
void sendMove(bBoard &b, bMove const &m) override
Definition usercmd.cpp:179
void sendInfoCurrMove(bBoard const &b, depth_t const nLogDepth, depth_t const nMaxDepth, std::string const &comment, bMove const &m, movenum_t const moveid, int64_t const nodes) const override
Definition usercmd.cpp:309
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 override
Definition usercmd.cpp:256
void sendInfo(std::string const &info) override
Definition usercmd.cpp:189
void sendDebug(int const l, std::string const &info) override
Definition usercmd.cpp:170
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 override
Definition usercmd.cpp:386
void sendInvalidMove(std::string const &info, std::string const &reason) override
Definition usercmd.cpp:379
~XboardMode() override
Definition belofte.cpp:389
void sendInfo(std::string const &info) override
Definition usercmd.cpp:163
Singleton implementation of application.
Definition belofte.h:305
bGame * m_game
Definition belofte.h:336
appInstance & operator=(appInstance const &)=delete
outputWriter sout
normal output
Definition belofte.h:331
std::string const & getMode() const
Definition belofte.h:318
engineInterface * m_ui
Definition belofte.h:335
void setName(char *sName)
set name of executable based on argv[0] Remove leading / and \ characters Remove trailing ....
Definition belofte.cpp:459
bel_debug m_debuginterface
Definition belofte.h:334
appInstance(appInstance const &)=delete
bel_hash m_hashEngine
read input
Definition belofte.h:333
appInstance(appInstance &&)=delete
appInstance & operator=(appInstance &&)=delete
void setConfig(std::string const &s, int64_t v)
Definition belofte.cpp:433
outputWriter bout
Definition belofte.h:330
int64_t getConfig(std::string const &s, int64_t v)
Definition belofte.cpp:438
std::string getName() const
Definition belofte.cpp:473
std::string const setMode(std::string const &iName)
Definition belofte.cpp:425
commandReader m_reader
searching output
Definition belofte.h:332
board
Definition board.h:147
game representation, singleton
Definition game.h:20
Definition move.h:69
command and parameters
Definition usercmd.h:35
bool isBatchMode()
no interactive input, mainly during start and '@' execute
Definition belofte.cpp:293
commandReader & operator=(commandReader const &)=delete
commandReader(commandReader const &)=delete
void clearBatchMode()
Definition belofte.cpp:304
void runner(void)
Definition belofte.cpp:204
bool isFileAttached()
Definition belofte.cpp:277
cmdParam getCommand()
Definition belofte.cpp:245
bool attach(std::string const &ifile)
Definition belofte.cpp:270
void detach()
Definition belofte.cpp:285
commandReader(commandReader &&)=delete
void setBatchMode()
Definition belofte.cpp:299
commandReader & operator=(commandReader &&)=delete
implementation of user interface
Definition belofte.h:183
virtual ~engineInterface()
Definition belofte.cpp:343
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
Definition belofte.h:217
engineInterface & operator=(engineInterface const &)=delete
void attachCommand(belofte::stringList const &sCommands, bool const published=true)
Definition belofte.cpp:171
virtual void sendResponse(std::string const &response)
Definition usercmd.cpp:150
virtual void sendResult(bBoard const &b, gameResult_t rs) const
Definition usercmd.cpp:351
engineInterface & operator=(engineInterface &&)=delete
virtual void sendInfoCurrMove(bBoard const &b, depth_t const nLogDepth, depth_t const nMaxDepth, std::string const &comment, bMove const &m, movenum_t const moveid, int64_t const nodes) const
Definition belofte.h:220
virtual void sendDebug(int const l, std::string const &info)
Definition belofte.h:209
engineInterface(engineInterface const &)=delete
virtual void sendInvalidMove(std::string const &info, std::string const &reason)
Definition belofte.h:203
static engineCommands_t m_engineCommands
Definition belofte.h:140
virtual void setRunning(bool const r)
Definition belofte.h:198
virtual void sendMove(bBoard &b, bMove const &m)
Definition usercmd.cpp:345
virtual void sendHelp(std::string const &args)
Send help on command or all commands.
Definition usercmd.cpp:100
virtual void sendInfoScore(long long timems, bBoard const &b, bScore const cp)
Definition belofte.h:215
engineInterface(engineInterface &&)=delete
void execute(std::string const &command, std::string const &params)
Definition belofte.cpp:145
virtual void sendInfo(std::string const &info)
Definition belofte.h:208
virtual bool isRunning() const
Definition belofte.h:199
std::string const & getHint() const
Definition belofte.h:225
virtual void sendError(std::string const &error, std::string const &description)
Definition usercmd.cpp:156
virtual void sendInfoDepth(int depth, int seldepth, int64_t nodes, int nps)
Definition belofte.h:213
virtual void sendPrompt()
Definition belofte.h:201
output to std::cout and if required to debug and log-file
Definition bel_debug.h:35
enum gameResult gameResult_t
Definition eval.h:41