diff options
| -rw-r--r-- | CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/main/game/game_state.cpp | 14 | ||||
| -rw-r--r-- | src/main/game/game_state.h | 7 | ||||
| -rw-r--r-- | src/main/game/sol_rules.cpp | 43 | ||||
| -rw-r--r-- | src/main/input-output/deal_parser.cpp | 63 | ||||
| -rw-r--r-- | src/main/input-output/deal_parser.h | 10 | ||||
| -rw-r--r-- | src/main/main.cpp | 13 | ||||
| -rw-r--r-- | src/main/util/util.cpp | 24 | ||||
| -rw-r--r-- | src/main/util/util.h | 5 |
9 files changed, 80 insertions, 101 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index c4fbffc..225d838 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(sources src/main/main.cpp src/main/input-output/command_line_helper.cpp src/main/input-output/command_line_helper.h src/main/input-output/log_helper.h src/main/game/card.h - src/main/game/card.cpp src/main/input-output/deal_parser.cpp src/main/input-output/deal_parser.h + src/main/game/card.cpp src/main/input-output/deal_parser.h src/main/input-output/deal_parser.cpp src/main/solver/solver.cpp src/main/solver/solver.h src/main/game/game_state.cpp src/main/game/game_state.h src/main/game/sol_rules.h src/main/game/sol_rules.cpp src/main/game/pile.cpp src/main/game/pile.h src/main/util/util.cpp src/main/util/util.h src/main/input-output/sol_preset_types.h src/main/input-output/sol_preset_types.cpp) diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 580e454..01a17fb 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -12,6 +12,7 @@ #include <boost/functional/hash.hpp> #include "game_state.h" +#include "../input-output/deal_parser.h" using namespace std; using namespace rapidjson; @@ -26,10 +27,18 @@ game_state::game_state(const sol_rules& s_rules) : reserve(pile::reserve_factory()), stock(pile::stock_factory()), waste(pile::waste_factory()), - hole(pile::hole_factory(s_rules.max_rank)) {} + hole(pile::hole_factory(s_rules.max_rank)) { +} + +// Construct an initial game state from a JSON doc +game_state::game_state(const sol_rules& s_rules, const Document& doc) : + game_state(s_rules) { + deal_parser::parse(*this, doc); +} // Construct an initial game state from a seed -game_state::game_state(const sol_rules& s_rules, int seed) : game_state(s_rules) { +game_state::game_state(const sol_rules& s_rules, int seed) : + game_state(s_rules) { vector<card> deck = shuffled_deck(seed, rules.max_rank); @@ -328,4 +337,3 @@ size_t hash_value(game_state const& gs) { size_t hash_value(vector<pile> const& vp) { return hash_range(begin(vp), end(vp)); } - diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 6c0b77b..4b96b72 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -15,10 +15,13 @@ #include "sol_rules.h" #include "pile.h" +class deal_parser; + class game_state { + friend class deal_parser; public: // Create a game state representation from a JSON doc - explicit game_state(const sol_rules&); + game_state(const sol_rules&, const rapidjson::Document&); // Do the same from a 'rules' object game_state(const sol_rules&, int seed); @@ -33,6 +36,8 @@ public: friend std::size_t hash_value(std::vector<pile> const&); private: + explicit game_state(const sol_rules&); + static std::vector<card> shuffled_deck(int, int); const game_state move(game_state&, pile*, pile*) const; diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp index d2b0253..66543de 100644 --- a/src/main/game/sol_rules.cpp +++ b/src/main/game/sol_rules.cpp @@ -38,18 +38,7 @@ int sol_rules::suit_val(pol bp) { const sol_rules sol_rules::from_file(const string rules_file) { sol_rules sr = get_default(); - - const optional<string> file_contents = util::read_file(rules_file); - if (!file_contents) { - throw runtime_error("Could not read rules file"); - } - - Document d; - if (d.Parse(file_contents->c_str()).HasParseError()) { - string errmsg = "Error parsing rules file JSON: "; - errmsg += GetParseError_En(d.GetParseError()); - throw runtime_error(errmsg); - } + Document d = util::get_file_json(rules_file); modify_sol_rules(sr, d); @@ -84,7 +73,7 @@ sol_rules sol_rules::get_default() { void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (!d.IsObject()) { - parse_err("JSON doc must be object"); + util::json_parse_err("JSON doc must be object"); } if (d.HasMember("tableau piles")) { @@ -94,7 +83,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["tableau piles"]["count"].IsInt()) { sr.tableau_pile_count = d["tableau piles"]["count"].GetInt(); } else { - parse_err("[tableau piles][count] must be an integer"); + util::json_parse_err("[tableau piles][count] must be an integer"); } } @@ -107,11 +96,11 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { } else if (bo_str == "descending") { sr.build_ord = build_order::DESCENDING; } else { - parse_err("[tableau piles][build order] is invalid"); + util::json_parse_err("[tableau piles][build order] is invalid"); } } else { - parse_err("[tableau piles][build order] must be an string"); + util::json_parse_err("[tableau piles][build order] must be an string"); } } @@ -124,16 +113,16 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { } else if (bo_str == "red-black") { sr.build_pol = build_policy::RED_BLACK; } else { - parse_err("[tableau piles][build policy] is invalid"); + util::json_parse_err("[tableau piles][build policy] is invalid"); } } else { - parse_err("[tableau piles][build policy] must be an string"); + util::json_parse_err("[tableau piles][build policy] must be an string"); } } } else { - parse_err("[tableau piles] must be an object"); + util::json_parse_err("[tableau piles] must be an object"); } } @@ -141,7 +130,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["max rank"].IsInt()) { sr.max_rank = d["max rank"].GetInt(); } else { - parse_err("[max rank] must be an integer"); + util::json_parse_err("[max rank] must be an integer"); } } @@ -149,7 +138,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["hole"].IsBool()) { sr.hole = d["hole"].GetBool(); } else { - parse_err("[hole] must be a boolean"); + util::json_parse_err("[hole] must be a boolean"); } } @@ -157,7 +146,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["foundations"].IsBool()) { sr.foundations = d["foundations"].GetBool(); } else { - parse_err("[foundations] must be a boolean"); + util::json_parse_err("[foundations] must be a boolean"); } } @@ -165,7 +154,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["cells"].IsInt()) { sr.cells = d["cells"].GetInt(); } else { - parse_err("[cells] must be an integer"); + util::json_parse_err("[cells] must be an integer"); } } @@ -173,7 +162,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["reserve size"].IsInt()) { sr.reserve_size = d["reserve size"].GetInt(); } else { - parse_err("[reserve size] must be an integer"); + util::json_parse_err("[reserve size] must be an integer"); } } @@ -181,11 +170,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["stock size"].IsInt()) { sr.stock_size = d["stock size"].GetInt(); } else { - parse_err("[stock size] must be an integer"); + util::json_parse_err("[stock size] must be an integer"); } } } - -void sol_rules::parse_err(const string& msg) { - throw runtime_error("Error in JSON doc: " + msg); -} diff --git a/src/main/input-output/deal_parser.cpp b/src/main/input-output/deal_parser.cpp index 76da77b..3ad973f 100644 --- a/src/main/input-output/deal_parser.cpp +++ b/src/main/input-output/deal_parser.cpp @@ -2,70 +2,39 @@ // Created by thecharlesblake on 10/21/17. // -#include <vector> -#include <fstream> -#include <iostream> -#include <sstream> - -#include <boost/optional.hpp> -#include <rapidjson/document.h> -#include <rapidjson/schema.h> - #include "deal_parser.h" +#include "../game/game_state.h" +#include "../util/util.h" using namespace std; using namespace rapidjson; -deal_parser::deal_parser(const sol_rules &r) : rules(r) {} - -game_state deal_parser::parse(const string& filename) const { - const Document doc = read_file(filename); +typedef sol_rules::build_order ord; +typedef sol_rules::build_policy pol; - game_state gs(rules); - /* +void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { // Construct tableau piles + if (gs.rules.tableau_pile_count > 0) { + parse_tableau_piles(gs, doc); + } +} + +void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& doc) { assert(doc.HasMember("tableau piles")); const Value& json_tab_piles = doc["tableau piles"]; assert(json_tab_piles.IsArray()); - int max_rank = 1; for (auto& json_tab : json_tab_piles.GetArray()) { - pile tableau_pile(true, rules.build_ord, pol::ANY_SUIT, false); + pile tableau_pile(true, gs.rules.build_ord, pol::ANY_SUIT, false); for (auto& json_card : json_tab.GetArray()) { - card c(json_card.GetString()); - if (c.get_rank() > max_rank) max_rank = c.get_rank(); - tableau_pile.place(c); + tableau_pile.place(card(json_card.GetString())); } - tableau_piles.push_back(tableau_pile); - } - - // Assign hole card - assert(doc.HasMember("hole card")); - const Value& json_h_card = doc["hole card"]; - assert(json_h_card.IsString()); - hole.place(json_h_card.GetString()); - */ - return gs; -} - -Document deal_parser::read_file(const string& filename) { - // Reads the file into a string - std::ifstream ifstr(filename); - std::stringstream buf; - buf << ifstr.rdbuf(); - - if (ifstr.fail()) { - throw runtime_error("could not read file " + filename); - } else { - Document d; + gs.tableau_piles.push_back(tableau_pile); - d.Parse(buf.str().c_str()); - if (d.HasParseError()) { - throw runtime_error(filename + " not valid json"); - } else { - return d; + if (gs.tableau_piles.size() != gs.rules.tableau_pile_count) { + util::json_parse_err("Incorrect number of tableau piles"); } } } diff --git a/src/main/input-output/deal_parser.h b/src/main/input-output/deal_parser.h index 1394f25..e8b817f 100644 --- a/src/main/input-output/deal_parser.h +++ b/src/main/input-output/deal_parser.h @@ -13,16 +13,14 @@ #include "game/card.h" #include "game/sol_rules.h" -#include "game/game_state.h" + +class game_state; class deal_parser { public: - explicit deal_parser(const sol_rules& rules); - game_state parse(const std::string&) const; + static void parse(game_state&, const rapidjson::Document&); private: - const sol_rules& rules; - - static rapidjson::Document read_file(const std::string&); + static void parse_tableau_piles(game_state&, const rapidjson::Document&); }; #endif //SOLVITAIRE_DEAL_PARSER_H diff --git a/src/main/main.cpp b/src/main/main.cpp index 54e9404..158ca07 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -3,9 +3,9 @@ #include <rapidjson/document.h> #include "input-output/command_line_helper.h" -#include "input-output/deal_parser.h" #include "solver/solver.h" #include "input-output/log_helper.h" +#include "util/util.h" using namespace rapidjson; @@ -69,14 +69,15 @@ void solve_random_game(int seed, const sol_rules& rules) { } void solve_input_files(const vector<string> input_files, const sol_rules& rules) { - deal_parser dp(rules); - - for (const string& input_json : input_files) { + for (const string& input_file : input_files) { try { + // Reads in the input file to a json doc + const Document in_doc = util::get_file_json(input_file); + // Attempts to create a game state object from the json - game_state gs = dp.parse(input_json); + game_state gs(rules, in_doc); - LOG_INFO ("Attempting to solve " << input_json << "..."); + LOG_INFO ("Attempting to solve " << input_file << "..."); solve_game(gs, rules); } catch (const runtime_error& error) { diff --git a/src/main/util/util.cpp b/src/main/util/util.cpp index 46038aa..7e7de8d 100644 --- a/src/main/util/util.cpp +++ b/src/main/util/util.cpp @@ -8,16 +8,28 @@ #include "util.h" using namespace std; -using namespace boost; +using namespace rapidjson; -const optional<string> util::read_file(const string& filename) { - ifstream ifstr(filename); - stringstream buf; +Document util::get_file_json(const string& filename) { + // Reads the file into a string + std::ifstream ifstr(filename); + std::stringstream buf; buf << ifstr.rdbuf(); if (ifstr.fail()) { - return none; + throw runtime_error("could not read file " + filename); } else { - return buf.str(); + Document d; + + d.Parse(buf.str().c_str()); + if (d.HasParseError()) { + throw runtime_error(filename + " not valid json"); + } else { + return d; + } } } + +void util::json_parse_err(const string& msg) { + throw runtime_error("Error in JSON doc: " + msg); +} diff --git a/src/main/util/util.h b/src/main/util/util.h index 1721daa..de2a94d 100644 --- a/src/main/util/util.h +++ b/src/main/util/util.h @@ -7,11 +7,12 @@ #include <string> -#include <boost/optional.hpp> +#include <rapidjson/schema.h> class util { public: - static const boost::optional<std::string> read_file(const std::string&); + static rapidjson::Document get_file_json(const std::string&); + static void json_parse_err(const std::string& msg); }; |
