UCICommand.h 6.5 KB

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