diff --git a/cpp/src/ais/ai.hpp b/cpp/src/ais/ai.hpp index a1748e0..7e56206 100644 --- a/cpp/src/ais/ai.hpp +++ b/cpp/src/ais/ai.hpp @@ -2,22 +2,25 @@ #include "../board.hpp" -#include - -#define DECLARE_AI(x) \ - struct x : public AI { \ - std::string search(std::string, int) override; \ - int minimax(const Board&, int) override; \ - int eval(const Board&) override; \ - }; +#include namespace ai { struct AI { - virtual std::string search(std::string, int) = 0; - virtual int minimax(const Board&, int) = 0; + std::atomic stop_computation = false; + virtual Move search(const Board&, bool = false) = 0; virtual int eval(const Board&) = 0; }; - DECLARE_AI(v0_random) - DECLARE_AI(v1_simple) + struct v0_random : public AI { + Move search(const Board&, bool) override; + // int eval(const Board&) override; + }; + + class v1_simple : public AI { + int _search(const Board&, int); + + public: + Move search(const Board&, bool) override; + int eval(const Board&) override; + }; } // namespace ai diff --git a/cpp/src/ais/v0_random.cpp b/cpp/src/ais/v0_random.cpp index 82f569c..8a0bd34 100644 --- a/cpp/src/ais/v0_random.cpp +++ b/cpp/src/ais/v0_random.cpp @@ -1,8 +1,7 @@ #include "ai.hpp" -std::string ai::v1_simple::search(std::string pos, int depth) { - Board b = Board::setup_fen_position(pos); +Move ai::v0_random::search(const Board& b, bool) { std::vector moves = b.all_legal_moves(); - return moves[rand() % moves.size()].to_string(); + return moves[rand() % moves.size()]; } diff --git a/cpp/src/ais/v1_simple.cpp b/cpp/src/ais/v1_simple.cpp index e848dfb..32bb6ab 100644 --- a/cpp/src/ais/v1_simple.cpp +++ b/cpp/src/ais/v1_simple.cpp @@ -7,21 +7,21 @@ static int INFINITY = std::numeric_limits::max(); -std::string ai::v1_simple::search(std::string pos, int depth) { - Board b = Board::setup_fen_position(pos); - +Move ai::v1_simple::search(const Board& b, bool am_black) { ThreadPool pool(std::thread::hardware_concurrency()); std::vector moves = b.all_legal_moves(); - std::map> futures; - for (const Move& move : moves) { - Board tmp_board = b.make_move(move); - futures.insert({move.to_string(), pool.enqueue([&]() { - return minimax(tmp_board, depth - 1); - })}); + std::map> futures; + for (int depth = 1; !stop_computation; depth++) { + for (const Move& move : moves) { + Board tmp_board = b.make_move(move); + futures.insert({move, pool.enqueue([&]() { + return _search(tmp_board, depth - 1); + })}); + } } - std::string best_move; + Move best_move; int best_eval = -INFINITY; for (auto& [move, future] : futures) { int eval = future.get(); @@ -34,11 +34,11 @@ std::string ai::v1_simple::search(std::string pos, int depth) { return best_move; } -int ai::v1_simple::minimax(const Board& b, int depth) { +int ai::v1_simple::_search(const Board& b, int depth) { if (b.is_checkmate_for(b.white_to_play ? White : Black)) return -INFINITY; - if (depth == 0) + if (depth == 0 || stop_computation) return eval(b); std::vector moves = b.all_legal_moves(); @@ -46,7 +46,7 @@ int ai::v1_simple::minimax(const Board& b, int depth) { Move best_move; for (const Move& move : moves) { Board tmp_board = b.make_move(move); - int tmp_eval = -minimax(tmp_board, depth - 1); + int tmp_eval = -_search(tmp_board, depth - 1); best_evaluation = std::max(best_evaluation, tmp_eval); } return best_evaluation; @@ -62,7 +62,7 @@ int count_material(const Board& b, int8_t colour) { int ret = 0; for (int i = 0; i < 64; i++) { if (b.colour_at(i) == colour) - switch (b.squares[i] & 0b111) { + switch (b.piece_at(i)) { case Piece::Pawn: ret += PawnValue; break;