From bba8775ce7c63783303240470d4a4b8404c9498b Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 11 Nov 2017 11:36:27 +0000 Subject: Add src file directory structure --- src/main/main.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/main/main.cpp') diff --git a/src/main/main.cpp b/src/main/main.cpp index 5e11537..7a0d390 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -3,10 +3,10 @@ #include #include -#include "command_line_helper.h" -#include "deal_parser.h" -#include "game_state.h" -#include "solver.h" +#include "input-output/command_line_helper.h" +#include "input-output/deal_parser.h" +#include "game/game_state.h" +#include "solver/solver.h" using namespace rapidjson; -- cgit v1.2.1 From 8670a71468d0deb2bcd6d52d27873f9427c42ad9 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 11 Nov 2017 11:55:19 +0000 Subject: Refactor main --- src/main/main.cpp | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) (limited to 'src/main/main.cpp') diff --git a/src/main/main.cpp b/src/main/main.cpp index 7a0d390..77d5d5b 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -14,6 +14,8 @@ using namespace std; namespace po = boost::program_options; +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&); int main(int argc, const char* argv[]) { @@ -26,31 +28,40 @@ 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()); - int seed = clh.get_random_deal(); - if (seed >= 0) { - cout << "Attempting to solve with seed: " << seed << "...\n"; - game_state gs(seed, rules); - solve_game(gs, rules); + // Retrieves the input files to be solved + const vector input_files = clh.get_input_files(); + + // 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); + } + // Otherwise, solve the input files + else { + solve_input_files(input_files, rules); } - for (auto input_json : clh.get_input_files()) { - try { - // Attempts to read the user's json into a document - Document doc; - deal_parser::parse(doc, input_json); + return 0; +} + +void solve_random_game(int seed, const sol_rules& rules) { + cout << "Attempting to solve with seed: " << seed << "...\n"; + game_state gs(seed, rules); + solve_game(gs, rules); +} - // Creates a game state object from the json, plus a solver - game_state gs(doc); +void solve_input_files(const vector input_files, const sol_rules rules) { + for (const auto &input_json : input_files) { + // Attempts to read the user's json into a document + Document doc; + deal_parser::parse(doc, input_json); - cout << "Attempting to solve " << input_json << "...\n"; - solve_game(gs, rules); + // Creates a game state object from the json, plus a solver + game_state gs(doc); - } catch (runtime_error& e) { - cerr << "Error: " << e.what() << "\n"; - } + cout << "Attempting to solve " << input_json << "...\n"; + solve_game(gs, rules); } - - return 0; } void solve_game(const game_state& gs, const sol_rules& rules) { -- cgit v1.2.1 From 14f534452ac99913931c0513532dfbad2e277493 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 11 Nov 2017 12:20:31 +0000 Subject: Update logging --- src/main/main.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'src/main/main.cpp') diff --git a/src/main/main.cpp b/src/main/main.cpp index 77d5d5b..803b587 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -1,5 +1,3 @@ -#include - #include #include @@ -7,6 +5,7 @@ #include "input-output/deal_parser.h" #include "game/game_state.h" #include "solver/solver.h" +#include "input-output/log_helper.h" using namespace rapidjson; @@ -45,7 +44,7 @@ int main(int argc, const char* argv[]) { } void solve_random_game(int seed, const sol_rules& rules) { - cout << "Attempting to solve with seed: " << seed << "...\n"; + LOG_INFO << "Attempting to solve with seed: " << seed << "...\n"; game_state gs(seed, rules); solve_game(gs, rules); } @@ -59,7 +58,7 @@ void solve_input_files(const vector input_files, const sol_rules rules) // Creates a game state object from the json, plus a solver game_state gs(doc); - cout << "Attempting to solve " << input_json << "...\n"; + LOG_INFO << "Attempting to solve " << input_json << "...\n"; solve_game(gs, rules); } } -- cgit v1.2.1 From b1809820b192fb93e95daa50c5993da0b35fa03d Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sun, 12 Nov 2017 09:52:23 +0000 Subject: Solution output printing moved to main --- src/main/main.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'src/main/main.cpp') diff --git a/src/main/main.cpp b/src/main/main.cpp index 803b587..d748c80 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "input-output/command_line_helper.h" @@ -10,6 +11,7 @@ using namespace rapidjson; using namespace std; +using namespace boost; namespace po = boost::program_options; @@ -44,7 +46,7 @@ int main(int argc, const char* argv[]) { } void solve_random_game(int seed, const sol_rules& rules) { - LOG_INFO << "Attempting to solve with seed: " << seed << "...\n"; + LOG_INFO ("Attempting to solve with seed: " << seed << "..."); game_state gs(seed, rules); solve_game(gs, rules); } @@ -58,7 +60,7 @@ void solve_input_files(const vector input_files, const sol_rules rules) // Creates a game state object from the json, plus a solver game_state gs(doc); - LOG_INFO << "Attempting to solve " << input_json << "...\n"; + LOG_INFO ("Attempting to solve " << input_json << "..."); solve_game(gs, rules); } } @@ -66,6 +68,11 @@ void solve_input_files(const vector input_files, const sol_rules rules) void solve_game(const game_state& gs, const sol_rules& rules) { solver sol(gs, rules); - sol.run(); - cout << sol; + optional solution = sol.run(); + if (solution) { + cout << "Solution:\n" << *solution << "States Searched: " + << sol.get_states_searched() << "\n"; + } else { + cout << "Deal:\n" << sol.get_root() << "\nNo Possible Solution\n"; + } } -- cgit v1.2.1