From 079c72c083a35c5d22a70e2d54bb1567db98321b Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 26 Dec 2017 12:03:35 +0000 Subject: Rework basic deal-parser outline --- src/main/input-output/deal_parser.cpp | 82 +++++++---------------------------- src/main/input-output/deal_parser.h | 13 +++--- src/main/main.cpp | 26 ++++++----- 3 files changed, 38 insertions(+), 83 deletions(-) (limited to 'src') diff --git a/src/main/input-output/deal_parser.cpp b/src/main/input-output/deal_parser.cpp index 8a9cef1..9fcd1d1 100644 --- a/src/main/input-output/deal_parser.cpp +++ b/src/main/input-output/deal_parser.cpp @@ -10,87 +10,37 @@ #include #include #include -#include #include "deal_parser.h" using namespace std; using namespace rapidjson; -using namespace boost; -void deal_parser::parse(Document& doc, const std::string filename) { - const optional in_json = read_file(filename); +deal_parser::deal_parser(const sol_rules &r) : rules(r) {} - if (!in_json) { - throw runtime_error("could not read file " + filename); - } - - 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"); - } +game_state deal_parser::parse(const string& filename) const { + const Document doc = read_file(filename); - if (!doc.Accept(validator)) { - throw runtime_error(schema_err_str(validator)); - } + game_state gs; + return gs; } -const optional deal_parser::read_file(const string filename) { +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()) { - return none; + throw runtime_error("could not read file " + filename); + } else { + Document d; + + d.Parse(buf.str().c_str()); + if (d.HasParseError()) { + throw runtime_error(filename + " not valid json"); + } else { + return d; + } } - - return buf.str(); -} - -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); -} - -bool deal_parser::to_json(rapidjson::Document& d, const char* in) { - d.Parse(in); - return !d.HasParseError(); -} - -bool deal_parser::to_json(rapidjson::Document& d, const string in) { - return to_json(d, in.c_str()); } - -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"; - - StringBuffer sb; - validator.GetInvalidSchemaPointer().StringifyUriFragment(sb); - ret += "Invalid schema keyword: "; - ret += validator.GetInvalidSchemaKeyword(); - ret += "\n"; - - sb.Clear(); - validator.GetInvalidDocumentPointer().StringifyUriFragment(sb); - - ret += "Invalid document: "; - ret += sb.GetString(); - ret += "\n"; - - return ret; -} \ No newline at end of file diff --git a/src/main/input-output/deal_parser.h b/src/main/input-output/deal_parser.h index 60eb8fb..1394f25 100644 --- a/src/main/input-output/deal_parser.h +++ b/src/main/input-output/deal_parser.h @@ -12,16 +12,17 @@ #include #include "game/card.h" +#include "game/sol_rules.h" +#include "game/game_state.h" class deal_parser { public: - static void parse(rapidjson::Document&, const std::string); + explicit deal_parser(const sol_rules& rules); + game_state parse(const std::string&) const; private: - static const boost::optional 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&); + const sol_rules& rules; + + static rapidjson::Document read_file(const std::string&); }; #endif //SOLVITAIRE_DEAL_PARSER_H diff --git a/src/main/main.cpp b/src/main/main.cpp index 2de01d8..ddf8f73 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -4,7 +4,6 @@ #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" @@ -17,7 +16,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(const vector, const sol_rules&); void solve_game(const game_state&, const sol_rules&); int main(int argc, const char* argv[]) { @@ -69,17 +68,22 @@ void solve_random_game(int seed, const sol_rules& rules) { solve_game(gs, rules); } -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); +void solve_input_files(const vector input_files, const sol_rules& rules) { + deal_parser dp(rules); - // Creates a game state object from the json, plus a solver - game_state gs(doc); + for (const string& input_json : input_files) { + try { + // Attempts to create a game state object from the json + game_state gs = dp.parse(input_json); - LOG_INFO ("Attempting to solve " << input_json << "..."); - solve_game(gs, rules); + LOG_INFO ("Attempting to solve " << input_json << "..."); + solve_game(gs, rules); + + } catch (const runtime_error& error) { + string errmsg = "Error parsing deal file: "; + errmsg += error.what(); + LOG_ERROR(errmsg); + } } } -- cgit v1.2.1 From bf23d041cb9e7d5f1dcad6b26a93c43a69fce8bc Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 26 Dec 2017 13:00:48 +0000 Subject: Refactor game state to prep for deal parse --- src/main/game/game_state.cpp | 41 +++++------------------------------ src/main/game/game_state.h | 6 ++--- src/main/input-output/deal_parser.cpp | 27 ++++++++++++++++++++++- src/main/main.cpp | 2 +- 4 files changed, 36 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 2b77fec..580e454 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -21,44 +21,15 @@ 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); - } - - 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()); -} - -// Construct an initial game state from a seed -game_state::game_state(int seed, const sol_rules& s_rules) : +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)), - rules(s_rules) { + hole(pile::hole_factory(s_rules.max_rank)) {} + +// Construct an initial game state from a seed +game_state::game_state(const sol_rules& s_rules, int seed) : game_state(s_rules) { vector deck = shuffled_deck(seed, rules.max_rank); diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 873ca6f..6c0b77b 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -18,9 +18,9 @@ class game_state { public: // Create a game state representation from a JSON doc - game_state(const rapidjson::Document&); + explicit game_state(const sol_rules&); // Do the same from a 'rules' object - game_state(int seed, const sol_rules&); + game_state(const sol_rules&, int seed); const std::vector get_next_legal_states() const; bool is_solved() const; @@ -44,6 +44,7 @@ private: void print_top_of_piles(std::ostream&, const std::vector&) const; void print_top_of_pile(std::ostream&, const pile&) const; + sol_rules rules; std::vector foundations; std::vector cells; std::vector tableau_piles; @@ -51,7 +52,6 @@ private: pile stock; pile waste; pile hole; - sol_rules rules; }; diff --git a/src/main/input-output/deal_parser.cpp b/src/main/input-output/deal_parser.cpp index 9fcd1d1..76da77b 100644 --- a/src/main/input-output/deal_parser.cpp +++ b/src/main/input-output/deal_parser.cpp @@ -21,7 +21,32 @@ 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); - game_state gs; + game_state gs(rules); + /* + // 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); + } + + 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; } diff --git a/src/main/main.cpp b/src/main/main.cpp index ddf8f73..54e9404 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -64,7 +64,7 @@ const optional 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); } -- cgit v1.2.1 From b8f4cae869eace06df73ac7c1c390cc42cfe499b Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 26 Dec 2017 17:29:24 +0000 Subject: Add basic tableau deal parsing --- src/main/game/game_state.cpp | 14 ++++++-- src/main/game/game_state.h | 7 +++- src/main/game/sol_rules.cpp | 43 ++++++++---------------- src/main/input-output/deal_parser.cpp | 63 +++++++++-------------------------- src/main/input-output/deal_parser.h | 10 +++--- src/main/main.cpp | 13 ++++---- src/main/util/util.cpp | 24 +++++++++---- src/main/util/util.h | 5 +-- 8 files changed, 79 insertions(+), 100 deletions(-) (limited to 'src') 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 #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 deck = shuffled_deck(seed, rules.max_rank); @@ -328,4 +337,3 @@ size_t hash_value(game_state const& gs) { size_t hash_value(vector 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 const&); private: + explicit game_state(const sol_rules&); + static std::vector 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 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 -#include -#include -#include - -#include -#include -#include - #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 #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 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 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 -#include +#include class util { public: - static const boost::optional read_file(const std::string&); + static rapidjson::Document get_file_json(const std::string&); + static void json_parse_err(const std::string& msg); }; -- cgit v1.2.1 From a84d8dc3860c324f6dc622e8eb9f983acd7f692c Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 26 Dec 2017 18:05:10 +0000 Subject: Add basic outline for spanish patience testing --- src/test/main_test.cpp | 19 ------------------- src/test/spanish_patience_test.cpp | 11 +++++++++++ src/test/test_helper.cpp | 9 +++++++++ src/test/test_helper.h | 16 ++++++++++++++++ 4 files changed, 36 insertions(+), 19 deletions(-) delete mode 100644 src/test/main_test.cpp create mode 100644 src/test/spanish_patience_test.cpp create mode 100644 src/test/test_helper.cpp create mode 100644 src/test/test_helper.h (limited to 'src') diff --git a/src/test/main_test.cpp b/src/test/main_test.cpp deleted file mode 100644 index 3f2081f..0000000 --- a/src/test/main_test.cpp +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by thecharlesblake on 10/4/17. -// - -#include - -const int actualValTrue = 4; -const int actualValFalse = 8; -const int expectVal = 4; - -namespace { - TEST(StrCompare, CStrEqual) { - EXPECT_EQ(expectVal, actualValTrue); - } - - TEST(StrCompare, CStrNotEqual) { - EXPECT_NE(expectVal, actualValFalse); - } -} diff --git a/src/test/spanish_patience_test.cpp b/src/test/spanish_patience_test.cpp new file mode 100644 index 0000000..f75fc8b --- /dev/null +++ b/src/test/spanish_patience_test.cpp @@ -0,0 +1,11 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include + +#include "test_helper.h" + +TEST(SpanishPatience, SPTestName) { + EXPECT_TRUE(test_helper::is_solvable("i", "simple-spanish-patience")); +} diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp new file mode 100644 index 0000000..5e08bc3 --- /dev/null +++ b/src/test/test_helper.cpp @@ -0,0 +1,9 @@ +// +// Created by thecharlesblake on 12/26/17. +// + +#include "test_helper.h" + +bool test_helper::is_solvable(std::string, std::string) { + return false; +} diff --git a/src/test/test_helper.h b/src/test/test_helper.h new file mode 100644 index 0000000..38bdfc0 --- /dev/null +++ b/src/test/test_helper.h @@ -0,0 +1,16 @@ +// +// Created by thecharlesblake on 12/26/17. +// + +#ifndef SOLVITAIRE_TEST_HELPER_H +#define SOLVITAIRE_TEST_HELPER_H + +#include + +class test_helper { +public: + static bool is_solvable(std::string, std::string); +}; + + +#endif //SOLVITAIRE_TEST_HELPER_H -- cgit v1.2.1 From f8931f348ef456d9fac78b5cadad280f81dfcfc6 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 27 Dec 2017 00:04:32 +0000 Subject: First sp test working --- src/main/game/game_state.cpp | 38 +++++++++++++-------------- src/main/game/sol_rules.cpp | 5 ++-- src/main/input-output/command_line_helper.cpp | 6 ++--- src/main/input-output/deal_parser.cpp | 17 ++++++------ src/main/input-output/deal_parser.h | 4 +-- src/main/solver/solver.cpp | 4 +-- src/main/solver/solver.h | 4 +-- src/test/resources/SPTestName.json | 7 +++++ src/test/spanish_patience_test.cpp | 6 ++++- src/test/test_helper.cpp | 18 +++++++++++-- src/test/test_helper.h | 2 +- 11 files changed, 66 insertions(+), 45 deletions(-) create mode 100644 src/test/resources/SPTestName.json (limited to 'src') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 01a17fb..b851f2a 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -28,6 +28,25 @@ game_state::game_state(const sol_rules& s_rules) : 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))); + } + } + + // 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()); + } + } } // Construct an initial game state from a JSON doc @@ -64,11 +83,6 @@ game_state::game_state(const sol_rules& s_rules, int seed) : } } - // 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()) { @@ -78,20 +92,6 @@ game_state::game_state(const sol_rules& s_rules, int seed) : // 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 diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp index 66543de..91b00c1 100644 --- a/src/main/game/sol_rules.cpp +++ b/src/main/game/sol_rules.cpp @@ -5,13 +5,12 @@ #include #include #include -#include #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; 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 #include -#include #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 3ad973f..fecc7d9 100644 --- a/src/main/input-output/deal_parser.cpp +++ b/src/main/input-output/deal_parser.cpp @@ -24,17 +24,16 @@ void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& const Value& json_tab_piles = doc["tableau piles"]; assert(json_tab_piles.IsArray()); - for (auto& json_tab : json_tab_piles.GetArray()) { - pile tableau_pile(true, gs.rules.build_ord, pol::ANY_SUIT, false); - - for (auto& json_card : json_tab.GetArray()) { - tableau_pile.place(card(json_card.GetString())); - } + if (gs.tableau_piles.size() != gs.rules.tableau_pile_count) { + util::json_parse_err("Incorrect number of tableau piles"); + } - gs.tableau_piles.push_back(tableau_pile); + 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) { - if (gs.tableau_piles.size() != gs.rules.tableau_pile_count) { - util::json_parse_err("Incorrect number of tableau piles"); + for (auto& json_card : p.first->GetArray()) { + p.second->place(card(json_card.GetString())); } } } diff --git a/src/main/input-output/deal_parser.h b/src/main/input-output/deal_parser.h index e8b817f..93d30fc 100644 --- a/src/main/input-output/deal_parser.h +++ b/src/main/input-output/deal_parser.h @@ -11,8 +11,8 @@ #include #include -#include "game/card.h" -#include "game/sol_rules.h" +#include "../game/card.h" +#include "../game/sol_rules.h" class game_state; 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 #include -#include - #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 #include -#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/test/resources/SPTestName.json b/src/test/resources/SPTestName.json new file mode 100644 index 0000000..6d1586c --- /dev/null +++ b/src/test/resources/SPTestName.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["2S", "AS", "AD"], + ["2D", "2H", "AH"], + ["2C", "AC"] + ] +} \ No newline at end of file diff --git a/src/test/spanish_patience_test.cpp b/src/test/spanish_patience_test.cpp index f75fc8b..4168193 100644 --- a/src/test/spanish_patience_test.cpp +++ b/src/test/spanish_patience_test.cpp @@ -6,6 +6,10 @@ #include "test_helper.h" +typedef test_helper th; + +const static std::string type = "simple-spanish-patience"; + TEST(SpanishPatience, SPTestName) { - EXPECT_TRUE(test_helper::is_solvable("i", "simple-spanish-patience")); + EXPECT_TRUE(th::is_solvable("SPTestName", type)); } diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index 5e08bc3..2e08829 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -2,8 +2,22 @@ // Created by thecharlesblake on 12/26/17. // +#include + #include "test_helper.h" +#include "../main/game/game_state.h" +#include "../main/solver/solver.h" +#include "../main/util/util.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 = util::get_file_json("resources/" + input_file + ".json"); + const sol_rules rules = sol_rules::from_preset(preset_type); + + game_state gs(rules, in_doc); + solver sol(gs, rules); -bool test_helper::is_solvable(std::string, std::string) { - return false; + return sol.run() ? true : false; } diff --git a/src/test/test_helper.h b/src/test/test_helper.h index 38bdfc0..5b8e755 100644 --- a/src/test/test_helper.h +++ b/src/test/test_helper.h @@ -9,7 +9,7 @@ class test_helper { public: - static bool is_solvable(std::string, std::string); + static bool is_solvable(const std::string&, const std::string&); }; -- cgit v1.2.1 From 6c4bdf37d6bebe3562df9c9b459e6b7036f21dea Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 27 Dec 2017 00:23:07 +0000 Subject: All basic sp tests complete --- src/test/resources/SPTestName.json | 7 ------- .../resources/spanish_patience/ComplexSolvable.json | 7 +++++++ .../resources/spanish_patience/ComplexUnsolvable.json | 7 +++++++ src/test/resources/spanish_patience/SimpleSolvable.json | 7 +++++++ .../resources/spanish_patience/SimpleUnsolvable.json | 7 +++++++ src/test/spanish_patience_test.cpp | 17 +++++++++++++++-- src/test/test_helper.cpp | 2 +- 7 files changed, 44 insertions(+), 10 deletions(-) delete mode 100644 src/test/resources/SPTestName.json create mode 100644 src/test/resources/spanish_patience/ComplexSolvable.json create mode 100644 src/test/resources/spanish_patience/ComplexUnsolvable.json create mode 100644 src/test/resources/spanish_patience/SimpleSolvable.json create mode 100644 src/test/resources/spanish_patience/SimpleUnsolvable.json (limited to 'src') diff --git a/src/test/resources/SPTestName.json b/src/test/resources/SPTestName.json deleted file mode 100644 index 6d1586c..0000000 --- a/src/test/resources/SPTestName.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "tableau piles": [ - ["2S", "AS", "AD"], - ["2D", "2H", "AH"], - ["2C", "AC"] - ] -} \ No newline at end of file diff --git a/src/test/resources/spanish_patience/ComplexSolvable.json b/src/test/resources/spanish_patience/ComplexSolvable.json new file mode 100644 index 0000000..c48ce47 --- /dev/null +++ b/src/test/resources/spanish_patience/ComplexSolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["AS", "2S", "AH"], + ["AD", "2D", "2H"], + ["2C", "AC"] + ] +} diff --git a/src/test/resources/spanish_patience/ComplexUnsolvable.json b/src/test/resources/spanish_patience/ComplexUnsolvable.json new file mode 100644 index 0000000..ba9be59 --- /dev/null +++ b/src/test/resources/spanish_patience/ComplexUnsolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["AS", "2S", "AH"], + ["AD", "2D", "2H"], + ["AC", "2C"] + ] +} diff --git a/src/test/resources/spanish_patience/SimpleSolvable.json b/src/test/resources/spanish_patience/SimpleSolvable.json new file mode 100644 index 0000000..eacad0e --- /dev/null +++ b/src/test/resources/spanish_patience/SimpleSolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["2S", "AS", "AD"], + ["2D", "2H", "AH"], + ["2C", "AC"] + ] +} diff --git a/src/test/resources/spanish_patience/SimpleUnsolvable.json b/src/test/resources/spanish_patience/SimpleUnsolvable.json new file mode 100644 index 0000000..99a3140 --- /dev/null +++ b/src/test/resources/spanish_patience/SimpleUnsolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["AS", "2S", "2D"], + ["AD", "AH", "2H"], + ["AC", "2C"] + ] +} diff --git a/src/test/spanish_patience_test.cpp b/src/test/spanish_patience_test.cpp index 4168193..8973a53 100644 --- a/src/test/spanish_patience_test.cpp +++ b/src/test/spanish_patience_test.cpp @@ -8,8 +8,21 @@ typedef test_helper th; +const static std::string path = "resources/spanish_patience/"; const static std::string type = "simple-spanish-patience"; -TEST(SpanishPatience, SPTestName) { - EXPECT_TRUE(th::is_solvable("SPTestName", type)); +TEST(SpanishPatience, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(SpanishPatience, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(SpanishPatience, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(SpanishPatience, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); } diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index 2e08829..9c258a3 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -13,7 +13,7 @@ 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 = util::get_file_json("resources/" + input_file + ".json"); + const Document in_doc = util::get_file_json(input_file); const sol_rules rules = sol_rules::from_preset(preset_type); game_state gs(rules, in_doc); -- cgit v1.2.1 From 3759513307f324761b9bf6f4ef20ebeb9c3da98a Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 27 Dec 2017 12:04:33 +0000 Subject: All basic bh tests complete --- src/main/input-output/deal_parser.cpp | 13 ++++++++++ src/main/input-output/deal_parser.h | 1 + src/main/input-output/sol_preset_types.cpp | 4 ++-- src/test/black_hole_test.cpp | 28 ++++++++++++++++++++++ src/test/resources/black_hole/ComplexSolvable.json | 9 +++++++ .../resources/black_hole/ComplexUnsolvable.json | 9 +++++++ src/test/resources/black_hole/SimpleSolvable.json | 9 +++++++ .../resources/black_hole/SimpleUnsolvable.json | 9 +++++++ 8 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 src/test/black_hole_test.cpp create mode 100644 src/test/resources/black_hole/ComplexSolvable.json create mode 100644 src/test/resources/black_hole/ComplexUnsolvable.json create mode 100644 src/test/resources/black_hole/SimpleSolvable.json create mode 100644 src/test/resources/black_hole/SimpleUnsolvable.json (limited to 'src') diff --git a/src/main/input-output/deal_parser.cpp b/src/main/input-output/deal_parser.cpp index fecc7d9..bf4ebaa 100644 --- a/src/main/input-output/deal_parser.cpp +++ b/src/main/input-output/deal_parser.cpp @@ -17,6 +17,11 @@ void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { if (gs.rules.tableau_pile_count > 0) { parse_tableau_piles(gs, doc); } + + // Construct hole card + if (gs.rules.hole) { + parse_hole(gs, doc); + } } void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& doc) { @@ -37,3 +42,11 @@ void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& } } } + +void deal_parser::parse_hole(game_state &gs, const rapidjson::Document& doc) { + assert(doc.HasMember("hole")); + const Value& json_hole = doc["hole"]; + assert(json_hole.IsString()); + + gs.hole.place(card(json_hole.GetString())); +} diff --git a/src/main/input-output/deal_parser.h b/src/main/input-output/deal_parser.h index 93d30fc..80a0f52 100644 --- a/src/main/input-output/deal_parser.h +++ b/src/main/input-output/deal_parser.h @@ -21,6 +21,7 @@ public: static void parse(game_state&, const rapidjson::Document&); private: static void parse_tableau_piles(game_state&, const rapidjson::Document&); + static void parse_hole(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 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/test/black_hole_test.cpp b/src/test/black_hole_test.cpp new file mode 100644 index 0000000..89ee50a --- /dev/null +++ b/src/test/black_hole_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include + +#include "test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/black_hole/"; +const static std::string type = "simple-black-hole"; + +TEST(BlackHole, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(BlackHole, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(BlackHole, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(BlackHole, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} diff --git a/src/test/resources/black_hole/ComplexSolvable.json b/src/test/resources/black_hole/ComplexSolvable.json new file mode 100644 index 0000000..8d0249d --- /dev/null +++ b/src/test/resources/black_hole/ComplexSolvable.json @@ -0,0 +1,9 @@ +{ + "tableau piles": [ + ["5D","4D","3D","2D","AD"], + ["5C","4C","3C","2C","4H","5H"], + ["AC","AH","2H","3H"], + ["2S","5S","4S","3S"] + ], + "hole": "AS" +} diff --git a/src/test/resources/black_hole/ComplexUnsolvable.json b/src/test/resources/black_hole/ComplexUnsolvable.json new file mode 100644 index 0000000..89eb08f --- /dev/null +++ b/src/test/resources/black_hole/ComplexUnsolvable.json @@ -0,0 +1,9 @@ +{ + "tableau piles": [ + ["5","3","2","4","A"], + ["5","3","2","4","A"], + ["5","3","3","4","A"], + ["5","2","2","2"] + ], + "hole": "AS" +} diff --git a/src/test/resources/black_hole/SimpleSolvable.json b/src/test/resources/black_hole/SimpleSolvable.json new file mode 100644 index 0000000..3a7be18 --- /dev/null +++ b/src/test/resources/black_hole/SimpleSolvable.json @@ -0,0 +1,9 @@ +{ + "tableau piles": [ + ["AD","2D","3D","4D","5D"], + ["AC","2C","3C","4C","5C"], + ["AH","2H","3H","4H","5H"], + ["2S","3S","4S","5S"] + ], + "hole": "AS" +} diff --git a/src/test/resources/black_hole/SimpleUnsolvable.json b/src/test/resources/black_hole/SimpleUnsolvable.json new file mode 100644 index 0000000..d6a5e7c --- /dev/null +++ b/src/test/resources/black_hole/SimpleUnsolvable.json @@ -0,0 +1,9 @@ +{ + "tableau piles": [ + ["5D","4D","3D","2D","AD"], + ["5C","4C","3C","2C","AC"], + ["5H","4H","3H","2H","AH"], + ["5S","4S","2S","3S"] + ], + "hole": "AS" +} -- cgit v1.2.1 From 588a36c63991d24566d4af6f1b9722186bdcc286 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 27 Dec 2017 23:55:34 +0000 Subject: Added fc tests - currently intract. --- src/main/input-output/deal_parser.cpp | 40 +++++++++++++++++++--- src/main/input-output/deal_parser.h | 1 + src/test/free_cell_test.cpp | 28 +++++++++++++++ src/test/resources/free_cell/ComplexSolvable.json | 7 ++++ .../resources/free_cell/ComplexUnsolvable.json | 7 ++++ src/test/resources/free_cell/SimpleSolvable.json | 7 ++++ src/test/resources/free_cell/SimpleUnsolvable.json | 7 ++++ 7 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 src/test/free_cell_test.cpp create mode 100644 src/test/resources/free_cell/ComplexSolvable.json create mode 100644 src/test/resources/free_cell/ComplexUnsolvable.json create mode 100644 src/test/resources/free_cell/SimpleSolvable.json create mode 100644 src/test/resources/free_cell/SimpleUnsolvable.json (limited to 'src') diff --git a/src/main/input-output/deal_parser.cpp b/src/main/input-output/deal_parser.cpp index bf4ebaa..2ff5a02 100644 --- a/src/main/input-output/deal_parser.cpp +++ b/src/main/input-output/deal_parser.cpp @@ -22,6 +22,11 @@ void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { if (gs.rules.hole) { parse_hole(gs, doc); } + + // Construct filled cells + if (gs.rules.cells) { + parse_cells(gs, doc); + } } void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& doc) { @@ -38,15 +43,40 @@ void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& ++p.first, ++p.second) { for (auto& json_card : p.first->GetArray()) { + assert(json_card.IsString()); p.second->place(card(json_card.GetString())); } } } -void deal_parser::parse_hole(game_state &gs, const rapidjson::Document& doc) { - assert(doc.HasMember("hole")); - const Value& json_hole = doc["hole"]; - assert(json_hole.IsString()); +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()); + + gs.hole.place(card(json_hole.GetString())); + } +} + +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()); - gs.hole.place(card(json_hole.GetString())); + const auto json_cell_arr = json_cells.GetArray(); + + if (json_cell_arr.Empty()) return; + else if (gs.rules.cells != json_cell_arr.Size()) { + util::json_parse_err("Incorrect number of cells"); + } + + for (auto p = std::make_pair(begin(json_cell_arr), begin(gs.cells)); + p.second != end(gs.cells); + ++p.first, ++p.second) { + + 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 80a0f52..255116b 100644 --- a/src/main/input-output/deal_parser.h +++ b/src/main/input-output/deal_parser.h @@ -22,6 +22,7 @@ public: private: 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/test/free_cell_test.cpp b/src/test/free_cell_test.cpp new file mode 100644 index 0000000..507c915 --- /dev/null +++ b/src/test/free_cell_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include + +#include "test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/free_cell/"; +const static std::string type = "simple-free-cell"; + +TEST(FreeCell, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(FreeCell, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(FreeCell, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(FreeCell, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} diff --git a/src/test/resources/free_cell/ComplexSolvable.json b/src/test/resources/free_cell/ComplexSolvable.json new file mode 100644 index 0000000..2c284d9 --- /dev/null +++ b/src/test/resources/free_cell/ComplexSolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["AH","AC","2C","3C"], + ["AD","2H","2D","3D"], + ["3H","2S","AS","3S"] + ] +} diff --git a/src/test/resources/free_cell/ComplexUnsolvable.json b/src/test/resources/free_cell/ComplexUnsolvable.json new file mode 100644 index 0000000..279c319 --- /dev/null +++ b/src/test/resources/free_cell/ComplexUnsolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["AH","AC","3H","3C"], + ["AD","2H","2D","3D"], + ["2C","2S","AS","3S"] + ] +} diff --git a/src/test/resources/free_cell/SimpleSolvable.json b/src/test/resources/free_cell/SimpleSolvable.json new file mode 100644 index 0000000..2ae2f1a --- /dev/null +++ b/src/test/resources/free_cell/SimpleSolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["3H","2H","AH","AC"], + ["3D","2D","2C","AD"], + ["3S","3C","2S","AS"] + ] +} diff --git a/src/test/resources/free_cell/SimpleUnsolvable.json b/src/test/resources/free_cell/SimpleUnsolvable.json new file mode 100644 index 0000000..f69312d --- /dev/null +++ b/src/test/resources/free_cell/SimpleUnsolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["AH","AC","3H","3C"], + ["AD","2C","2D","3D"], + ["AS","2S","2H","3S"] + ] +} -- cgit v1.2.1