ChessEngine.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ChessEngine.h"
  7. #include "MCTSTree.h"
  8. #include <AK/Random.h>
  9. #include <LibCore/ElapsedTimer.h>
  10. using namespace Chess::UCI;
  11. void ChessEngine::handle_uci()
  12. {
  13. send_command(IdCommand(IdCommand::Type::Name, "ChessEngine"));
  14. send_command(IdCommand(IdCommand::Type::Author, "the SerenityOS developers"));
  15. send_command(UCIOkCommand());
  16. }
  17. void ChessEngine::handle_position(const PositionCommand& command)
  18. {
  19. // FIXME: Implement fen board position.
  20. VERIFY(!command.fen().has_value());
  21. m_board = Chess::Board();
  22. for (auto& move : command.moves()) {
  23. VERIFY(m_board.apply_move(move));
  24. }
  25. }
  26. void ChessEngine::handle_go(const GoCommand& command)
  27. {
  28. // FIXME: A better algorithm than naive mcts.
  29. // FIXME: Add different ways to terminate search.
  30. VERIFY(command.movetime.has_value());
  31. srand(get_random<u32>());
  32. Core::ElapsedTimer elapsed_time;
  33. elapsed_time.start();
  34. MCTSTree mcts(m_board);
  35. int rounds = 0;
  36. while (elapsed_time.elapsed() <= command.movetime.value()) {
  37. mcts.do_round();
  38. ++rounds;
  39. }
  40. dbgln("MCTS finished {} rounds.", rounds);
  41. dbgln("MCTS evaluation {}", mcts.expected_value());
  42. auto best_move = mcts.best_move();
  43. dbgln("MCTS best move {}", best_move.to_long_algebraic());
  44. send_command(BestMoveCommand(best_move));
  45. }