From bf2e05a2b415ab17203eb980aa5873316ce5c72f Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 13 Nov 2017 10:34:17 +0000 Subject: Add rule reading logic to main class --- src/main/main.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'src/main/main.cpp') diff --git a/src/main/main.cpp b/src/main/main.cpp index d748c80..c8e2dc1 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -15,6 +15,7 @@ using namespace boost; namespace po = boost::program_options; +const optional gen_rules(command_line_helper&); void solve_random_game(int, const sol_rules&); void solve_input_files(const vector, const sol_rules); void solve_game(const game_state&, const sol_rules&); @@ -27,7 +28,8 @@ int main(int argc, const char* argv[]) { } // Generates the rules of the solitaire from the game type - const sol_rules rules(clh.get_solitaire_type()); + const optional rules = gen_rules(clh); + if (!rules) return -1; // Retrieves the input files to be solved const vector input_files = clh.get_input_files(); @@ -35,16 +37,24 @@ int main(int argc, const char* argv[]) { // If there are no input files, solve a random deal based on the // supplied seed if (input_files.empty()) { - solve_random_game(clh.get_random_deal(), rules); + solve_random_game(clh.get_random_deal(), *rules); } // Otherwise, solve the input files else { - solve_input_files(input_files, rules); + solve_input_files(input_files, *rules); } return 0; } +const optional gen_rules(command_line_helper& clh) { + if (!clh.get_solitaire_type().empty()) { + return sol_rules::from_preset(clh.get_solitaire_type()); + } else { + return sol_rules::from_file(clh.get_rules_file()); + } +} + void solve_random_game(int seed, const sol_rules& rules) { LOG_INFO ("Attempting to solve with seed: " << seed << "..."); game_state gs(seed, rules); -- cgit v1.2.1 From a76f4164e8a13fcf3c561d79ecbda28a0bc0e49d Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 14 Nov 2017 01:49:46 +0000 Subject: Add new valid sol check to main --- src/main/main.cpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'src/main/main.cpp') diff --git a/src/main/main.cpp b/src/main/main.cpp index c8e2dc1..1d110ef 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -24,12 +24,12 @@ int main(int argc, const char* argv[]) { // Parses the command-line options command_line_helper clh; if (!clh.parse(argc, argv)) { - return 1; + return EXIT_FAILURE; } // Generates the rules of the solitaire from the game type const optional rules = gen_rules(clh); - if (!rules) return -1; + if (!rules) return EXIT_FAILURE; // Retrieves the input files to be solved const vector input_files = clh.get_input_files(); @@ -44,14 +44,21 @@ int main(int argc, const char* argv[]) { solve_input_files(input_files, *rules); } - return 0; + return EXIT_SUCCESS; } const optional gen_rules(command_line_helper& clh) { - if (!clh.get_solitaire_type().empty()) { - return sol_rules::from_preset(clh.get_solitaire_type()); - } else { - return sol_rules::from_file(clh.get_rules_file()); + try { + if (!clh.get_solitaire_type().empty()) { + return sol_rules::from_preset(clh.get_solitaire_type()); + } else { + return sol_rules::from_file(clh.get_rules_file()); + } + } catch (const runtime_error& error) { + string errmsg = "Error in rules generation: "; + errmsg += error.what(); + LOG_ERROR(errmsg); + return none; } } -- cgit v1.2.1