diff --git a/cpp/src/main.cpp b/cpp/src/main.cpp index ff3b6d3..1fd4a79 100644 --- a/cpp/src/main.cpp +++ b/cpp/src/main.cpp @@ -10,21 +10,26 @@ #include int main(int argc, char* argv[]) { + // std::string pos = + // "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; std::string pos = - "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; + "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 4 3"; Board b = Board::setup_fen_position(pos); - ai::v0_random p1(true, std::chrono::milliseconds(500)); - ai::v0_random p2(false, std::chrono::milliseconds(500)); + // ai::v0_random p1(true, std::chrono::milliseconds(500)); + ai::v1_simple p1(false, std::chrono::milliseconds(100000)); + // ai::v1_simple p2(false, std::chrono::milliseconds(100000)); + // ai::v0_random p2(false, std::chrono::milliseconds(10000)); GUI gui; - AIvsAIController manual(b, gui, p1, p2); + // AIvsAIController manual(b, gui, p1, p2); + HumanVsAIController manual(b, gui, p1); Controller& controller = manual; controller.start(); - // perft(); + // perft(pos); return 0; } diff --git a/cpp/src/model/ais/v1_simple.cpp b/cpp/src/model/ais/v1_simple.cpp index c61e0ef..52342b5 100644 --- a/cpp/src/model/ais/v1_simple.cpp +++ b/cpp/src/model/ais/v1_simple.cpp @@ -2,18 +2,29 @@ #include "../utils/threadpool.hpp" #include "ai.hpp" +#include + static int INFINITY = std::numeric_limits::max(); +int position_counter; + Move ai::v1_simple::_search(const Board& b) { ThreadPool pool(std::thread::hardware_concurrency()); - Move best_move; - int best_eval = -INFINITY; std::vector moves = b.all_legal_moves(); + std::map> futures; for (const Move& move : moves) { Board tmp_board = b.make_move(move); - int eval = _search(tmp_board, 4); + futures.insert({move, pool.enqueue([&, tmp_board]() { + return _search(tmp_board, 3); + })}); + } + + Move best_move; + int best_eval = -INFINITY; + for (auto& [move, future] : futures) { + int eval = future.get(); if (!am_white) eval *= -1; if (eval > best_eval) { @@ -22,18 +33,22 @@ Move ai::v1_simple::_search(const Board& b) { } } + std::cout << "Looked at " << position_counter << " positions" << std::endl; return best_move; } int ai::v1_simple::_search(const Board& b, int depth) { - if (b.is_checkmate_for(b.white_to_play ? White : Black)) - return -INFINITY; + if (b.is_terminal()) { + if (b.is_checkmate_for(b.white_to_play ? White : Black)) + return -INFINITY; + return 0; + } if (depth == 0 || stop_computation) return eval(b); std::vector moves = b.all_legal_moves(); - int best_evaluation = 0; + int best_evaluation = -INFINITY; Move best_move; for (const Move& move : moves) { Board tmp_board = b.make_move(move); @@ -69,12 +84,16 @@ int count_material(const Board& b, int8_t colour) { case Piece::Queen: ret += QueenValue; break; + case Piece::King: + case Piece::None: + break; } } return ret; } int ai::v1_simple::eval(const Board& b) { + position_counter++; int white_eval = count_material(b, Colour::White); int black_eval = count_material(b, Colour::Black);