From 758d919cb5d600a421636ce54df9f28b6fc832ba Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 12:51:47 +0000 Subject: Refactor sol_rules into seperate parser class --- src/main/game/sol_rules.cpp | 175 --------------------------------- src/main/game/sol_rules.h | 12 +-- src/main/input-output/rules_parser.cpp | 173 ++++++++++++++++++++++++++++++++ src/main/input-output/rules_parser.h | 21 ++++ src/main/main.cpp | 9 +- src/test/test_helper.cpp | 5 +- 6 files changed, 203 insertions(+), 192 deletions(-) delete mode 100644 src/main/game/sol_rules.cpp create mode 100644 src/main/input-output/rules_parser.cpp create mode 100644 src/main/input-output/rules_parser.h (limited to 'src') diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp deleted file mode 100644 index 28c2af7..0000000 --- a/src/main/game/sol_rules.cpp +++ /dev/null @@ -1,175 +0,0 @@ -// -// Created by thecharlesblake on 10/28/17. -// - -#include -#include -#include - -#include "rapidjson/error/en.h" - -#include "sol_rules.h" -#include "../input-output/json_helper.h" -#include "../input-output/sol_preset_types.h" - -using namespace std; -using namespace boost; -using namespace rapidjson; - -//////////// -// Static // -//////////// - -typedef sol_rules::build_policy pol; - -const sol_rules sol_rules::from_file(const string rules_file) { - sol_rules sr = get_default(); - Document d = json_helper::get_file_json(rules_file); - - modify_sol_rules(sr, d); - - return sr; -} - -const sol_rules sol_rules::from_preset(const string sol_type) { - sol_rules sr = get_default(); - - const string preset_json = sol_preset_types::get(sol_type); - - Document d; - d.Parse(preset_json.c_str()); - assert(!d.HasParseError()); - - modify_sol_rules(sr, d); - return sr; -} - -sol_rules sol_rules::get_default() { - sol_rules sr; - const string default_json = sol_preset_types::get("default"); - - Document d; - d.Parse(default_json.c_str()); - assert(!d.HasParseError()); - - modify_sol_rules(sr, d); - return sr; -} - -void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { - - if (!d.IsObject()) { - json_helper::json_parse_err("JSON doc must be object"); - } - - if (d.HasMember("tableau piles")) { - if (d["tableau piles"].IsObject()) { - - if (d["tableau piles"].HasMember("count")) { - if (d["tableau piles"]["count"].IsInt()) { - sr.tableau_pile_count = static_cast(d["tableau piles"]["count"].GetInt()); - } else { - json_helper::json_parse_err("[tableau piles][count] must be an integer"); - } - } - - if (d["tableau piles"].HasMember("build order")) { - if (d["tableau piles"]["build order"].IsString()) { - string bo_str = d["tableau piles"]["build order"].GetString(); - - if (bo_str == "ascending") { - sr.build_ord = build_order::ASCENDING; - } else if (bo_str == "descending") { - sr.build_ord = build_order::DESCENDING; - } else { - json_helper::json_parse_err("[tableau piles][build order] is invalid"); - } - - } else { - json_helper::json_parse_err("[tableau piles][build order] must be an string"); - } - } - - if (d["tableau piles"].HasMember("build policy")) { - if (d["tableau piles"]["build policy"].IsString()) { - string bp_str = d["tableau piles"]["build policy"].GetString(); - - if (bp_str == "any-suit") { - sr.build_pol = build_policy::ANY_SUIT; - } else if (bp_str == "red-black") { - sr.build_pol = build_policy::RED_BLACK; - } else if (bp_str == "same-suit") { - sr.build_pol = build_policy::SAME_SUIT; - } else if (bp_str == "no-build") { - sr.build_pol = build_policy::NO_BUILD; - } else { - json_helper::json_parse_err("[tableau piles][build policy] is invalid"); - } - - } else { - json_helper::json_parse_err("[tableau piles][build policy] must be an string"); - } - } - - if (d["tableau piles"].HasMember("spaces policy")) { - if (d["tableau piles"]["spaces policy"].IsString()) { - string sp_str = d["tableau piles"]["spaces policy"].GetString(); - - if (sp_str == "any") { - sr.spaces_pol = spaces_policy::ANY; - } else if (sp_str == "no-build") { - sr.spaces_pol = spaces_policy::NO_BUILD; - } else { - json_helper::json_parse_err("[tableau piles][spaces policy] is invalid"); - } - - } else { - json_helper::json_parse_err("[tableau piles][spaces policy] must be an string"); - } - } - - } else { - json_helper::json_parse_err("[tableau piles] must be an object"); - } - } - - if (d.HasMember("max rank")) { - if (d["max rank"].IsInt()) { - sr.max_rank = static_cast(d["max rank"].GetInt()); - } else { - json_helper::json_parse_err("[max rank] must be an integer"); - } - } - - if (d.HasMember("hole")) { - if (d["hole"].IsBool()) { - sr.hole = d["hole"].GetBool(); - } else { - json_helper::json_parse_err("[hole] must be a boolean"); - } - } - - if (d.HasMember("foundations")) { - if (d["foundations"].IsBool()) { - sr.foundations = d["foundations"].GetBool(); - } else { - json_helper::json_parse_err("[foundations] must be a boolean"); - } - } - - if (d.HasMember("cells")) { - if (d["cells"].IsInt()) { - sr.cells = static_cast(d["cells"].GetInt()); - } else { - json_helper::json_parse_err("[cells] must be an integer"); - } - } - - if (d.HasMember("stock size")) { - if (d["stock size"].IsInt()) { - sr.stock_size = static_cast(d["stock size"].GetInt()); - } else { - json_helper::json_parse_err("[stock size] must be an integer"); - } - } -} diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h index 3d4995d..1e6dfa4 100644 --- a/src/main/game/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -5,16 +5,13 @@ #ifndef SOLVITAIRE_SOL_RULES_H #define SOLVITAIRE_SOL_RULES_H -#include #include -#include #include #include "card.h" -class sol_rules { -public: +struct sol_rules { // Tableau policies enum class build_order { DESCENDING, @@ -31,9 +28,6 @@ public: ANY }; - static const sol_rules from_preset(std::string); - static const sol_rules from_file(std::string); - uint8_t tableau_pile_count; build_order build_ord; build_policy build_pol; @@ -43,10 +37,6 @@ public: bool foundations; uint8_t cells; uint8_t stock_size; - -private: - static sol_rules get_default(); - static void modify_sol_rules(sol_rules&, rapidjson::Document&); }; #endif //SOLVITAIRE_SOL_RULES_H diff --git a/src/main/input-output/rules_parser.cpp b/src/main/input-output/rules_parser.cpp new file mode 100644 index 0000000..81cdd20 --- /dev/null +++ b/src/main/input-output/rules_parser.cpp @@ -0,0 +1,173 @@ +// +// Created by thecharlesblake on 1/3/18. +// + +#include +#include + +#include "rules_parser.h" +#include "../input-output/json_helper.h" +#include "../input-output/sol_preset_types.h" + +using namespace std; +using namespace rapidjson; + +//////////// +// Static // +//////////// + +typedef sol_rules::build_order ord; +typedef sol_rules::build_policy pol; +typedef sol_rules::spaces_policy s_pol; + +const sol_rules rules_parser::from_file(const string rules_file) { + sol_rules sr = get_default(); + Document d = json_helper::get_file_json(rules_file); + + modify_sol_rules(sr, d); + + return sr; +} + +const sol_rules rules_parser::from_preset(const string sol_type) { + sol_rules sr = get_default(); + + const string preset_json = sol_preset_types::get(sol_type); + + Document d; + d.Parse(preset_json.c_str()); + assert(!d.HasParseError()); + + modify_sol_rules(sr, d); + return sr; +} + +sol_rules rules_parser::get_default() { + sol_rules sr; + const string default_json = sol_preset_types::get("default"); + + Document d; + d.Parse(default_json.c_str()); + assert(!d.HasParseError()); + + modify_sol_rules(sr, d); + return sr; +} + +void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { + + if (!d.IsObject()) { + json_helper::json_parse_err("JSON doc must be object"); + } + + if (d.HasMember("tableau piles")) { + if (d["tableau piles"].IsObject()) { + + if (d["tableau piles"].HasMember("count")) { + if (d["tableau piles"]["count"].IsInt()) { + sr.tableau_pile_count = static_cast(d["tableau piles"]["count"].GetInt()); + } else { + json_helper::json_parse_err("[tableau piles][count] must be an integer"); + } + } + + if (d["tableau piles"].HasMember("build order")) { + if (d["tableau piles"]["build order"].IsString()) { + string bo_str = d["tableau piles"]["build order"].GetString(); + + if (bo_str == "ascending") { + sr.build_ord = ord::ASCENDING; + } else if (bo_str == "descending") { + sr.build_ord = ord::DESCENDING; + } else { + json_helper::json_parse_err("[tableau piles][build order] is invalid"); + } + + } else { + json_helper::json_parse_err("[tableau piles][build order] must be an string"); + } + } + + if (d["tableau piles"].HasMember("build policy")) { + if (d["tableau piles"]["build policy"].IsString()) { + string bp_str = d["tableau piles"]["build policy"].GetString(); + + if (bp_str == "any-suit") { + sr.build_pol = pol::ANY_SUIT; + } else if (bp_str == "red-black") { + sr.build_pol = pol::RED_BLACK; + } else if (bp_str == "same-suit") { + sr.build_pol = pol::SAME_SUIT; + } else if (bp_str == "no-build") { + sr.build_pol = pol::NO_BUILD; + } else { + json_helper::json_parse_err("[tableau piles][build policy] is invalid"); + } + + } else { + json_helper::json_parse_err("[tableau piles][build policy] must be an string"); + } + } + + if (d["tableau piles"].HasMember("spaces policy")) { + if (d["tableau piles"]["spaces policy"].IsString()) { + string sp_str = d["tableau piles"]["spaces policy"].GetString(); + + if (sp_str == "any") { + sr.spaces_pol = s_pol::ANY; + } else if (sp_str == "no-build") { + sr.spaces_pol = s_pol::NO_BUILD; + } else { + json_helper::json_parse_err("[tableau piles][spaces policy] is invalid"); + } + + } else { + json_helper::json_parse_err("[tableau piles][spaces policy] must be an string"); + } + } + + } else { + json_helper::json_parse_err("[tableau piles] must be an object"); + } + } + + if (d.HasMember("max rank")) { + if (d["max rank"].IsInt()) { + sr.max_rank = static_cast(d["max rank"].GetInt()); + } else { + json_helper::json_parse_err("[max rank] must be an integer"); + } + } + + if (d.HasMember("hole")) { + if (d["hole"].IsBool()) { + sr.hole = d["hole"].GetBool(); + } else { + json_helper::json_parse_err("[hole] must be a boolean"); + } + } + + if (d.HasMember("foundations")) { + if (d["foundations"].IsBool()) { + sr.foundations = d["foundations"].GetBool(); + } else { + json_helper::json_parse_err("[foundations] must be a boolean"); + } + } + + if (d.HasMember("cells")) { + if (d["cells"].IsInt()) { + sr.cells = static_cast(d["cells"].GetInt()); + } else { + json_helper::json_parse_err("[cells] must be an integer"); + } + } + + if (d.HasMember("stock size")) { + if (d["stock size"].IsInt()) { + sr.stock_size = static_cast(d["stock size"].GetInt()); + } else { + json_helper::json_parse_err("[stock size] must be an integer"); + } + } +} diff --git a/src/main/input-output/rules_parser.h b/src/main/input-output/rules_parser.h new file mode 100644 index 0000000..a478a01 --- /dev/null +++ b/src/main/input-output/rules_parser.h @@ -0,0 +1,21 @@ +// +// Created by thecharlesblake on 1/3/18. +// + +#ifndef SOLVITAIRE_RULES_PARSER_H +#define SOLVITAIRE_RULES_PARSER_H + +#include "../game/sol_rules.h" + +class rules_parser { +public: + static const sol_rules from_file(std::string); + static const sol_rules from_preset(std::string); + +private: + static sol_rules get_default(); + static void modify_sol_rules(sol_rules&, rapidjson::Document&); +}; + + +#endif //SOLVITAIRE_RULES_PARSER_H diff --git a/src/main/main.cpp b/src/main/main.cpp index 342cdd0..5386da6 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -3,9 +3,10 @@ #include #include "input-output/command_line_helper.h" -#include "solver/solver.h" #include "input-output/log_helper.h" #include "input-output/json_helper.h" +#include "input-output/rules_parser.h" +#include "solver/solver.h" using namespace rapidjson; @@ -16,7 +17,7 @@ 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_input_files(vector, const sol_rules&); void solve_game(const game_state&, const sol_rules&); int main(int argc, const char* argv[]) { @@ -50,9 +51,9 @@ int main(int argc, const char* argv[]) { const optional gen_rules(command_line_helper& clh) { try { if (!clh.get_solitaire_type().empty()) { - return sol_rules::from_preset(clh.get_solitaire_type()); + return rules_parser::from_preset(clh.get_solitaire_type()); } else { - return sol_rules::from_file(clh.get_rules_file()); + return rules_parser::from_file(clh.get_rules_file()); } } catch (const runtime_error& error) { string errmsg = "Error in rules generation: "; diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index 59e7584..117273c 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -8,16 +8,17 @@ #include "../main/game/game_state.h" #include "../main/solver/solver.h" #include "../main/input-output/json_helper.h" +#include "../main/input-output/rules_parser.h" using namespace rapidjson; using namespace std; bool test_helper::is_solvable(const std::string& input_file, const std::string& preset_type) { const Document in_doc = json_helper::get_file_json(input_file); - const sol_rules rules = sol_rules::from_preset(preset_type); + const sol_rules rules = rules_parser::from_preset(preset_type); game_state gs(rules, in_doc); solver sol(gs, rules); - return sol.run() ? true : false; + return sol.run(); } -- cgit v1.2.1