1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-16 20:05:07 +00:00
serenity/Userland/Services/ChessEngine/ChessEngine.cpp
sin-ack 3f3f45580a Everywhere: Add sv suffix to strings relying on StringView(char const*)
Each of these strings would previously rely on StringView's char const*
constructor overload, which would call __builtin_strlen on the string.
Since we now have operator ""sv, we can replace these with much simpler
versions. This opens the door to being able to remove
StringView(char const*).

No functional changes.
2022-07-12 23:11:35 +02:00

53 lines
1.4 KiB
C++

/*
* Copyright (c) 2020, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "ChessEngine.h"
#include "MCTSTree.h"
#include <AK/Random.h>
#include <LibCore/ElapsedTimer.h>
using namespace Chess::UCI;
void ChessEngine::handle_uci()
{
send_command(IdCommand(IdCommand::Type::Name, "ChessEngine"sv));
send_command(IdCommand(IdCommand::Type::Author, "the SerenityOS developers"sv));
send_command(UCIOkCommand());
}
void ChessEngine::handle_position(PositionCommand const& command)
{
// FIXME: Implement fen board position.
VERIFY(!command.fen().has_value());
m_board = Chess::Board();
for (auto& move : command.moves()) {
VERIFY(m_board.apply_move(move));
}
}
void ChessEngine::handle_go(GoCommand const& command)
{
// FIXME: A better algorithm than naive mcts.
// FIXME: Add different ways to terminate search.
VERIFY(command.movetime.has_value());
srand(get_random<u32>());
auto elapsed_time = Core::ElapsedTimer::start_new();
MCTSTree mcts(m_board);
int rounds = 0;
while (elapsed_time.elapsed() <= command.movetime.value()) {
mcts.do_round();
++rounds;
}
dbgln("MCTS finished {} rounds.", rounds);
dbgln("MCTS evaluation {}", mcts.expected_value());
auto best_move = mcts.best_move();
dbgln("MCTS best move {}", best_move.to_long_algebraic());
send_command(BestMoveCommand(best_move));
}