ChessEngine.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "ChessEngine.h"
  27. #include "MCTSTree.h"
  28. #include <LibCore/ElapsedTimer.h>
  29. using namespace Chess::UCI;
  30. void ChessEngine::handle_uci()
  31. {
  32. send_command(IdCommand(IdCommand::Type::Name, "ChessEngine"));
  33. send_command(IdCommand(IdCommand::Type::Author, "the SerenityOS developers"));
  34. send_command(UCIOkCommand());
  35. }
  36. void ChessEngine::handle_position(const PositionCommand& command)
  37. {
  38. // FIXME: Implement fen board position.
  39. ASSERT(!command.fen().has_value());
  40. m_board = Chess::Board();
  41. for (auto& move : command.moves()) {
  42. ASSERT(m_board.apply_move(move));
  43. }
  44. }
  45. void ChessEngine::handle_go(const GoCommand& command)
  46. {
  47. // FIXME: A better algorithm than naive mcts.
  48. // FIXME: Add different ways to terminate search.
  49. ASSERT(command.movetime.has_value());
  50. srand(arc4random());
  51. Core::ElapsedTimer elapsed_time;
  52. elapsed_time.start();
  53. MCTSTree mcts(m_board);
  54. // FIXME: optimize simulations enough for use.
  55. mcts.set_eval_method(MCTSTree::EvalMethod::Heuristic);
  56. int rounds = 0;
  57. while (elapsed_time.elapsed() <= command.movetime.value()) {
  58. mcts.do_round();
  59. ++rounds;
  60. }
  61. dbg() << "MCTS finished " << rounds << " rounds.";
  62. dbg() << "MCTS evaluation " << mcts.expected_value();
  63. auto best_move = mcts.best_move();
  64. dbg() << "MCTS best move " << best_move.to_long_algebraic();
  65. send_command(BestMoveCommand(best_move));
  66. }