#include "config.hpp" #include <iostream> #include <fstream> #include <algorithm> #include "movement_freedom_heuristic.hpp" #include "number_direction_freedom_heuristic.hpp" #include "points_heuristic.hpp" #include "go_left_up_heuristic.hpp" #include "zone_heuristic.hpp" #include "generalization_heuristic.hpp" namespace game { config::config(std::string& filename) : ai_vs_ai(false), think_time(5000), game_count(100), heuristic_ai_1(nullptr), heuristic_ai_2(nullptr) { std::ifstream file; std::string line; file.open(filename); if(file.is_open()){ while(std::getline(file,line)) { line.erase(std::remove_if(line.begin(),line.end(),[](char c) {return std::isspace(c);}), line.end()); size_t indexEqual = line.find_first_of('='); const std::string& lvalue = line.substr(0,indexEqual); const std::string& rvalue = line.substr(indexEqual+1); handle_line(lvalue,rvalue); } file.close(); } if(heuristic_ai_1 == nullptr) { std::cout << "H1"; heuristic_ai_1 = new mcts::penguin_heuristic(); } if(heuristic_ai_2 == nullptr) { heuristic_ai_2 = new mcts::penguin_heuristic(); } } void config::handle_line(const std::string& option_name, const std::string& option_value) { if(option_name == "ai_vs_ai") { set_game_mode(option_value); } else if(option_name == "ai_think_time") { set_think_time(option_value); } else if(option_name == "ai_vs_ai_game_count") { set_game_count(option_value); } else if (option_name == "heuristic_ai_1") { set_heuristic(option_value,1); } else if (option_name == "heuristic_ai_2") { set_heuristic(option_value,2); } else if(option_name == "send_game_to_gui"){ set_send_game(option_value); } else { std::cerr << "Unknown option : " << option_name << std::endl; } } void config::set_game_mode(const std::string& value) { int v = std::stoi(value); ai_vs_ai = v; } void config::set_think_time(const std::string& value) { int v = std::stoi(value); think_time = v; } void config::set_game_count(const std::string& value) { int v = std::stoi(value); game_count = v; } void config::set_heuristic(const std::string& value, short player) { mcts::penguin_heuristic** h = player == 1 ? &heuristic_ai_1 : &heuristic_ai_2; if(value == "left_up") { *h = new mcts::go_left_up_heuristic(); }else if(value == "direction_freedom") { *h = new mcts::number_direction_freedom_heuristic(); }else if(value == "general"){ *h = new mcts::generalization_heuristic(); }else if(value == "points") { std::cout << "POINTS" << std::endl; *h = new mcts::points_heuristic(); }else if(value == "movement_freedom") { *h = new mcts::movement_freedom_heuristic(); }else if(value == "zone") { *h = new mcts::zone_heuristic(); }else if(value == "default") { *h = new mcts::penguin_heuristic(); } else { std::cerr << "Unknown heuristic : " << value << std::endl; } } void config::set_send_game(const std::string& value) { int v = std::stoi(value); send_game_to_gui = v; } }