Skip to content

Commit

Permalink
Engine interfaces with GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
jsilll committed Jun 24, 2023
1 parent 2019eb3 commit 7c85731
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 208 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ add_subdirectory(codchess)
add_subdirectory(codbrain)

# -- Codfish --
add_subdirectory(codfish)
add_subdirectory(app)
26 changes: 26 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -- Source Files --
file(GLOB SOURCES "*.cpp")

# -- Codfish Engine --
add_executable(codfish ${SOURCES})

# -- Link Libraries --
target_link_libraries(codfish
PUBLIC codbrain)

# -- All Warnings as Errors --
if (MSVC)
target_compile_options(codfish PRIVATE /WX)
else ()
target_compile_options(codfish PRIVATE -Werror)
endif ()

# -- Release and Debug Flags --
if (CMAKE_BUILD_TYPE MATCHES Release)
target_compile_options(codfish PRIVATE -O3 -march=native)
elseif (CMAKE_BUILD_TYPE MATCHES ReleaseCrossPlatform)
target_compile_options(codfish PRIVATE -O3 -march=x86-64)
elseif (CMAKE_BUILD_TYPE MATCHES Debug)
target_compile_options(codfish PRIVATE -g -O0)
target_compile_definitions(codfish PRIVATE DEBUG)
endif ()
8 changes: 7 additions & 1 deletion codfish/app/main.cpp → app/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#include "uci.hpp"
#include <codbrain/codbrain.hpp>

using namespace codbrain;

int
main([[maybe_unused]] const int argc, [[maybe_unused]] const char *argv[]) {
::codbrain::Init();
SimpleBrain brain;

try {
brain.Launch();
return EXIT_SUCCESS;
} catch (const std::exception &e) {
std::cerr << e.what() << '\n';
Expand Down
41 changes: 29 additions & 12 deletions codbrain/include/codbrain/codbrain.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#pragma once

#include <vector>

#include <codchess/codchess.hpp>

#include <codbrain/uci.hpp>

namespace codbrain {
/// @brief Initializes the library
void
inline void
Init() noexcept {
codchess::Init();
}
Expand All @@ -30,24 +34,37 @@ struct Result {
PrincipalVariation pv;
};

class Brain final {
class Brain {
public:
/// @brief Construct a new Brain object.
explicit Brain(const Depth depth) noexcept : _depth{depth} {}
Brain() noexcept;
virtual ~Brain() noexcept {}

/// @brief Searches the position.
virtual Result Search() noexcept = 0;

/// @brief Launches the UCI.
void Launch() noexcept { _uci.Launch(); }

/// @brief Returns the board.
[[nodiscard]] auto &board() noexcept { return _board; }

/// @brief Searches the position.
[[nodiscard]] Result Search() noexcept {
const auto legal_moves = codchess::movegen::Legal(_board);
return {0, 0, {*legal_moves.begin()}};
}

private:
/// @brief The depth of the search.
[[maybe_unused]] Depth _depth;
protected:
/// @brief The UCI.
Uci _uci{};
/// @brief The board.
codchess::Board _board{};
};

class SimpleBrain final : public Brain {
public:
/// @brief Construct a new SimpleBrain object.
SimpleBrain() noexcept : Brain() {}

/// @brief Searches the position.
Result Search() noexcept override {
const auto moves = codchess::movegen::Legal(_board);
return Result{0, 0, {*moves.begin()}};
}
};
} // namespace codbrain
71 changes: 46 additions & 25 deletions codfish/app/uci.hpp → codbrain/include/codbrain/uci.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Original Source Code: https://github.com/acdemiralp/uci

#pragma once

#include <cstddef>
Expand All @@ -9,6 +11,7 @@

#include <boost/signals2.hpp>

namespace codbrain {
/// @brief Universal Chess Interface.
class Uci final {
public:
Expand Down Expand Up @@ -144,9 +147,10 @@ class Uci final {
/// @brief Sends the 'bestmove' response.
/// @param move
/// @param ponder
static void SendBestMove([[maybe_unused]] const std::string &move,
static void SendBestMove(const std::string &move,
const bool ponder = false) {
std::cout << "bestmove" << (ponder ? " ponder" : "") << std::endl;
std::cout << "bestmove " << move << (ponder ? " ponder" : "")
<< std::endl;
}

/// @brief Sends the copy protection state.
Expand Down Expand Up @@ -394,6 +398,11 @@ class Uci final {
SendOptionString("UCI_SetPositionValue", initial);
}

/// @brief Sends an illegal move error.
static void SendErrorIllegalMove() {
std::cout << "Illegal move" << std::endl;
}

/// @brief Launches the UCI loop.
void Launch() {
std::string line{};
Expand All @@ -412,23 +421,29 @@ class Uci final {
} else if (token == "setoption") {
std::string name{}, value{};
iss >> token;
while (iss >> token && token != "value")
while (iss >> token && token != "value") {
name += std::string(" ", name.empty() ? 0 : 1) + token;
while (iss >> token)
}
while (iss >> token) {
value += std::string(" ", value.empty() ? 0 : 1) + token;
}
receive_set_option(name, value);
} else if (token == "register") {
auto later{false};
bool later{false};
std::string name{};
std::size_t code{0};
iss >> token;
if (token == "later")
if (token == "later") {
later = true;
if (token == "name")
while (iss >> token && token != "code")
}
if (token == "name") {
while (iss >> token && token != "code") {
name += std::string(" ", name.empty() ? 0 : 1) + token;
if (token == "code")
}
}
if (token == "code") {
iss >> code;
}
receive_register(later, name, code);
} else if (token == "ucinewgame") {
receive_uci_new_game();
Expand All @@ -439,47 +454,52 @@ class Uci final {
if (token == "startpos") {
fen = start_fen;
iss >> token;
} else if (token == "fen")
while (iss >> token && token != "moves")
} else if (token == "fen") {
while (iss >> token && token != "moves") {
fen += token + " ";
else
}
} else {
continue;
while (iss >> token)
}
while (iss >> token) {
moves.push_back(token);
}
receive_position(fen, moves);
} else if (token == "go") {
std::map<Command, std::string> commands{};
while (iss >> token)
if (token == "searchmoves")
while (iss >> token)
while (iss >> token) {
commands[Command::SearchMoves] +=
std::string(
" ", commands[Command::SearchMoves].empty()
? 0
: 1) +
token;
else if (token == "ponder")
}
else if (token == "ponder") {
commands[Command::Ponder];
else if (token == "wtime")
} else if (token == "wtime") {
iss >> commands[Command::WhiteTime];
else if (token == "btime")
} else if (token == "btime") {
iss >> commands[Command::BlackTime];
else if (token == "winc")
} else if (token == "winc") {
iss >> commands[Command::WhiteIncrement];
else if (token == "binc")
} else if (token == "binc") {
iss >> commands[Command::BlackIncrement];
else if (token == "movestogo")
} else if (token == "movestogo") {
iss >> commands[Command::MovesToGo];
else if (token == "depth")
} else if (token == "depth") {
iss >> commands[Command::Depth];
else if (token == "nodes")
} else if (token == "nodes") {
iss >> commands[Command::Nodes];
else if (token == "mate")
} else if (token == "mate") {
iss >> commands[Command::Mate];
else if (token == "move_time")
} else if (token == "move_time") {
iss >> commands[Command::MoveTime];
else if (token == "infinite")
} else if (token == "infinite") {
commands[Command::Infinite];
}
receive_go(commands);
} else if (token == "stop") {
receive_stop();
Expand All @@ -494,3 +514,4 @@ class Uci final {
}
}
};
} // namespace codbrain
34 changes: 32 additions & 2 deletions codbrain/src/codbrain.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
#include <codbrain/codbrain.hpp>

using namespace codchess;

namespace codbrain {
Brain::Brain() noexcept {
_uci.receive_uci.connect([&]() {
_uci.SendId("Engine name", "Author name");
_uci.SendOptionUciLimitStrength(false);
_uci.SendUciOk();
});

_uci.receive_is_ready.connect([&]() { _uci.SendReadyOk(); });

_uci.receive_position.connect(
[&](const std::string &fen, const std::vector<std::string> &moves) {
_board.FromFen(fen);
for (const auto &move : moves) {
const auto legal = codchess::movegen::Legal(_board);
const auto it = std::find_if(legal.begin(), legal.end(),
[&](const codchess::Move &m) {
return m.ToString() == move;
});
if (it == legal.end()) {
_uci.SendErrorIllegalMove();
return;
} else {
_board.Make(*it);
}
}
});

_uci.receive_go.connect(
[&](const std::map<Uci::Command, std::string> &) {
const auto result = Search();
_uci.SendBestMove(result.pv[0].ToString());
});
}
} // namespace codbrain
Loading

0 comments on commit 7c85731

Please sign in to comment.