diff options
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/game/game_state.cpp | 81 | ||||
| -rw-r--r-- | src/main/game/game_state.h | 11 | ||||
| -rw-r--r-- | src/main/game/sol_rules.cpp | 48 | ||||
| -rw-r--r-- | src/main/input-output/command_line_helper.cpp | 6 | ||||
| -rw-r--r-- | src/main/input-output/deal_parser.cpp | 118 | ||||
| -rw-r--r-- | src/main/input-output/deal_parser.h | 15 | ||||
| -rw-r--r-- | src/main/input-output/sol_preset_types.cpp | 4 | ||||
| -rw-r--r-- | src/main/main.cpp | 31 | ||||
| -rw-r--r-- | src/main/solver/solver.cpp | 4 | ||||
| -rw-r--r-- | src/main/solver/solver.h | 4 | ||||
| -rw-r--r-- | src/main/util/util.cpp | 24 | ||||
| -rw-r--r-- | src/main/util/util.h | 5 |
12 files changed, 161 insertions, 190 deletions
diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 2b77fec..b851f2a 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; @@ -21,44 +22,42 @@ typedef sol_rules::build_order ord; typedef sol_rules::build_policy pol; // Construct an initial game state from a JSON doc -game_state::game_state(const Document& doc) - : reserve(true, ord::NO_BUILD, pol::ANY_SUIT, false), - stock(true, ord::NO_BUILD, pol::ANY_SUIT, false), - waste(true, ord::NO_BUILD, pol::ANY_SUIT, false), - hole(false, ord::BOTH, pol::ANY_SUIT, true, rules.max_rank), - rules(sol_rules::from_preset("simple-black-hole")) { - // Construct tableau piles - 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); - - 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); +game_state::game_state(const sol_rules& s_rules) : + rules(s_rules), + reserve(pile::reserve_factory()), + stock(pile::stock_factory()), + waste(pile::waste_factory()), + hole(pile::hole_factory(s_rules.max_rank)) { + + // Creates the tableau piles + for (unsigned int i = 0; i < rules.tableau_pile_count; i++) { + tableau_piles.push_back(pile::tableau_factory(rules.build_ord)); + } + + // If there are foundation piles, create the relevant pile vectors + if (rules.foundations) { + for (int i = 0; i < 4; i++) { + foundations.push_back(pile::foundation_factory(pol(i))); } + } - tableau_piles.push_back(tableau_pile); + // If there are cell piles, create the relevant cell vectors + if (rules.cells > 0) { + for (unsigned int i = 0; i < rules.cells; i++) { + cells.push_back(pile::cell_factory()); + } } +} - // 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()); +// 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(int seed, 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)), - rules(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); @@ -84,11 +83,6 @@ game_state::game_state(int seed, const sol_rules& s_rules) : } } - // Creates the tableau piles - for (unsigned int i = 0; i < rules.tableau_pile_count; i++) { - tableau_piles.push_back(pile::tableau_factory(rules.build_ord)); - } - // Deal to the tableau piles (row-by-row) unsigned int t = 0; while (!deck.empty()) { @@ -98,20 +92,6 @@ game_state::game_state(int seed, const sol_rules& s_rules) : // Add the randomly generated card to the tableau piles tableau_piles[t++ % rules.tableau_pile_count].place(c); } - - // If there are foundation piles, create the relevant pile vectors - if (rules.foundations) { - for (int i = 0; i < 4; i++) { - foundations.push_back(pile::foundation_factory(pol(i))); - } - } - - // If there are cell piles, create the relevant cell vectors - if (rules.cells > 0) { - for (unsigned int i = 0; i < rules.cells; i++) { - cells.push_back(pile::cell_factory()); - } - } } // Generate a randomly ordered vector of cards @@ -357,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 873ca6f..4b96b72 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -15,12 +15,15 @@ #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 - game_state(const rapidjson::Document&); + game_state(const sol_rules&, const rapidjson::Document&); // Do the same from a 'rules' object - game_state(int seed, const sol_rules&); + game_state(const sol_rules&, int seed); const std::vector<game_state> get_next_legal_states() const; bool is_solved() const; @@ -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; @@ -44,6 +49,7 @@ private: void print_top_of_piles(std::ostream&, const std::vector<pile>&) const; void print_top_of_pile(std::ostream&, const pile&) const; + sol_rules rules; std::vector<pile> foundations; std::vector<pile> cells; std::vector<pile> tableau_piles; @@ -51,7 +57,6 @@ private: pile stock; pile waste; pile hole; - sol_rules rules; }; diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp index d2b0253..91b00c1 100644 --- a/src/main/game/sol_rules.cpp +++ b/src/main/game/sol_rules.cpp @@ -5,13 +5,12 @@ #include <string> #include <vector> #include <algorithm> -#include <input-output/log_helper.h> #include "rapidjson/error/en.h" -#include "util/util.h" #include "sol_rules.h" -#include "input-output/sol_preset_types.h" +#include "../util/util.h" +#include "../input-output/sol_preset_types.h" using namespace std; using namespace boost; @@ -38,18 +37,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 +72,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 +82,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 +95,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 +112,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 +129,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 +137,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 +145,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 +153,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 +161,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 +169,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/command_line_helper.cpp b/src/main/input-output/command_line_helper.cpp index 49bb300..97751a4 100644 --- a/src/main/input-output/command_line_helper.cpp +++ b/src/main/input-output/command_line_helper.cpp @@ -6,11 +6,11 @@ #include <vector> #include <boost/program_options.hpp> -#include <input-output/sol_preset_types.h> #include "command_line_helper.h" -#include "game/sol_rules.h" -#include "input-output/log_helper.h" +#include "../game/sol_rules.h" +#include "log_helper.h" +#include "sol_preset_types.h" using namespace std; diff --git a/src/main/input-output/deal_parser.cpp b/src/main/input-output/deal_parser.cpp index 8a9cef1..2ff5a02 100644 --- a/src/main/input-output/deal_parser.cpp +++ b/src/main/input-output/deal_parser.cpp @@ -2,95 +2,81 @@ // 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 <rapidjson/stringbuffer.h> - #include "deal_parser.h" +#include "../game/game_state.h" +#include "../util/util.h" using namespace std; using namespace rapidjson; -using namespace boost; -void deal_parser::parse(Document& doc, const std::string filename) { - const optional<string> in_json = read_file(filename); +typedef sol_rules::build_order ord; +typedef sol_rules::build_policy pol; - if (!in_json) { - throw runtime_error("could not read file " + filename); +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); } - Document sd; - gen_schema_doc(sd); - SchemaDocument schema(sd); - SchemaValidator validator(schema); - - if(!to_json(doc, *in_json)) { - throw runtime_error(filename + " not valid json"); + // Construct hole card + if (gs.rules.hole) { + parse_hole(gs, doc); } - if (!doc.Accept(validator)) { - throw runtime_error(schema_err_str(validator)); + // Construct filled cells + if (gs.rules.cells) { + parse_cells(gs, doc); } } -const optional<string> deal_parser::read_file(const string filename) { - // Reads the file into a string - std::ifstream ifstr(filename); - std::stringstream buf; - buf << ifstr.rdbuf(); +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()); - if (ifstr.fail()) { - return none; + if (gs.tableau_piles.size() != gs.rules.tableau_pile_count) { + util::json_parse_err("Incorrect number of tableau piles"); } - return buf.str(); -} + for (auto p = std::make_pair(begin(json_tab_piles.GetArray()), begin(gs.tableau_piles)); + p.second != end(gs.tableau_piles); + ++p.first, ++p.second) { -void deal_parser::gen_schema_doc(Document& d) { - const char* schema_json = "{\"$schema\":\"http://json-schema.org/draft-04/sc" - "hema#\",\"definitions\":{\"card\":{\"type\":\"string\",\"pattern" - "\":\"^(?i)([A|[1-9]|1[0-3]|J|Q|K])([C|D|H|S])$\"}},\"type\":\"" - "object\",\"properties\":{\"tableau piles\":{\"type\":\"array\"" - ",\"items\":{\"type\":\"array\",\"items\":{\"$ref\":\"#/definit" - "ions/card\"}}},\"hole card\":{\"$ref\":\"#/definitions/card\"}}" - ",\"additionalProperties\":false,\"anyOf\":[{\"required\":[\"tableau" - " piles\"]}]}"; - - to_json(d, schema_json); + for (auto& json_card : p.first->GetArray()) { + assert(json_card.IsString()); + p.second->place(card(json_card.GetString())); + } + } } -bool deal_parser::to_json(rapidjson::Document& d, const char* in) { - d.Parse(in); - return !d.HasParseError(); -} +void deal_parser::parse_hole(game_state &gs, const Document& doc) { + if (doc.HasMember("hole")) { + const Value &json_hole = doc["hole"]; + assert(json_hole.IsString()); -bool deal_parser::to_json(rapidjson::Document& d, const string in) { - return to_json(d, in.c_str()); + gs.hole.place(card(json_hole.GetString())); + } } -const string deal_parser::schema_err_str(const SchemaValidator& validator) { - // Input JSON is invalid according to the schema - // Output diagnostic information - string ret = "input JSON failed to match the required schema.\n"; +void deal_parser::parse_cells(game_state &gs, const Document& doc) { + if (doc.HasMember("cells")) { + const Value &json_cells = doc["cells"]; + assert(json_cells.IsArray()); - StringBuffer sb; - validator.GetInvalidSchemaPointer().StringifyUriFragment(sb); - ret += "Invalid schema keyword: "; - ret += validator.GetInvalidSchemaKeyword(); - ret += "\n"; + const auto json_cell_arr = json_cells.GetArray(); - sb.Clear(); - validator.GetInvalidDocumentPointer().StringifyUriFragment(sb); + if (json_cell_arr.Empty()) return; + else if (gs.rules.cells != json_cell_arr.Size()) { + util::json_parse_err("Incorrect number of cells"); + } - ret += "Invalid document: "; - ret += sb.GetString(); - ret += "\n"; + for (auto p = std::make_pair(begin(json_cell_arr), begin(gs.cells)); + p.second != end(gs.cells); + ++p.first, ++p.second) { - return ret; -}
\ No newline at end of file + auto json_card = p.first; + assert(json_card->IsString()); + p.second->place(json_card->GetString()); + } + } +} diff --git a/src/main/input-output/deal_parser.h b/src/main/input-output/deal_parser.h index 60eb8fb..255116b 100644 --- a/src/main/input-output/deal_parser.h +++ b/src/main/input-output/deal_parser.h @@ -11,17 +11,18 @@ #include <rapidjson/document.h> #include <rapidjson/schema.h> -#include "game/card.h" +#include "../game/card.h" +#include "../game/sol_rules.h" + +class game_state; class deal_parser { public: - static void parse(rapidjson::Document&, const std::string); + static void parse(game_state&, const rapidjson::Document&); private: - static const boost::optional<std::string> read_file(const std::string); - static void gen_schema_doc(rapidjson::Document&); - static bool to_json(rapidjson::Document&, const char*); - static bool to_json(rapidjson::Document&, const std::string); - static const std::string schema_err_str(const rapidjson::SchemaValidator&); + static void parse_tableau_piles(game_state&, const rapidjson::Document&); + static void parse_hole(game_state&, const rapidjson::Document&); + static void parse_cells(game_state&, const rapidjson::Document&); }; #endif //SOLVITAIRE_DEAL_PARSER_H diff --git a/src/main/input-output/sol_preset_types.cpp b/src/main/input-output/sol_preset_types.cpp index 19d529a..3c77b05 100644 --- a/src/main/input-output/sol_preset_types.cpp +++ b/src/main/input-output/sol_preset_types.cpp @@ -55,10 +55,10 @@ std::map<string, string> sol_preset_types::mp = { R"( { "tableau piles": { - "count": 9, + "count": 4, "build order": "no-build" }, - "max rank": 7, + "max rank": 5, "hole": true, "foundations": false } diff --git a/src/main/main.cpp b/src/main/main.cpp index 2de01d8..158ca07 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -3,10 +3,9 @@ #include <rapidjson/document.h> #include "input-output/command_line_helper.h" -#include "input-output/deal_parser.h" -#include "game/game_state.h" #include "solver/solver.h" #include "input-output/log_helper.h" +#include "util/util.h" using namespace rapidjson; @@ -17,7 +16,7 @@ namespace po = boost::program_options; const optional<sol_rules> gen_rules(command_line_helper&); void solve_random_game(int, const sol_rules&); -void solve_input_files(const vector<string>, const sol_rules); +void solve_input_files(const vector<string>, const sol_rules&); void solve_game(const game_state&, const sol_rules&); int main(int argc, const char* argv[]) { @@ -65,21 +64,27 @@ const optional<sol_rules> gen_rules(command_line_helper& clh) { void solve_random_game(int seed, const sol_rules& rules) { LOG_INFO ("Attempting to solve with seed: " << seed << "..."); - game_state gs(seed, rules); + game_state gs(rules, seed); solve_game(gs, rules); } -void solve_input_files(const vector<string> 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); +void solve_input_files(const vector<string> input_files, const sol_rules& rules) { + 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); - // Creates a game state object from the json, plus a solver - game_state gs(doc); + // Attempts to create a game state object from the json + game_state gs(rules, in_doc); - LOG_INFO ("Attempting to solve " << input_json << "..."); - solve_game(gs, rules); + LOG_INFO ("Attempting to solve " << input_file << "..."); + solve_game(gs, rules); + + } catch (const runtime_error& error) { + string errmsg = "Error parsing deal file: "; + errmsg += error.what(); + LOG_ERROR(errmsg); + } } } diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 02b9c20..c487c29 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -7,10 +7,8 @@ #include <iostream> #include <algorithm> -#include <gperftools/profiler.h> - #include "solver.h" -#include "input-output/log_helper.h" +#include "../input-output/log_helper.h" using namespace std; using namespace boost; diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index 73e5e24..bb5f8cf 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -14,8 +14,8 @@ #include <boost/multi_index/identity.hpp> #include <boost/multi_index/sequenced_index.hpp> -#include "game/game_state.h" -#include "game/sol_rules.h" +#include "../game/game_state.h" +#include "../game/sol_rules.h" class solver { public: 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); }; |
