From e0a52f57b7ccb9df7fc5eb337bca2f102e339db2 Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Thu, 6 Feb 2025 20:38:27 +0100 Subject: [PATCH] made the thinking time of AIs common to all of them --- cpp/src/main.cpp | 4 +++- cpp/src/model/ais/ai.cpp | 23 +++++++++++++++++++++++ cpp/src/model/ais/ai.hpp | 22 ++++++++++++++++++---- cpp/src/model/ais/v0_random.cpp | 4 +++- cpp/src/model/ais/v1_simple.cpp | 2 +- 5 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 cpp/src/model/ais/ai.cpp diff --git a/cpp/src/main.cpp b/cpp/src/main.cpp index 59bf3eb..1cec09a 100644 --- a/cpp/src/main.cpp +++ b/cpp/src/main.cpp @@ -5,13 +5,15 @@ #include "view/noop.hpp" #include "view/view.hpp" +#include + int main(int argc, char* argv[]) { std::string pos = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; Board b = Board::setup_fen_position(pos); - ai::v0_random ai; + ai::v0_random ai(std::chrono::milliseconds(500)); GUI gui; HumanVsAIController manual(b, gui, ai); diff --git a/cpp/src/model/ais/ai.cpp b/cpp/src/model/ais/ai.cpp new file mode 100644 index 0000000..297368a --- /dev/null +++ b/cpp/src/model/ais/ai.cpp @@ -0,0 +1,23 @@ +#include "ai.hpp" + +#include + +Move ai::AI::search(const Board& b, bool am_white) { + Move result; + + // Start computation in a separate thread + std::thread computation_thread([&]() { result = _search(b, am_white); }); + + // Start a timer thread to stop computation after given time + std::thread timer_thread([&]() { + std::this_thread::sleep_for(thinking_time); + stop_computation = true; + }); + + // Wait for computation thread to finish + computation_thread.join(); + + // Ensure timer thread is also stopped + timer_thread.join(); + return result; +} diff --git a/cpp/src/model/ais/ai.hpp b/cpp/src/model/ais/ai.hpp index 1356211..dfc9f07 100644 --- a/cpp/src/model/ais/ai.hpp +++ b/cpp/src/model/ais/ai.hpp @@ -3,16 +3,28 @@ #include "../board/board.hpp" #include +#include namespace ai { - struct AI { + class AI { + protected: + std::chrono::milliseconds thinking_time; + virtual Move _search(const Board&, bool = false) = 0; + + public: + AI(std::chrono::milliseconds tt): thinking_time(tt) {} + std::atomic stop_computation = false; - virtual Move search(const Board&, bool = false) = 0; + + Move search(const Board& b, bool am_white = false); + virtual int eval(const Board&) = 0; }; struct v0_random : public AI { - Move search(const Board&, bool) override; + v0_random(std::chrono::milliseconds tt): AI(tt) {} + + Move _search(const Board&, bool) override; int eval(const Board&) override { return 0; @@ -23,7 +35,9 @@ namespace ai { int _search(const Board&, int); public: - Move search(const Board&, bool) override; + v1_simple(std::chrono::milliseconds tt): AI(tt) {} + + Move _search(const Board&, bool) override; int eval(const Board&) override; }; } // namespace ai diff --git a/cpp/src/model/ais/v0_random.cpp b/cpp/src/model/ais/v0_random.cpp index 8a0bd34..d2c4d0a 100644 --- a/cpp/src/model/ais/v0_random.cpp +++ b/cpp/src/model/ais/v0_random.cpp @@ -1,6 +1,8 @@ #include "ai.hpp" -Move ai::v0_random::search(const Board& b, bool) { +#include + +Move ai::v0_random::_search(const Board& b, bool) { std::vector moves = b.all_legal_moves(); return moves[rand() % moves.size()]; diff --git a/cpp/src/model/ais/v1_simple.cpp b/cpp/src/model/ais/v1_simple.cpp index 56972c5..b4f7833 100644 --- a/cpp/src/model/ais/v1_simple.cpp +++ b/cpp/src/model/ais/v1_simple.cpp @@ -7,7 +7,7 @@ static int INFINITY = std::numeric_limits::max(); -Move ai::v1_simple::search(const Board& b, bool am_black) { +Move ai::v1_simple::_search(const Board& b, bool am_black) { ThreadPool pool(std::thread::hardware_concurrency()); std::vector moves = b.all_legal_moves();