UCICommand.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (c) 2020-2022, the SerenityOS developers.
  3. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2023, Tim Ledbetter <timledbetter@gmail.com>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/Optional.h>
  10. #include <AK/String.h>
  11. #include <LibChess/Chess.h>
  12. #include <LibCore/Event.h>
  13. namespace Chess::UCI {
  14. class Command : public Core::Event {
  15. public:
  16. enum class Type {
  17. // GUI to engine commands.
  18. UCI = 12000,
  19. Debug,
  20. IsReady,
  21. SetOption,
  22. Register,
  23. UCINewGame,
  24. Position,
  25. Go,
  26. Stop,
  27. PonderHit,
  28. Quit,
  29. // Engine to GUI commands.
  30. Id,
  31. UCIOk,
  32. ReadyOk,
  33. BestMove,
  34. CopyProtection,
  35. Registration,
  36. Info,
  37. Option,
  38. };
  39. virtual ErrorOr<String> to_string() const = 0;
  40. virtual ~Command() = default;
  41. protected:
  42. explicit Command(Type type)
  43. : Core::Event(to_underlying(type))
  44. {
  45. }
  46. };
  47. class UCICommand : public Command {
  48. public:
  49. explicit UCICommand()
  50. : Command(Command::Type::UCI)
  51. {
  52. }
  53. static ErrorOr<NonnullOwnPtr<UCICommand>> from_string(StringView command);
  54. virtual ErrorOr<String> to_string() const override;
  55. };
  56. class DebugCommand : public Command {
  57. public:
  58. enum class Flag {
  59. On,
  60. Off
  61. };
  62. explicit DebugCommand(Flag flag)
  63. : Command(Command::Type::Debug)
  64. , m_flag(flag)
  65. {
  66. }
  67. static ErrorOr<NonnullOwnPtr<DebugCommand>> from_string(StringView command);
  68. virtual ErrorOr<String> to_string() const override;
  69. Flag flag() const { return m_flag; }
  70. private:
  71. Flag m_flag;
  72. };
  73. class IsReadyCommand : public Command {
  74. public:
  75. explicit IsReadyCommand()
  76. : Command(Command::Type::IsReady)
  77. {
  78. }
  79. static ErrorOr<NonnullOwnPtr<IsReadyCommand>> from_string(StringView command);
  80. virtual ErrorOr<String> to_string() const override;
  81. };
  82. class SetOptionCommand : public Command {
  83. public:
  84. explicit SetOptionCommand(String name, Optional<String> value = {})
  85. : Command(Command::Type::SetOption)
  86. , m_name(move(name))
  87. , m_value(move(value))
  88. {
  89. }
  90. static ErrorOr<NonnullOwnPtr<SetOptionCommand>> from_string(StringView command);
  91. virtual ErrorOr<String> to_string() const override;
  92. String const& name() const { return m_name; }
  93. Optional<String> const& value() const { return m_value; }
  94. private:
  95. String m_name;
  96. Optional<String> m_value;
  97. };
  98. class PositionCommand : public Command {
  99. public:
  100. explicit PositionCommand(Optional<String> fen, Vector<Move> moves)
  101. : Command(Command::Type::Position)
  102. , m_fen(move(fen))
  103. , m_moves(move(moves))
  104. {
  105. }
  106. static ErrorOr<NonnullOwnPtr<PositionCommand>> from_string(StringView command);
  107. virtual ErrorOr<String> to_string() const override;
  108. Optional<String> const& fen() const { return m_fen; }
  109. Vector<Chess::Move> const& moves() const { return m_moves; }
  110. private:
  111. Optional<String> m_fen;
  112. Vector<Chess::Move> m_moves;
  113. };
  114. class GoCommand : public Command {
  115. public:
  116. explicit GoCommand()
  117. : Command(Command::Type::Go)
  118. {
  119. }
  120. static ErrorOr<NonnullOwnPtr<GoCommand>> from_string(StringView command);
  121. virtual ErrorOr<String> to_string() const override;
  122. Optional<Vector<Chess::Move>> searchmoves;
  123. bool ponder { false };
  124. Optional<int> wtime;
  125. Optional<int> btime;
  126. Optional<int> winc;
  127. Optional<int> binc;
  128. Optional<int> movestogo;
  129. Optional<int> depth;
  130. Optional<int> nodes;
  131. Optional<int> mate;
  132. Optional<int> movetime;
  133. bool infinite { false };
  134. };
  135. class StopCommand : public Command {
  136. public:
  137. explicit StopCommand()
  138. : Command(Command::Type::Stop)
  139. {
  140. }
  141. static ErrorOr<NonnullOwnPtr<StopCommand>> from_string(StringView command);
  142. virtual ErrorOr<String> to_string() const override;
  143. };
  144. class IdCommand : public Command {
  145. public:
  146. enum class Type {
  147. Name,
  148. Author,
  149. };
  150. explicit IdCommand(Type field_type, String value)
  151. : Command(Command::Type::Id)
  152. , m_field_type(field_type)
  153. , m_value(move(value))
  154. {
  155. }
  156. static ErrorOr<NonnullOwnPtr<IdCommand>> from_string(StringView command);
  157. virtual ErrorOr<String> to_string() const override;
  158. Type field_type() const { return m_field_type; }
  159. String const& value() const { return m_value; }
  160. private:
  161. Type m_field_type;
  162. String m_value;
  163. };
  164. class UCIOkCommand : public Command {
  165. public:
  166. explicit UCIOkCommand()
  167. : Command(Command::Type::UCIOk)
  168. {
  169. }
  170. static ErrorOr<NonnullOwnPtr<UCIOkCommand>> from_string(StringView command);
  171. virtual ErrorOr<String> to_string() const override;
  172. };
  173. class ReadyOkCommand : public Command {
  174. public:
  175. explicit ReadyOkCommand()
  176. : Command(Command::Type::ReadyOk)
  177. {
  178. }
  179. static ErrorOr<NonnullOwnPtr<ReadyOkCommand>> from_string(StringView command);
  180. virtual ErrorOr<String> to_string() const override;
  181. };
  182. class BestMoveCommand : public Command {
  183. public:
  184. explicit BestMoveCommand(Chess::Move move, Optional<Chess::Move> move_to_ponder = {})
  185. : Command(Command::Type::BestMove)
  186. , m_move(::move(move))
  187. , m_move_to_ponder(::move(move_to_ponder))
  188. {
  189. }
  190. static ErrorOr<NonnullOwnPtr<BestMoveCommand>> from_string(StringView command);
  191. virtual ErrorOr<String> to_string() const override;
  192. Chess::Move move() const { return m_move; }
  193. Optional<Chess::Move> move_to_ponder() const { return m_move_to_ponder; }
  194. private:
  195. Chess::Move m_move;
  196. Optional<Chess::Move> m_move_to_ponder;
  197. };
  198. class InfoCommand : public Command {
  199. public:
  200. enum class ScoreType {
  201. Centipawns,
  202. Mate
  203. };
  204. enum class ScoreBound {
  205. None,
  206. Lower,
  207. Upper
  208. };
  209. struct Score {
  210. ScoreType type;
  211. int value;
  212. ScoreBound bound;
  213. };
  214. explicit InfoCommand()
  215. : Command(Command::Type::Info)
  216. {
  217. }
  218. static ErrorOr<NonnullOwnPtr<InfoCommand>> from_string(StringView command);
  219. virtual ErrorOr<String> to_string() const override;
  220. private:
  221. Optional<int> m_depth;
  222. Optional<int> m_seldepth;
  223. Optional<int> m_time;
  224. Optional<int> m_nodes;
  225. Optional<Vector<Chess::Move>> m_pv;
  226. Optional<int> m_multipv;
  227. Optional<Score> m_score;
  228. Optional<Chess::Move> m_currmove;
  229. Optional<int> m_currmovenumber;
  230. Optional<int> m_hashfull;
  231. Optional<int> m_nps;
  232. Optional<int> m_tbhits;
  233. Optional<int> m_cpuload;
  234. Optional<String> m_string;
  235. Optional<Vector<Chess::Move>> m_refutation;
  236. Optional<Vector<Chess::Move>> m_currline;
  237. };
  238. class QuitCommand : public Command {
  239. public:
  240. explicit QuitCommand()
  241. : Command(Command::Type::Quit)
  242. {
  243. }
  244. static ErrorOr<NonnullOwnPtr<QuitCommand>> from_string(StringView command);
  245. virtual ErrorOr<String> to_string() const override;
  246. };
  247. class UCINewGameCommand : public Command {
  248. public:
  249. explicit UCINewGameCommand()
  250. : Command(Command::Type::UCINewGame)
  251. {
  252. }
  253. static ErrorOr<NonnullOwnPtr<UCINewGameCommand>> from_string(StringView command);
  254. virtual ErrorOr<String> to_string() const override;
  255. };
  256. }