respect option on whether to start interactive shell

This commit is contained in:
Maximilian Keßler 2023-11-10 14:17:55 +01:00
parent 2debafe5d6
commit c40aae3c4b
Signed by: max
GPG key ID: BCC5A619923C0BA5

View file

@ -11,7 +11,8 @@
namespace Hanabi { namespace Hanabi {
void analyze_game_and_start_cli(std::variant<int, const char*> game_id, int turn, std::optional<uint8_t> score_goal) { void analyze_game_and_start_cli(std::variant<int, const char*> game_id, int turn, std::optional<uint8_t> score_goal,
bool start_cli) {
auto game = Download::get_game(game_id, turn, 0, score_goal); auto game = Download::get_game(game_id, turn, 0, score_goal);
if (game == nullptr) { if (game == nullptr) {
if(game_id.index() == 0) { if(game_id.index() == 0) {
@ -34,21 +35,14 @@ namespace Hanabi {
std::cout << "Took " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start) << "." << std::endl; std::cout << "Took " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start) << "." << std::endl;
std::cout << "Visited " << game->enumerated_states() << " states." << std::endl; std::cout << "Visited " << game->enumerated_states() << " states." << std::endl;
std::cout << "Enumerated " << game->position_tablebase().size() << " unique game states. " << std::endl; std::cout << "Enumerated " << game->position_tablebase().size() << " unique game states. " << std::endl;
std::cout << std::endl;
std::cout << "Dropping into interactive command line to explore result (type 'help'):" << std::endl;
auto game_shared = std::shared_ptr<HanabiStateIF>(game.release()); if (start_cli) {
auto states = game_shared->possible_next_states(0, false); std::cout << std::endl;
cli(game_shared); std::cout << "Dropping into interactive command line to explore result (type 'help'):" << std::endl;
} auto game_shared = std::shared_ptr<HanabiStateIF>(game.release());
auto states = game_shared->possible_next_states(0, false);
void print_usage(const char* program_name) { cli(game_shared);
std::cout << "Usage: " << program_name << "(GAME_ID | GAME_FILE) TURN [SCORE_GOAL]" << std::endl; }
std::cout << " GAME_ID A game id from hanab.live" << std::endl;
std::cout << " GAME_FILE A path to a file describing the game in hanab.live json format." << std::endl;
std::cout << " TURN Turn number of state to analyze. Turn 1 means no actions have been taken." << std::endl;
std::cout << " TURN Turn number of state to analyze. Turn 1 means no actions have been taken." << std::endl;
std::cout << " SCORE_GOAL Score that counts as a win, i.e. is optimized for achieving." << std::endl;
} }
} }
@ -57,6 +51,7 @@ namespace Hanabi {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int turn; int turn;
std::optional<int> score; std::optional<int> score;
bool interactive_shell;
boost::program_options::options_description desc("Allowed options"); boost::program_options::options_description desc("Allowed options");
desc.add_options() desc.add_options()
@ -65,7 +60,7 @@ int main(int argc, char *argv[]) {
("file,f", boost::program_options::value<std::string>(), "Input file containing game in hanab.live json format") ("file,f", boost::program_options::value<std::string>(), "Input file containing game in hanab.live json format")
("turn,t", boost::program_options::value<int>(&turn)->default_value(1), "Turn number of state to analyze. Turn 1 means no actions have been taken.") ("turn,t", boost::program_options::value<int>(&turn)->default_value(1), "Turn number of state to analyze. Turn 1 means no actions have been taken.")
("score,s", boost::program_options::value<int>(), "Score that counts as a win, i.e. is optimized for achieving.") ("score,s", boost::program_options::value<int>(), "Score that counts as a win, i.e. is optimized for achieving.")
("interactive,i", "Drop into interactive shell to explore game") ("interactive,i", boost::program_options::value<bool>(&interactive_shell)->default_value(true), "Drop into interactive shell to explore game")
; ;
boost::program_options::variables_map vm; boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm); boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
@ -87,9 +82,9 @@ int main(int argc, char *argv[]) {
} }
if (vm.count("file")) { if (vm.count("file")) {
Hanabi::analyze_game_and_start_cli(vm["file"].as<std::string>().c_str(), turn, score); Hanabi::analyze_game_and_start_cli(vm["file"].as<std::string>().c_str(), turn, score, interactive_shell);
} else { } else {
Hanabi::analyze_game_and_start_cli(vm["id"].as<int>(), turn, score); Hanabi::analyze_game_and_start_cli(vm["id"].as<int>(), turn, score, interactive_shell);
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }