UCICommand.h 5.8 KB

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